Jump to content

A Simple Backup Tool


FreeFry
 Share

Recommended Posts

It's something small I just made, don't know if it has been made before, perhaps, perhaps not:

Dim $bakExtensions[1] = ["*"]
Dim $ubakExtensions[1] = [".bak"]
Dim $ShellBackupTxt = "Make Backup"
Dim $ShellRestoreTxt = "Restore Backup"

If Not @Compiled Then Exit(MsgBox(16, "Error", "Compile me first.;)"))

_RegisterShellExtension($bakExtensions, @ScriptFullPath, $ShellBackupTxt, "/backup")
_RegisterShellExtension($ubakExtensions, @ScriptFullPath, $ShellRestoreTxt, "/restore", "BackupFile")

If $CmdLine[0] < 2 Or (($CmdLine[0] = 2 And ($CmdLine[1] <> "/backup" And $CmdLine[1] <> "/restore")) Or ($CmdLine[0] = 2 And Not FileExists($CmdLine[2]))) Then Exit(MsgBox(16, "Error", "Wrong usage, only use me from the Context(right click) menu."))
    
Dim $File = $CmdLine[2]
Dim $Action = $CmdLine[1]

_Backup($File, $Action)

Func _Backup(ByRef $svFile, $ivAction)
    
    Local $svNewFile
    Local $ivUserAction
    
    Switch $ivAction
        
        Case "/backup"
            $svNewFile = $svFile & ".bak"
            If FileExists($svNewFile) Then
                $ivUserAction = MsgBox(70, "Attention", "There already exist a backup, do you wish to overwrite this backup, Or create a new backup of this file?" & @LF & "Cancel = Cancel backup, Try Again = Make new backup, Continue = Overwrite")
                Switch $ivUserAction
                    Case 2
                        Return False
                    Case 10
                        Return _Backup($svNewFile, "/backup")
                EndSwitch
            EndIf
            FileCopy($svFile, $svNewFile, 1)
            
        Case "/restore"
            $svNewFile = StringLeft($svFile, StringInStr($svFile, ".", 0, -1)-1)
            If FileExists($svNewFile) Then
                If MsgBox(65, "Attention", "There already exist a non-backup of this file, do you wish to overwrite this file?") = 2 Then Return False
            EndIf
            FileMove($svFile, $svNewFile, 1)
            
    EndSwitch
    
    Return 1
    
EndFunc

Func _RegisterShellExtension($a_ExtArray, $s_AppPath, $s_AssocText, $s_additionalParameter = '"%1"', $s_FileType = "")
    
    Local $s_FullParameter
    
    If $s_additionalParameter <> '"%1"' Then $s_additionalParameter = $s_additionalParameter & ' "%1"'
    
    If UBound($a_ExtArray) = 1 And $a_ExtArray[0] = "*" Then
        RegWrite("HKCR\*\Shell\" & $s_AssocText, "", "REG_SZ", $s_AssocText)
        RegWrite("HKCR\*\Shell\" & $s_AssocText & "\command", "", "REG_SZ", '"' & $s_AppPath & '" ' & $s_additionalParameter)
    Else
        For $i = 0 To UBound($a_ExtArray)-1
            If $s_FileType <> "" Then RegWrite("HKCR\" & $a_ExtArray[$i], "", "REG_SZ", $s_FileType)
            RegWrite("HKCR\" & RegRead("HKCR\" & $a_ExtArray[$i], "") & "\Shell\" & $s_AssocText, "", "REG_SZ", $s_AssocText)
            RegWrite("HKCR\" & RegRead("HKCR\" & $a_ExtArray[$i], "") & "\Shell\" & $s_AssocText & "\command", "", "REG_SZ", '"' & $s_AppPath & '" ' & $s_additionalParameter)
        Next
    EndIf
    
    Return 1
    
EndFunc

Usage:

Just compile it, and run it, it will add itself to the context menu in Windows Explorer, which allows you to quickly make a backup copy of a file, and also supports restoring it later(right click backup, and select restore).

It will create a new filetype in the system registry (HKCU) called BackupFile, and associate .bak files to it.

It also contains one of my other pretty helpful functions, _RegisterShellExtension, which is a pretty straight forward function used to register Shell Extensions in windows:

Func _RegisterShellExtension($a_ExtArray, $s_AppPath, $s_AssocText, $s_additionalParameter = '"%1"', $s_FileType = "")
    
    Local $s_FullParameter
    
    If $s_additionalParameter <> '"%1"' Then $s_additionalParameter = $s_additionalParameter & ' "%1"'
    
    If UBound($a_ExtArray) = 1 And $a_ExtArray[0] = "*" Then
        RegWrite("HKCR\*\Shell\" & $s_AssocText, "", "REG_SZ", $s_AssocText)
        RegWrite("HKCR\*\Shell\" & $s_AssocText & "\command", "", "REG_SZ", '"' & $s_AppPath & '" ' & $s_additionalParameter)
    Else
        For $i = 0 To UBound($a_ExtArray)-1
            If $s_FileType <> "" Then RegWrite("HKCR\" & $a_ExtArray[$i], "", "REG_SZ", $s_FileType)
            RegWrite("HKCR\" & RegRead("HKCR\" & $a_ExtArray[$i], "") & "\Shell\" & $s_AssocText, "", "REG_SZ", $s_AssocText)
            RegWrite("HKCR\" & RegRead("HKCR\" & $a_ExtArray[$i], "") & "\Shell\" & $s_AssocText & "\command", "", "REG_SZ", '"' & $s_AppPath & '" ' & $s_additionalParameter)
        Next
    EndIf
    
    Return 1
    
EndFunc

Usage:

$a_ExtArray, a one dimension array, containing strings, of file extensions.

$s_AppPath, the filepath to the program that will gets run when the specified item is selected in the context menu.

$s_AssocText, the text to display in the context menu.

$s_additionalParameter, any additional parameters to send to the program(default, and always last, is %1(which is the path to the file that is right clicked on, also note that this parameter is optional)

$s_FileType, is the FileType to register the extension(s) to(note that if this parameter is specified, it will re-associate, the file extension to the specified filetype(should not be used with default/already registered extensions, this is also an optional parameter(mostly made for using with non registered extensions, like in my backup tool: .bak).

Constructive criticism, and/or suggestions are very welcome. muttley

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...