Skip to content

Instantly share code, notes, and snippets.

@hidsh
Created February 6, 2012 07:34
Show Gist options
  • Save hidsh/51b8ac76d0dc69771b5d to your computer and use it in GitHub Desktop.
Save hidsh/51b8ac76d0dc69771b5d to your computer and use it in GitHub Desktop.
backup file /w rotation
' bak_rotate
nbak_default = 10 'number of backups as default
' -- main procedure
Set args = WScript.Arguments
If args.Count < 1 Or args.Count > 2 Then
error_exit("usage: bak_rotate.vbs filename n" & vbCrLf & _
" bak_rotate.vbs filename" & vbCrLf & _
" e.g. bak_rotate.vbs x.c 3 --> x_0.c x_1.c x_2.c" & vbCrLf & _
" bak_rotate.vbs x.c --> x_0.c .. x_9.c")
End If
orig = args(0) ' original (backup source)
If args.Count = 2 then
nbak = args(1)
Else
nbak = nbak_default
End If
If exist_p(orig) = False Then
error_exit "not found: " & orig
End If
bak_rotate orig, nbak
' -- sub functions
Sub error_exit(msg)
MsgBox msg
WScript.Quit -1
End Sub
Function exist_p(path)
Set fso = CreateObject("Scripting.FileSystemObject")
exist_p = fso.FileExists(path)
End Function
Sub mv(src, dest)
Set fso = CreateObject("Scripting.FileSystemObject")
fso.MoveFile src, dest ' CANNOT overwrite
End Sub
Sub cp(src, dest)
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile src, dest, True ' overwrite
End Sub
Sub rm(path)
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile path, True ' force delete
End Sub
Function get_dest_name(orig, n)
a = split(orig, ".")
base = ""
For i=0 To UBound(a)-1
base = base & a(i) & "."
Next
base = left(base, len(base)-1)
ext = a(UBound(a))
get_dest_name = base & "_" & n & "." & ext
End Function
Sub bak_rotate(orig, nbak)
found = False
For i=nbak-1 To 0 Step -1
young = get_dest_name(orig, i)
old = get_dest_name(orig, i+1)
If exist_p(young) Then
If i<nbak-1 Then
mv young, old
Else
rm young
End If
End If
Next
cp orig, get_dest_name(orig, 0)
End Sub
' this program ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment