Code:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#Persistent
#SingleInstance, Ignore
; ************************************************************************************
;
; Generic Timed File Backup Script
;
; v0.05 Beta - b6210701
;
; Modified by corrupt - July 21, 2007
; ************************************************************************************
; Config
; ************************************************************************************
; -----------------------------------------------------
; Time until next backup
; -----------------------------------------------------
B_Time = 3600000
; Default = 3600000 (once an hour) if not specified
; -----------------------------------------------------
; Backup when script starts?
; -----------------------------------------------------
B_startup := True
; Default = True if not specified
; -----------------------------------------------------
; Allow Force Full Backup Tray Menu Item
; -----------------------------------------------------
B_ForceBackup := True
;
; Default = False
;
; This item will force file backup of all files in the
; backup list regardless of timestamp
; -----------------------------------------------------
; Log file location
; -----------------------------------------------------
B_Log = c:\GTB_log.txt
; An invalid path (or lack of) will cause discard log info
; -----------------------------------------------------
; Number of backups to keep in log:
; -----------------------------------------------------
B_Num = 4
;
; (default = 4):
;
; -----------------------------------------------------
; Show progress bar
; -----------------------------------------------------
B_pbar := True
;
; Default = True
;
; -----------------------------------------------------
; Allow <Esc> to cancel backup
; -----------------------------------------------------
B_AllowCancel := True
;
; Default = True
;
;
; -----------------------------------------------------
; Display completion dialog (times out after 30 seconds)
; -----------------------------------------------------
B_Complete := True
;
; Default = True
;
; -----------------------------------------------------
; Use external file for loading Backup Info instead?
; -----------------------------------------------------
B_UseExtFile := False
;
; An invalid path will halt the backup with an error
; if set to use an external file
;
; -----------------------------------------------------
; External Backup Info file location:
; -----------------------------------------------------
B_ExtFile = blist.txt
;
; - the script's directory will be assumed as path if
; the path isn't specified.
;
; - tested ok with network share
;
; - An invalid path will halt the backup with an error
; if set to use an external file
;
; -----------------------------------------------------
; Backup Locations, options (if external file not selected)
; -----------------------------------------------------
; use | symbol to separate the following:
; source, destination, backup subdirectories?, empty subdirectories too?
; options ---> 1 = yes, 0 = no
; use ? to separate each set of backup info
BackupInfo = c:\Program Files\AutoHotkey\*.txt|c:\MyBackup\AutoHotkey text files|1|0
BackupInfo = %BackupInfo%?%A_MyDocuments%\desktop.ini|c:\MyBackup\destop.ini backup files|0|0|backup|A_Now
; BackupInfo = %BackupInfo%?another source|another destination|1|1
; BackupInfo = %BackupInfo%?another source|another destination|1|1
; BackupInfo = %BackupInfo%?another source|another destination|1|1
; BackupInfo = %BackupInfo%?another source|another destination|1|1
; BackupInfo = %BackupInfo%?another source|another destination|1|1
; etc...
; Optional:
; - prepend filename(s), append filename(s)
; * specify the text to append or prepend or specify A_Now to add date/time
; to the filename(s).
; note: the script will add an underscore character when appending/prepending to the filename
; note: the appended text will appear before the file extension (if any)
; note: the prepend/append options will always create a new destination file if A_Now is specified
; Example:
; BackupInfo = %A_MyDocuments%\desktop.ini|c:\MyBackup\destop.ini backup files|0|0|backup|A_Now
; -----------------------------------------------------
; Admin Mode
; -----------------------------------------------------
B_Admin := True
;
; Default = False
;
; Allow additional menu items & Functions
;
; ************************************************************************************
; Code Start (v0.05 Beta - b6210701)
; ************************************************************************************
;
; -----------------------------------------------------
; Autoexec
; -----------------------------------------------------
Menu, Tray, NoStandard
Menu, Tray, Add, Show Log File, B_ShowLog
if B_Admin = 1
{
Menu, Tray, Add, Edit Script, B_EditScript
Menu, Tray, Add, Edit Backup Locations (Ext. File), B_EditLoc
Menu, Tray, Add, Reload Backup Locations (Ext. File), B_PExtFile
Menu, Tray, Add, ; Separator
Menu, Tray, Add, Reload Script, B_Reload
}
Menu, Tray, Add, ; Separator
Menu, Tray, Add, Start Backup, BackupC
if (B_ForceBackup = 1 or B_Admin = 1)
Menu, Tray, Add, Start Forced Backup, B_Fbackup
Menu, Tray, Add, ; Separator
Menu, Tray, Add, About, B_About
Menu, Tray, Add, Exit, B_Exit
Menu, Tray, Default, Start Backup
; -----------------------------------------------------
if B_AllowCancel =
B_AllowCancel = 1
; -----------------------------------------------------
if B_pbar =
B_pbar = 1
; -----------------------------------------------------
if B_Complete =
B_Complete = 1
; -----------------------------------------------------
if B_Log <>
{
FileAppend,,%B_Log%
if ErrorLevel = 1
B_Log =
}
; -----------------------------------------------------
Gosub, B_PExtFile
; -----------------------------------------------------
if B_Time =
B_Time = 3600000 ; default once an hour if not specified
if B_Startup <> 0
Gosub, BackupC ; copy when script starts (default yes if not specified)
; -----------------------------------------------------
OnExit, B_Exit
; -----------------------------------------------------
SetTimer, BackupC, %B_Time%
Return
;
; -----------------------------------------------------
; Optionally Cancel Backup while running
; -----------------------------------------------------
Esc::
B_Cancel:
If B_AllowCancel = 1
if B_Running = 1
{
B_StopProcess = 1
Progress,,Cancelling... Please Wait...
}
Return
;
; -----------------------------------------------------
; Timed Backup
; -----------------------------------------------------
BackupC:
; -----------------------------------------------------
; Start Time
; -----------------------------------------------------
B_Lvar = %B_Lvar%Backup Started: %A_DDDD%, %A_MMMM%%A_Space%%A_DD%, %A_Hour%:%A_Min%:%A_Sec%
B_Running = 1
; -----------------------------------------------------
; Count Backup Items
; -----------------------------------------------------
B_Titems := 0
B_Temp1 =
Loop, Parse, BackupInfo, ?
{
B_Titems ++
if A_LoopField =
{
B_Titems --
if A_Index = 1
{
StringTrimLeft, BackupInfo, BackupInfo, 1
}
}
else
{
if B_Titems = 1
{
B_Temp1 = %A_LoopField%
}
else
{
B_Temp1 = %B_Temp1%?%A_LoopField%
}
}
}
if B_UseExtFile = 1
{
BackupInfo = %B_Temp1%
}
B_Temp1 =
;
; -----------------------------------------------------
; Split backup paths from main string
; -----------------------------------------------------
Loop, Parse, BackupInfo, ?
{
B_ItemNum = %A_Index%
; -----------------------------------------------------
; Assign paths/option(s) to variables
; -----------------------------------------------------
n_prep =
n_appp =
Loop, Parse, A_LoopField, |
{
if A_Index = 1
{
CopySourcePattern = %A_LoopField%
; -----------------------------------------------------
; Determine path of source
; -----------------------------------------------------
RelPath =
Loop, %A_LoopField%
RelPath = %A_LoopFileDir%
if RelPath =
{
SplitPath, A_LoopField,, OutDir,,, OutDrive
StringReplace, OutDir, OutDir, %OutDrive%,
RelPath = %OutDrive%%OutDir%
}
}
else if A_Index = 2
{
CopyDest = %A_LoopField%
StringRight, Temp1, CopyDest, 1
if Temp1 = \
StringTrimRight, CopyDest, CopyDest, 1
}
else if A_Index = 3
{
CopySubFlag = %A_LoopField%
}
else if A_Index = 4
{
CpEmpty = %A_LoopField%
}
else if A_index = 5
{
n_prep = %A_LoopField%
}
else if A_index = 6
{
n_appp = %A_LoopField%
}
}
;
; -----------------------------------------------------
; Show progress bar (optional)
; -----------------------------------------------------
if B_pbar = 1
{
Progress, W480 B P0 R0-%PBcount%, %CopySourcePattern%, Calculating... Please Wait... (Esc To Cancel)
Loop, %CopySourcePattern%, 1, %CopySubFlag%
{
PBcount = %A_Index%
}
if B_AllowCancel = 1
{
Progress, W480 B P0 R0-%PBcount%, %CopySourcePattern%, Backup Progress - %B_ItemNum% of %B_Titems% (Esc To Cancel)
}
else
{
Progress, W480 B P0 R0-%PBcount%, %CopySourcePattern%, Backup Progress - %B_ItemNum% of %B_Titems%
}
}
; -----------------------------------------------------
; locate files for backup
; -----------------------------------------------------
Loop, %CopySourcePattern%, 1, %CopySubFlag%
{
if B_StopProcess = 1
{
B_Running = 0
Gosub, BCancelled
B_StopProcess = 0
Return
}
If (Instr(A_LoopFileFullPath, CopyDest) = 1)
Continue
LRelPath =
; -----------------------------------------------------
; Get Relative Path
; -----------------------------------------------------
StringReplace, A_LoopRelativeFileName, A_LoopFileFullPath, %RelPath%\, ;
; -----------------------------------------------------
; Update Progress Bar
; -----------------------------------------------------
if B_pbar = 1
Progress, %A_Index%, %A_LoopFileName%
; -----------------------------------------------------
; determine if file needs to be backed up
; -----------------------------------------------------
copy_it = n
A_LoopRelativeFileNamePA =
If (!(n_appp = "") OR !(n_prep = "")) {
A_LoopRelativeFileNamePA = %A_LoopRelativeFileName%
alrfp = %CopyDest%\%A_LoopRelativeFileName%
SplitPath, CopyDest,,dirt
SplitPath, alrfp, alrfn, alrdir, alrext, alrname
StringTrimLeft, alrdir, alrdir, (StrLen(dirt))
If (SubStr(alrdir, 1, 1) = "\")
StringTrimLeft, alrdir, alrdir, 1
StringTrimLeft, alrfp, alrfp, (StrLen(CopyDest) + 1)
If (SubStr(alrfp, 1, 1) = "\")
StringTrimLeft, alrfp, alrfp, 1
StringTrimRight, alrfp, alrfp, (StrLen(alrname . alrext) + 1)
If !(n_prep = "") {
If (n_prep = "A_Now")
n_prep = %A_Now%
alrname := n_prep . "_" . alrname
}
If !(n_appp = "") {
If (n_appp = "A_Now")
n_appp = %A_Now%
alrname := alrname . "_" . n_appp
}
A_LoopRelativeFileName := alrfp . alrname . "." . alrext
}
IfNotExist, %CopyDest%\%A_LoopRelativeFileName% ; Always copy if target file doesn't yet exist.
copy_it = y
else
{
FileGetTime, time, %CopyDest%\%A_LoopRelativeFileName%
EnvSub, time, %A_LoopFileTimeModified%, seconds ; Subtract the source file's time from the destination's.
if time < 0 ; Source file is newer than destination file.
copy_it = y
}
if B_doforce = 1
copy_it = y
; -----------------------------------------------------
; backup file(s), (and files in sub directories if selected)
; -----------------------------------------------------
If !(A_LoopRelativeFileNamePA = "") {
A_LRPT = %A_LoopRelativeFileName%
A_LoopRelativeFileName = %A_LoopRelativeFileNamePA%
}
if copy_it = y
{
; -----------------------------------------------------
; Create Directory if necessary
; -----------------------------------------------------
LRelPath =
Temp1 =
FileGetAttrib, LCurAttrib, %A_LoopFileFullPath%
IfInString, LCurAttrib, D
{
LRelPath = %CopyDest%\%A_LoopRelativeFileName%
Dir1 = 1
}
else
{
Loop, %A_LoopFileFullPath%
{
StringReplace, LRelPath, A_LoopRelativeFileName, %A_LoopFileName%,
LRelPath = %CopyDest%\%LRelPath%
Dir1 = 0
}
}
StringRight, Temp1, LRelPath, 1
if Temp1 = \
StringTrimRight, LRelPath, LRelPath, 1
IfNotExist, %LRelPath%
{
If Dir1 = 0
{
Gosub, B_CheckNCreate
}
else
{
if CopySubFlag = 1
if CpEmpty = 1
Gosub, B_CheckNCreate
}
}
; -----------------------------------------------------
; copy file
; -----------------------------------------------------
if Dir1 = 0
{
If !(A_LoopRelativeFileNamePA = "")
A_LoopRelativeFileName = %A_LRPT%
FileCopy, %A_LoopFileFullPath%, %CopyDest%\%A_LoopRelativeFileName%, 1 ; Copy with overwrite=yes
if ErrorLevel <> 0
{
B_Lvar = %B_Lvar%`r`n Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopRelativeFileName%".
}
else
{
B_Lvar = %B_Lvar%`r`n Backup Ok - "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopRelativeFileName%"
}
}
}
}
}
; -----------------------------------------------------
; Write log file (if specified)
; -----------------------------------------------------
Progress, Off
B_Running = 0
if B_Log <>
{
B_Lvar3 =
B_Temp3 := "****************************************************************?"
B_Lvar = %B_Lvar%`r`nBackup Completed: %A_DDDD%, %A_MMMM%%A_Space%%A_DD%, %A_Hour%:%A_Min%:%A_Sec%
B_Lvar = %B_Lvar%`r`n%B_Temp3%
FileRead, B_Lvar2, %B_Log%
Loop, Parse, B_Lvar2, ?
{
if A_Index = 1
{
B_Lvar3 = `r`n%A_LoopField%?
}
else if A_Index < %B_Num%
{
B_Lvar3 = %B_Lvar3%%A_LoopField%?
}
}
StringTrimRight, B_Lvar3, B_Lvar3, 3
B_Lvar2 = %B_Lvar3%
B_Lvar3 =
B_Lvar = %B_Lvar%`r`n%B_Lvar2%
FileDelete, %B_Log%
FileAppend, %B_Lvar%, %B_Log%
B_Lvar =
B_Lvar2 =
if B_Complete = 1
{
MsgBox, 52, Finished, Backup Completed. `r`n`r`nLog file generated: `r`n%B_Log% `r`n`r`nOpen Log File?, 30
ifMsgBox, Yes
{
Sleep, 1000
Run, Wordpad.exe %B_Log%
}
}
}
else
{
if B_Complete = 1
MsgBox, 48, Finished, Backup Completed. `r`n`r`nLog file not generated (not specified), 30
}
Return
; -----------------------------------------------------
; Backup Cencelled - Write log file (if specified)
; -----------------------------------------------------
BCancelled:
Progress, Off
if B_Log <>
{
B_Lvar3 =
B_Temp3 := "****************************************************************?"
B_Lvar = %B_Lvar%`r`nBackup Cancelled: %A_DDDD%, %A_MMMM%%A_Space%%A_DD%, %A_Hour%:%A_Min%:%A_Sec%
B_Lvar = %B_Lvar%`r`n%B_Temp3%
FileRead, B_Lvar2, %B_Log%
Loop, Parse, B_Lvar2, ?
{
if A_Index = 1
{
B_Lvar3 = `r`n%A_LoopField%?
}
else if A_Index < %B_Num%
{
B_Lvar3 = %B_Lvar3%%A_LoopField%?
}
}
StringTrimRight, B_Lvar3, B_Lvar3, 3
B_Lvar2 = %B_Lvar3%
B_Lvar3 =
B_Lvar = %B_Lvar%`r`n%B_Lvar2% ; linefeeds removed
FileDelete, %B_Log%
FileAppend, %B_Lvar%, %B_Log%
B_Lvar =
B_Lvar2 =
if B_Complete = 1
{
MsgBox, 52, Finished, Backup Cancelled. `r`n`r`nLog file generated: `r`n%B_Log% `r`n`r`nOpen Log File?, 30
ifMsgBox, Yes
{
Sleep, 1000
Run, Wordpad.exe %B_Log%
}
}
}
else
{
If B_Complete = 1
MsgBox, 48, Finished, Backup Cancelled. `r`n`r`nLog file not generated (not specified), 30
}
Return
; -----------------------------------------------------
; Create Sub directories as necessary
; -----------------------------------------------------
B_CheckNCreate:
B_CpBase =
Loop, Parse, LRelPath,\
{
if A_Index = 1
{
if A_LoopField =
Break
B_CpBase = %A_LoopField%
}
else
{
B_CpBase = %B_CpBase%\%A_LoopField%
IfNotExist, %B_CpBase%
FileCreateDir, %B_CpBase%
If ErrorLevel
B_Lvar = %B_Lvar%`r`n Error: Could not create destination directory: %B_CpBase%
}
}
B_CpBase =
LRelPath =
Return
; -----------------------------------------------------
; Show Log File
; -----------------------------------------------------
B_ShowLog:
Run, Wordpad.exe %B_Log%
Return
; -----------------------------------------------------
; Show About Box
; -----------------------------------------------------
B_About:
MsgBox, 0, ABout, Generic File Backup Script`r`n`r`nv0.05 Beta - b6210701, 6
Return
; -----------------------------------------------------
; Edit script
; -----------------------------------------------------
B_EditScript:
Run, Notepad %A_ScriptFullPath%
Return
; -----------------------------------------------------
; Edit Backup Locations
; -----------------------------------------------------
B_EditLoc:
IfExist, %B_ExtFile%
{
RunWait, Notepad %B_ExtFile%
}
else
{
MsgBox, 0, Error, External Backup file: `r`nB_ExtFile`r`n`r`rdoes not exist., 4
}
Return
; -----------------------------------------------------
; Forced Backup
; -----------------------------------------------------
B_Fbackup:
B_doforce = 1
Gosub, BackupC
B_doforce = 0
Return
; -----------------------------------------------------
; Process external file
; -----------------------------------------------------
B_PExtFile:
if B_UseExtFile = 1
{
IfInString, B_ExtFile, \
B_Temp2 = 1
IfInString, B_ExtFile, /
B_Temp2 = 1
if B_Temp2 <> 1
B_ExtFile = %A_ScriptDir%\%B_ExtFile%
IfExist, %B_ExtFile%
{
FileRead, BackupInfo, %B_ExtFile%
StringReplace, BackupInfo, BackupInfo, `r`n, ?, All
}
else
{
MsgBox, 48, Error, External backup file not found:`r`n`r`n%B_ExtFile%`r`n`r`n Backup Cancelled., 30
Return
}
}
Return
; -----------------------------------------------------
; Reload Script
; -----------------------------------------------------
B_Reload:
Reload
Return
; -----------------------------------------------------
; Exit
; -----------------------------------------------------
B_Exit:
Gosub, B_Cancel
ExitApp
; ************************************************************************************
Bookmarks