Jump to content

Nimps Folder Backup


Recommended Posts

"Nimps" is a regional slang word, which means easy or simple or basic and such.

I made a quick little script the other day to backup the visual studio project folder I was actively working on.

Thought I'd generalise it for use with the current active windows explorer folder.

When this script is running, and you hit "Ctrl and Shift and b", if a folder is open in windows explorer, it will copy the contents of it to your desired location ($GENERAL_BACKUP_FOLDER) including subfolders and its files and folders (so beware on deep directories)

If you have command line version of 7zip (7za.exe) you can choose to compress the folder rather than just copy it ($ZIP_FOLDER = True), or both I suppose.

Think that's all.

Reading the code should tell you anything else.

Oh, success or failure is indicated by a high or low frequency Beep().

#include <AutoItConstants.au3>
#include <String.au3>

HotKeySet("^+b", _Backup) ; ctrl and shift and b
HotKeySet("!+b", _Exit) ; alt and shift and b

Global Const $GENERAL_BACKUP_FOLDER = @ScriptDir & "Backup\"
Global Const $ZIP_FOLDER = False
Global Const $PATH_TO_7ZA = "D:\7za.exe"

While 3

; High frequency beep if successful
; Low frequency beep if failed
Func _Backup()
    Local $FOLDER_TO_BACKUP = ""
    Local $BACKUP_FOLDER = ""
    Local $WIN_TITLE = WinGetTitle("[Active]")
    Local $WIN_TEXT = WinGetText("[Active]")

    ; If text of window has no address, it is not a windows explorer
    If StringInStr($WIN_TEXT, "Address: ") Then
        Local $ACTIVE_EXPLORER_ADDRESS = _StringBetween($WIN_TEXT, "Address: ", @LF)
        If Not IsArray($ACTIVE_EXPLORER_ADDRESS) Then
            Beep(400, 150)
            Return @ScriptLineNumber
        Return MsgBox(0, "Info", "The active window is not explorer")

    ; Check to see if root drive is backup source
    If StringRight($FOLDER_TO_BACKUP, 2) = ":\" Then
        $Answer = MsgBox(4, "Warning", "You are about to attempt to backup a whole disk" & @CRLF & "Are you sure?")
        If $Answer <> 6 Then Return

    ; Date and time to append to backup zip file or folder
    $APPEND = "_" & @YEAR & "_" & @MON & "_" & @MDAY & "_" & @HOUR & "_" & @MIN & "_" & @SEC

    ; Zip to backup folder and return, 7za.exe required
    If $ZIP_FOLDER Then

        ; Buile command for 7za
        $command = $PATH_TO_7ZA & ' a ' & '"' & $GENERAL_BACKUP_FOLDER & $WIN_TITLE & $APPEND & '"' & '.zip ' & '"' & $FOLDER_TO_BACKUP & '"'
        $pid = Run($command, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
        While ProcessExists($pid)
        $out = StdoutRead($pid)

        ; Indicate if successfull
        If StringInStr($out, "Everything is Ok") Then
            Beep(700, 200)
        Beep(400, 200)

    ; Copy folder and beep when complete, no 7zip needed.
    Local $RTN = DirCopy($FOLDER_TO_BACKUP, $CURRENT_BACKUP_FOLDER, 1) ? Beep(700, 500) : Beep(400, 500)

EndFunc   ;==>_Backup

Func _Exit()
EndFunc   ;==>_Exit


Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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

  • Create New...