Jump to content

Few noob question about using AUTOIT


Recommended Posts

First, I am new to AUTOIT, and I am kind of dummy about scripts language and I trying to learn about this wonderful tool. I have read the Help file and quite confusing, I hope someone can help me answer these questions or show me a link to something similar that can help me get more understand about the problems:

1 - I usually working at office and save all data on my USB drive, I use AUTOIT (Au3Record.exe to be precise) to make a script Backup.au3 like this:  "1-Open Backup folder 2-Open USB 3-Drag all file in USB into Backup folder 4-Eject USB after amount of time". My question for this case are:

    a - When ever I running this script file, I cannot using my mouse to do anything else because it will interrupted the script, how can i avoid that problem and let the script run in silent?

    b - In case of a file already inside the Backup folder, how can I make the script keep the new file and rename the old file with day behind it (example: testdocument.doc and testdocument 12 - 28 - 2015.exe)?

    c - I have few USB drives, how can I tell the script Backup.au3 to continues doing the same process like the previous USB with out double click on the script again?

    d- While the script is running, if something happened and I want to stop the script, how can i set up hotkey for that situation? Also, how  can i make a note popup in middle of a desktop telling me that a script is running (I try the tooltip before but that one appear on top left of the desktop and i don't want that)

2 - I have 4 PCs connected to each other via LAN Network, all of them have a test script Test.au3 on their Desktop, how can I make a script that when I run it on what ever PC, the script will automatically run the Test.au3 file on each of PC?

Thank you!

 

Link to comment
Share on other sites

Welcome to the forums!

1a. Instead of copying over files using your mouse click you can use FileCopy or FileMove.

1b. Use _FileLIstToArray to get the files in the Backup folder, before using FileCopy you check to see if the file exists in the Backup Folder. If it exists the use FileMove to rename the old file.

1c. There's a few ways to do this:

  1. Have the script run indefinitely and start the backup process using a hotkey, or create a GUI and have it set to a button (If you use a GUI you could have the script output what it's doing. I.e., copying file Filename.txt.... copying complete, renaming old backup file *Filename.txt* to *Filename (old).txt*)
  2. You could still have the script run indefinitely and restart itself each time the last USB drive is removed and a new one is inserted. (Function: DriveGetDrive("ALL") will give you all the drive letters.)

1d. Create a global variable (There's a few examples on the forums where they used $Paused) and set the hotkey to change the variable to true/false for paused/unpaused.

Here's an example, I didn't implement it all, you'll have to change some things to make it work how you want, but it should give you a starting point

#include <GUIConstants.au3>
#include <AutoItConstants.au3>
#include <File.au3>
#include <Array.au3>

Global $frmMain = GUICreate("Main Window", 400, 400)
Global $btnStartBackup = GUICtrlCreateButton("Start Backup", 10, 10, 75, 25)
Global $lblStatus = GUICtrlCreateLabel("Paused", 95, 10, 75, 25)
Global $edtOutput = GUICtrlCreateEdit("", 10, 45, 380, 350)

Global $aFilesToBackup[] = ["Test.txt", "My File.txt", "Old Script.au3"]
Global $sFilePath = @ScriptDir & '\'

GUICtrlSetFont($lblStatus, 10, 400, "", "Segoe UI")
GUICtrlSetFont($edtOutput, 10, 400, "", "Segoe UI")

GUISetState(@SW_SHOW, $frmMain)

While (True)
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btnStartBackup
            GUICtrlSetData($lblStatus, "Running")
            BackupUSB()
            GUICtrlSetData($lblStatus, "Paused")
    EndSwitch
WEnd

Func BackupUSB()
    Local $drives = DriveGetDrive("ALL")
    Local $iBackupDrive = -1
    Local $aOldFiles

    ; If getting the drive letters failed, send an error message and return
    If (@Error) Then
        DebugOutput("Failed to get Drive Letters" & @CRLF)
        Return
    EndIf

    ; Go through each drive letter and find the one that's USB. May have to change this if you have multiple USB's plugged int
    For $i = 1 to $drives[0]
        If (DriveGetType($drives[$i], $DT_BUSTYPE) = "USB") Then
            $iBackupDrive = $i
            ExitLoop
        EndIf
    Next

    ; If the $iBackupDrive is still -1, the for loop failed, send an error message and return
    If ($iBackupDrive = -1) Then
        DebugOutput("No USB Drive detected" & @CRLF)
        Return
    ; Else the $iBackupDrive is not -1 and the for loop succeeded, send a message that the backup process is starting
    Else
        DebugOutput("USB Drive detected. Backing up Drive " & $drives[$iBackupDrive] & @CRLF)
    EndIf

    ; Get all the files in the backup folder on the USB drive
    $aOldFiles = _FileListToArray($drives[$iBackupDrive] & "\Backup Folder\", $FLTA_FILES)

    ; Go through each file that we're going to backup from the computer
    For $i = 0 to UBound($aFilesToBackup) - 1
        ; If the file already exists then rename it
        If (FileExists($drives[$iBackupDrive] & "\Backup Folder\" & $aFilesToBackup[$i])) Then
            Local $sFileExtension = StringTrimLeft($aFilesToBackup[$i], StringInStr($aFilesToBackup[$i], '.') - 1)
            Local $sFileName = StringTrimRight($aFilesToBackup[$i], StringLen($sFileExtension)) & ' ' & @MON & '-' & @MDAY & '-' & @YEAR
            
            DebugOutput("File " & $aFilesToBackup[$i] & " already exists. Renaming to " & $sFileName & $sFileExtension & @CRLF)

            If (FileMove($drives[$iBackupDrive] & "\Backup Folder\" & $aFilesToBackup[$i], $drives[$iBackupDrive] & "\Backup Folder\" & $sFileName & $sFileExtension)) Then
                DebugOutput(@TAB & "File renamed successfully" & @CRLF)
            Else
                DebugOutput(@TAB & "Failed to rename file" & @CRLF)
            EndIf
        EndIf

        If (FileCopy($sFilePath & $aFilesToBackup[$i], $drives[$iBackupDrive] & "\Backup Folder\" & $aFilesToBackup[$i])) Then
            DebugOutput("Successfully copied file " & $aFilesToBackup[$i] & @CRLF)
        Else
            DebugOutput("Failed to copy file " & $aFilesToBackup[$i] & @CRLF)
        EndIf
    Next

    DebugOutput("Backup process complete" & @CRLF)
EndFunc

Func DebugOutput(Const ByRef $sMsg)
    GUICtrlSetData($edtOutput, GUICtrlRead($edtOutput) & $sMsg)
EndFunc

 

Edited by InunoTaishou
Link to comment
Share on other sites

Lovely, thank you very much for the quick reply. I am will explore and play around with your example. In the mean time can you tell me (base on the script above), where should i change to make it recognize f drive as usb, and backup folder is located at Destop?

Thank you very much, again, and have a great day!

Link to comment
Share on other sites

In the script I posted

Line 11 is an array of all of the files that you would want to copy to the backup folder on your usb drive

Line 12 is the path of where all the files are at. Change @ScriptDir to @DesktopDir (Or you could just make it C:\Users\Name\Desktop\ yourself)

Line 31 is what gets all of the drive letters on your system, 42-47 check to see which drive letter is a USB drive. If you just wanted to check F: then you should get rid of the loop for 42-47 and make it

If (DriveGetType("F:", $DT_BUSTYPE) = "USB") Then
        ; Set the drive letter to a variable to be used in the rest of the function
    Else
        DebugOutput("F: is not a USB drive" & @CRLF)
    EndIf

 

Edited by InunoTaishou
Link to comment
Share on other sites

Hello Vincent3da, welcome to the Forum and AutoIt. :D

What you did with 'Au3Record.exe' was record a macro. What it did was create a script (.au3) based on your actions (mouse movement etc). That means, that when you run it, it acts much like as if you were doing the actions yourself, and generally only one person can use the same PC at one time.

What you need to do, so that things work silently, is modify that script. But first off, you need to study the AutoIt language to get the basics of coding.

InunoTaishou has been very kind to you, and provided a whole working script, that should probably meet your need with a little adjustment. Alas though, you cannot shortcut learning the basics, so I suggest you try some of the tutorials here, which will help you better understand what he has provided.

Edited by Santa

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...