Sign in to follow this  
Followers 0

backup mydocs routine - saves up to 3 versions of files

1 post in this topic

#1 ·  Posted (edited)

This script is what I developed for a business to backup mydocs files saving up to 3 iterations of any changed file... First it copies any .bat & .txt files from the root folder (for logs and such) to a mydocs folder named "data". It also copies any office dot files to mydocs. It also does a screen capture for debugging in case it doesn't work as expected. It deletes old "temp" files also. It closes outlook before copy begins. It also saves the name of all open windows in a file named c:openwins.txt.

It creates a file called c:backup.txt that shows all files backed up, where they were backed up and the date the backup was done. I use a scheduled job to print the file every morning so the user can see that in fact their backup is working...

The script uses the "xcopy" command and will back up to a local drive or a network drive.. the backup does files with archive flag set and then clears the archive flag. Change the xcopy option for the archive flag if you want all files backed up the first time around...(remove /m) The destination folder and some other info is stored in an .ini file in the root directory.. You must create the folders wherever you will be backing up. The script still contains some options I do not use anymore.. they are commented out.... It also has an exclude files capability.



; in the root folder (C:) create an ini file named user.ini

; that looks like this (without the semicolon)



;dirnumber=1 9/8/2011 changed limit to 3 (three) due to disk being filled search for "if $dirno =" to change

; delete old TEMP filesI used /b and /i to check modified date only THE /a5 specifies files older than 5 days

; download decay here:

RunWait(@ComSpec & " /c " & 'c:decay.exe /a5 /b /d /i /n "C:Documents and SettingsJim MihalskiLocal SettingsTemp*.*"')

; Copy any *.bat files from root c: for backup

RunWait(@ComSpec & " /c " & 'C:\Windows\System32\XCOPY.EXE " C:*.bat "C:Documents and SettingsJim MihalskiMy Documentsdata" /c/m/y')

; Copy any *.txt files from root c: for backup

RunWait(@ComSpec & " /c " & 'C:\Windows\System32\XCOPY.EXE " C:*.txt "C:Documents and SettingsJim MihalskiMy Documentsdata" /c/m/y')

; Copy the MS word file (macros, etc) to the data folder for backup

RunWait(@ComSpec & " /c " & '"C:\Windows\System32\XCOPY.EXE "C:Documents and SettingsJim MihalskiApplication DataMicrosoftTemplatesNormal*.dot*" "C:Documents and SettingsJim MihalskiMy Documentsdata" /c/y"')

; following takes a screen snapshot and saves it as c:capture??.jpg

; so I can see what might have caused the daily backup

; not to run....

; delete any prior capture jpg files


; If printscreen was started before, close it

If ProcessExists("printscreen.exe") Then

RunWait("C:Program FilesPrintScreenPrintScreen.exe /exit")


; Do special printscreen with parameters to capture full screen and save as capture??.jpg

RunWait("C:Program FilesPrintScreenPrintScreen.exe /cptarea='2' /dir='c:' /image='1' /file='capture' /justnow")

; OK Now run regular printscreen

Run("C:Program FilesPrintScreenPrintScreen.exe /cptarea='3'")



; now check for any reminder or overdue Outlool windows

; if any exist close them before backup

Opt("WinWaitDelay", 100)

Opt("WinTitleMatchMode", 4)

$loop = 0

While $loop < 2

If WinExists("Overdue -") Then

If Not WinActive("Overdue -") Then WinActivate("Overdue -")




$loop = 1


If WinExists("Reminder -") Then

If Not WinActive("Reminder -") Then WinActivate("Reminder -")




$loop = 2



; next routine will compact Outlook pst file if Outlook is open then cloaes Outlook

; works for Outlook 2003

Opt("WinTitleMatchMode", 2)

If WinExists("Microsoft Outlook") Then

WinActivate("Microsoft Outlook", "")

WinWaitActive("Microsoft Outlook", "")


winWaitClose("Microsoft Outlook", "")



; will list all open windows to file c:openwins.txt

; used to determine why outlook.pst isn't being copied


$file = FileOpen("c:openwins.txt", 1)

; Check if file opened for writing OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")



$var = WinList()

For $i = 1 To $var[0][0]

; Only display visble windows that have a title

If $var[$i][0] <> "" And IsVisible($var[$i][1]) Then

FileWrite($file, "Title=" & $var[$i][0] & @CRLF)

; MsgBox(0, "Details", "Title=" & $var[$i][0] & @LF & "Handle=" & $var[$i][1])



Func IsVisible($handle)

If BitAND(WinGetState($handle), 2) Then

Return 1


Return 0


EndFunc ;==>IsVisible

$user = IniRead("c:user.ini", "settings", "username", "")

$dirno = IniRead("c:user.ini", "settings", "dirnumber", "")

$thedir = "I:backup" & $user & "" & $user & "-" & $dirno

; Once in four backups, do not exclude droid videos or my downloads - that will be backup dir #1

If $dirno = 1 Then

FileCopy("C:excludewovideos.txt", "C:exclude.txt", 1)


FileCopy("C:excludewithvideos.txt", "C:exclude.txt", 1)


RunWait(@ComSpec & " /c " & 'C:\Windows\System32\XCOPY.EXE "' & @MyDocumentsDir & '" "' & $thedir & '" /c/s/m/y/exclude:exclude.txt >c:backup.txt')

; /A Copies files with the archive attribute set, doesn't change the attribute.

; /M Copies files with the archive attribute set, turns off the archive attribute.

; /S Copies directories and subdirectories except empty ones.

; /C Continues copying even if errors occur.

; /Y Overwrites existing files without prompting.

; Use consolewrite to see how the command will look.. can even copy and paste the consolewrite output to a DOS window to see if it works.

;ConsoleWrite('C:\Windows\System32\XCOPY.EXE "' & @MyDocumentsDir & '" "' & $thedir & '" /c/s/a/exclude:exclude.txt >c:backup.txt' & @CRLF)


WinWaitActive("C:\Windows\System32\XXCOPY.EXE") ; this code is a work around for the netwrok restriction

Sleep(2000) ; two seconds

If WinExists("C:\Windows\System32\XXCOPY.EXE", "") Then

WinActivate("C:\Windows\System32\XXCOPY.EXE", "")



Sleep(2000) ; two seconds

If WinExists("C:\Windows\System32\XXCOPY.EXE", "") Then

WinActivate("C:\Windows\System32\XXCOPY.EXE", "")




;RunWait(@ComSpec & " /c " & "c:tm start /L >>c:backup.txt")

; MsgBox(0,"test","dir " & $thedir & " /S >>c:backup.txt")

;RunWait(@ComSpec & " /c " & "dir " & $thedir & " /S >>c:backup.txt")

;RunWait(@ComSpec & " /c " & "c:tm stop /L >>c:backup.txt")

$dirno = $dirno + 1

If $dirno = 4 Then $dirno = 1

IniWrite("c:user.ini", "settings", "dirnumber", $dirno)

; now write a record of the backup end time to the backup.txt file

$file = FileOpen("c:backup.txt", 1) ; (1) is write mode to append

; Check if file opened for writing OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")



FileWrite($file, @CRLF & "Backup destination " & $thedir & @CRLF & @CRLF)

FileWrite($file, "Backup Finished " & @MON & "/" & @MDAY & "/" & @YEAR & " -- " & @HOUR & ":" & @MIN & @CRLF)


Edited by speedi

Share this post

Link to post
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
Sign in to follow this  
Followers 0