Saturday, September 19, 2009

AutoHotKey - BackUp Tool

허접 백업 툴. 만들면서 헷갈렸던 부분들은 주석으로 달아둠.


;===================================================
; for BackUp
;===================================================

^!b::
;-------- Speed up the BackUp Script -------------------------------
SetBatchLines, -1 ; Make the operation run at maximum speed.

;-------- Initialize the Variables ---------------------------------
FolderSizeKB = 0

;-------- Choose the source folder [ sourceFolder ] ---------------------------------------------------------
sourceFolder = C:\backup
MsgBox, 33, BackUp Script, %sourceFolder% will be backuped.

IfMsgBox, Cancel, GOTO EXITMYBACKUPSCRIPT

;-------- Choose the target folder [ targetFolder ] ---------------------------------------------------------
FileSelectFolder, pretargetFolder, ::{20d04fe0-3aea-1069-a2d8-08002b30309d}, 0, Target Folder`r`n>>> Choose a Drive to BackUp >>> ; CLSID indicates 'My Computer'

; Get the free space of target drive [ free ] ----
DriveSpaceFree, free, %pretargetFolder%
EnvMult, free, 1000

;------- Add the DATE to targetFolder ------------
folderSuffix = %A_Year%%A_MM%%A_DD% ; DATE is automatically set to today.
targetFolder = %pretargetFolder%\%folderSuffix%
FileCreateDir, %targetFolder%

;------- Get the size of source folder [ FolderSizeKB ] ----------------------
Loop, %sourceFolder%\*.*, , 1
FolderSizeKB += %A_LoopFileSizeKB%
;------- Compare the Free space & Folder size -------------------------------
If free > %FolderSizeKB% ; Watch the format of IF is 'If, Var, Value'.
; ; Var is Name(Content) of Variable, and Value is NOT Variable
{
FileCopyDir, %sourceFolder%, %targetFolder%, 1

ErrorLevel := 0
If ErrorLevel
{
MsgBox FAIL the BackUp
GOTO EXITMYBACKUPSCRIPT
}
else
{
MsgBox Success the BackUp
GOTO EXITMYBACKUPSCRIPT
}
}
else
{
MsgBox Out of Space !
GOTO EXITMYBACKUPSCRIPT
}

;------- Leave the BackUp Script & Clear variables ----------------
EXITMYBACKUPSCRIPT:

VarSetCapacity(FolderSizeKB,64)
VarSetCapacity(sourceFolder,64)
VarSetCapacity(targetFolder,64)
VarSetCapacity(pretargetFolder,64)
VarSetCapacity(folderSuffix,64)
VarSetCapacity(free,64)
VarSetCapacity(ErrorLevel,64)
VarSetCapacity(A_LoopFileSizeKB,64)

VarSetCapacity(FolderSizeKB,0)
VarSetCapacity(sourceFolder,0)
VarSetCapacity(targetFolder,0)
VarSetCapacity(pretargetFolder,0)
VarSetCapacity(folderSuffix,0)
VarSetCapacity(free,0)
VarSetCapacity(ErrorLevel,0)
VarSetCapacity(A_LoopFileSizeKB,0)

return


No comments: