Where I work we have 2 types of users, Redirects which are users that are on Desktops and their Desktop and My Documents are redirected to our File Print Server, and non-redirects usually laptop users where their profiles aren't redirected (obviously). There is a set of 2 radio buttons that allow you to select Redirect or Non-Redirect, the Redirect button only backs up the user's Favorites, their Outlook contacts, and any PST files it finds in their profile folder. Non-Redirect backs those up as well as their Desktop folder and My Documents.
This has been written to be usable under Windows XP and above and uses Xcopy to perform the copy. I used Xcopy to keep the code smaller as it does the copy process and will create the folders necessary without having to go through the trouble of using a recursive file routine and arrays. My philosophy is don't reinvent the wheel; when I have a tool that does what I need it to do, why remake one that does it slower?
The program also has the option of creating a log file of the files that were copied as well as any errors that were encountered. The location that this file is saved to is the folder you select in the Input field.
I don't know how useful this will be to everyone, but it has some interesting methods for doing what it does. Please let me know if you have any issues with it.
This program uses Melba23's ExtMsgBox UDF as well as a trick I found to color the Radio buttons that AdmiralAlkex posted HERE showing how to disable the Windows theme on a per control basis.
#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> #include <File.au3> #include <Array.au3> #include <ExtMsgBox.au3> #include <EditConstants.au3> Opt("GUIOnEventMode", 1) Opt("Mustdeclarevars", 1) Opt("GUICloseOnESC", 0) Global $Backup, $Restore, $SavePath, $CopyPath[5], $Search, $LogType, $I Global $X = 0, $NewOS, $UserName1, $Form2, $strPrompt1, $aUserList[10] Global $ArrayItem, $Log, $LogFlag, $LogFile, $LogPath, $UserList[10] Global $Icon, $Button, $Title, $Text, $Timeout, $Pos, $Vpos Global $strSavePath, $Redirect, $NonRedirect, $Loop, $Success = False Global $strLocation, $OK, $strPrompt, $Split, $sUserList[3] If @OSVersion == "WIN_7" Or @OSVersion == "WIN_VISTA" Or @OSVersion = "WIN_2008" Then $NewOS = True ElseIf @OSVersion == "WIN_XP" Then $NewOS = False Else _MsgBox($MB_ICONexclam, "&OK", "Wrong OS", "This Script only works on Windows XP and up, it can't be used on this version of Windows.", $Timeout, $Pos, $Vpos) Exit EndIf If Not $NewOS Then $UserList = _FileListToArray("C:\Documents and Settings", "*.*", 2) For $I = 1 To $UserList[0] Select Case $UserList[$I] = "All Users" Case $UserList[$I] = "Default User" Case $UserList[$I] = "NetworkService" Case $UserList[$I] = "LocalService" Case $UserList[$I] = "Administrator" Case Else $aUserList[$X] = $UserList[$I] $X += 1 If $X >= UBound($aUserList) Then ReDim $aUserList[UBound($aUserList) + 10] EndIf EndSelect Next Else $UserList = _FileListToArray("C:\Users", "*.*", 2) For $I = 1 To $UserList[0] Select Case $UserList[$I] = "All Users" Case $UserList[$I] = "Default" Case $UserList[$I] = "Default User" Case $UserList[$I] = "Public" Case Else $aUserList[$X] = $UserList[$I] $X += 1 If $X >= UBound($aUserList) Then ReDim $aUserList[UBound($aUserList) + 10] EndIf EndSelect Next EndIf ReDim $aUserList[$X] $sUserList = _ArrayToString($aUserList, "|") #Region ### START Koda GUI section ### Form=Form1 Global $Form1 = GUICreate("Backup and Restore", 629, 350, 314, 132) GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close") GUISetBkColor(0x4D689A, $Form1) ; Create Ok button GUICtrlCreateButton("&OK", 528, 70, 75, 25) GUICtrlSetState(-1, $GUI_FOCUS) GUICtrlSetFont(-1, 8, 400, 0, "Arial") GUICtrlSetOnEvent(-1, "OKButton1Click") ; Create Cancel button GUICtrlCreateButton("Cancel", 528, 150, 75, 25) GUICtrlSetFont(-1, 8, 400, 0, "Arial") GUICtrlSetOnEvent(-1, "CancelButton1Click") ;Create Help button 1 GUICtrlCreateButton("Help", 528, 230, 75, 25) GUICtrlSetFont(-1, 8, 400, 0, "Arial") GUICtrlSetOnEvent(-1, "HelpButton1Click") ; Create group control Global $Group1 = GUICtrlCreateGroup("", 32, 10, 473, 320) Global $Radio1 = GUICtrlCreateRadio("Restore?", 128, 30, 113, 17) DllCall("UxTheme.dll", "int", "SetWindowTheme", "hwnd", GUICtrlGetHandle($Radio1), "wstr", 0, "wstr", 0) GUICtrlSetColor(-1, 0xff8040) GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif") GUICtrlSetTip(-1, "Restore user files from backup?") Global $Radio2 = GUICtrlCreateRadio("Backup?", 312, 30, 91, 17) DllCall("UxTheme.dll", "int", "SetWindowTheme", "hwnd", GUICtrlGetHandle($Radio2), "wstr", 0, "wstr", 0) GUICtrlSetColor(-1, 0xff8040) GUICtrlSetState(-1, $GUI_CHECKED) GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif") GUICtrlSetTip(-1, "Backup user files?") Global $Input1 = GUICtrlCreateInput("", 80, 130, 321, 22) GUICtrlSetTip(-1, "Drive location") ; Create browse button GUICtrlCreateButton("Browse", 408, 130, 43, 17) GUICtrlSetTip(-1, "Browse for path") GUICtrlSetOnEvent(-1, "BrowseButtonclick") ; Create Label1 GUICtrlCreateLabel("Enter the location that your back ups are located or where you want to save them to.", 88, 80, 350, 33) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") GUICtrlSetColor(-1, 0xff8040) GUICtrlCreateLabel("Select the user to process", 88, 170, 350, 33) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") GUICtrlSetColor(-1, 0xff8040) Global $UserName = GUICtrlCreateCombo("", 80, 190, 177, 25) GUICtrlSetTip(-1, "Select User to back up/restore!") GUICtrlSetData(-1, $sUserList) Global $Log = GUICtrlCreateCheckbox("Do you want to keep a log of this?", 80, 300, 209, 17) DllCall("UxTheme.dll", "int", "SetWindowTheme", "hwnd", GUICtrlGetHandle($Log), "wstr", 0, "wstr", 0) GUICtrlSetColor(-1, 0xff8040) GUICtrlSetFont(-1, 8, 400, 0, "Arial") GUIStartGroup() Global $Radio3 = GUICtrlCreateRadio("Redirect?", 128, 250, 113, 17) DllCall("UxTheme.dll", "int", "SetWindowTheme", "hwnd", GUICtrlGetHandle($Radio3), "wstr", 0, "wstr", 0) GUICtrlSetColor(-1, 0xff8040) GUICtrlSetTip(-1, "Only back up PST files, Favorites and Outlook contacts") Global $Radio4 = GUICtrlCreateRadio("Non-Redirect?", 312, 250, 113, 17) DllCall("UxTheme.dll", "int", "SetWindowTheme", "hwnd", GUICtrlGetHandle($Radio4), "wstr", 0, "wstr", 0) GUICtrlSetColor(-1, 0xff8040) GUICtrlSetTip(-1, "Back up PST files, Favorites, Desktop, Documents, and Outlook contacts") GUICtrlSetState(-1, $GUI_CHECKED) GUICtrlCreateTabItem("") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### $strPrompt = "This script is used to backup and restore the standard user folders on a Windows computer to an external drive." & @CRLF $strPrompt &= "It will try to write to or read from the logged on user's network shared drive or any other valid drive" & @CRLF $strPrompt &= "letter including USB drives." & @CRLF & "It is compatible with Windows XP/Vista/Windows 7/Server 2003/Server 2008." $strPrompt &= @CRLF & "As the script is written, it will back up and restore the following folders." & @CRLF $strPrompt &= @CRLF & """My Documents"" or ""Documents""" & @CRLF & """Desktop""" & @CRLF & """Favorites""" & @CRLF $strPrompt &= "plus any ""*.pst"" files and Outlook contacts stored in the user's profile." & @CRLF $strPrompt &= @CRLF & @CRLF & "When selecting the location to restore from please select the folder that the back up folder is saved to, and not the folder with the user's name." $strPrompt &= @CRLF & @CRLF & "Example: Back ups are saved to the ""E:"" drive in the folder called ""Backups"", and the user's name is ""Jim"" (E:\Backups\Jim)" $strPrompt &= @CRLF & "Select the folder ""E:\Backups"" and not the ""E:\Backups\Jim"" folder." While 1 Sleep(100) WEnd #Region BUR Functions #cs ;=========================================================================== Checks the chosen path to see if it exists, and if it can be written to if backing up, or it checks to see that the restore folder is where you said it was. ;=========================================================================== #ce Func _CheckValidPaths() Local $TestFile, $Test If $Restore = 1 Then If Not FileExists($strLocation) Then GUISetState(@SW_HIDE) _MsgBox($MB_ICONexclam, "&OK", "Restore - Bad Path", "The path you selected " & "'" & $strLocation & "'" & " doesn't contain any back ups for the selected user, please try again", $Timeout, $Pos, $Vpos) GUISetState(@SW_SHOW) Return -1 EndIf Else $TestFile = $SavePath & "\testfile" $Test = FileOpen($TestFile, 8) FileClose($Test) If Not FileExists($SavePath) Then GUISetState(@SW_HIDE) _MsgBox($MB_ICONexclam, "&OK", "Backup - Bad Path", "The path you selected " & "'" & $SavePath & "'" & " is an invalid path, please try again", $Timeout, $Pos, $Vpos) GUISetState(@SW_SHOW, $Form1) Return -1 EndIf FileDelete($TestFile) EndIf EndFunc ;==>_CheckValidPaths #cs ;=========================================================================== _CopyFiles ;=========================================================================== #ce Func _CopyFiles(ByRef $strSearch, ByRef $ArrayItem) Local $strRunXcopy $strRunXcopy = " Xcopy.exe """ & $strSearch & """ """ & $SavePath _ & $CopyPath[$ArrayItem] & """ /y /s /h /i /c " If $LogFlag = 1 Then $strRunXcopy &= " >>" & Chr(34) & $LogPath & $LogType & Chr(34) & " 2>&1" EndIf RunWait(@ComSpec & " /c title " & $CopyPath[$ArrayItem] & "|" & $strRunXcopy, "", @SW_HIDE) EndFunc ;==>_CopyFiles #cs ;=========================================================================== ;=========================================================================== #ce Func _LogFile() $LogFile = FileOpen($LogPath & $LogType, 2) FileClose($LogFile) EndFunc ;==>_LogFile #cs ;=========================================================================== Initiates the browse for folder function and returns the path selected by user. ;=========================================================================== #ce Func BrowseButtonclick() $strSavePath = FileSelectFolder("Select a drive letter or folder, don't select My Computer", _ "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", 7) GUICtrlSetData($Input1, $strSavePath) EndFunc ;==>BrowseButtonclick #cs ;=========================================================================== Cancel button exits the script ;=========================================================================== #ce Func CancelButton1Click() Exit EndFunc ;==>CancelButton1Click #cs ;=========================================================================== Calls the Help message box ;=========================================================================== #ce Func HelpButton1Click() GUISetState(@SW_HIDE) $Icon = $MB_ICONINFO $Button = "&OK" $Title = "Help" $Text = $strPrompt Opt("MustDeclareVars", 0) _MsgBox($Icon, $Button, $Title, $Text, $Timeout, $Pos, $Vpos) GUISetState(@SW_SHOW, $Form1) Opt("MustDeclareVars", 1) EndFunc ;==>HelpButton1Click #cs ;=========================================================================== ;=========================================================================== #ce ; ; Func OKButton1Click() Local $Test, $SavedTo ; You pressed the OK button, now we are going to read your settings $Backup = GUICtrlRead($Radio2) $Restore = GUICtrlRead($Radio1) $SavePath = GUICtrlRead($Input1) $LogFlag = GUICtrlRead($Log) $UserName1 = GUICtrlRead($UserName) $Redirect = GUICtrlRead($Radio3) $NonRedirect = GUICtrlRead($Radio4) If $UserName1 <> "0" Then Local $Split[3], $OK $OK = False $Split = StringSplit($sUserList, "|") For $I In $Split If $UserName1 = $I Then $OK = True Next If Not $OK Then $Icon = $MB_ICONexclam $Button = "OK" $Title = "No user name selected" $Text = "You need to select the user before starting the process. Please try again" $Pos = $Form1 _MsgBox($Icon, $Button, $Title, $Text, $Timeout, $Pos, $Vpos) Return EndIf Else Return EndIf Select Case StringLen($UserName1) = 0 $Icon = $MB_ICONexclam $Button = "OK" $Title = "No user name selected" $Text = "You need to select the user before starting the process. Please try again" $Pos = $Form1 _MsgBox($Icon, $Button, $Title, $Text, $Timeout, $Pos, $Vpos) Return Case $SavePath = "" ; No path selected GUISetState(@SW_HIDE) $Icon = $MB_ICONINFO $Button = "&OK" $Title = "No drive selected" $Text = "You need to put in the save/restore path" _MsgBox($Icon, $Button, $Title, $Text, $Timeout, $Pos, $Vpos) GUICtrlSetData($UserName, $sUserList) GUISetState(@SW_SHOW) Return Case StringLeft($SavePath, 1) = "C" $Icon = $MB_ICONexclam $Button = "OK" $Title = "Don't use C: drive" $Text = "You can't back up the files to the same drive you're copying from, ple" & _ "ase don't select the C: drive. Please try again" $Pos = $Form1 _MsgBox($Icon, $Button, $Title, $Text, $Timeout, $Pos, $Vpos) Return Case StringLen($SavePath) = 1 ; Just drive letter entered? $SavePath &= ":\" Case StringRight($SavePath, 1) <> "\" $SavePath &= "\" EndSelect $SavePath &= $UserName1 ;=========================================================================== ; Now that we know where you're saving the data to, we need to set some ; variables to tell it what folders/files to back up ;=========================================================================== If $Backup = 1 Then If $NewOS Then $strLocation = "C:\Users\" & $UserName1 DirCreate($SavePath) $LogPath = $SavePath $LogType = "\Backup.log" Else $strLocation = "C:\Documents and Settings\" & $UserName1 DirCreate($SavePath) $LogPath = $SavePath $LogType = "\Backup.log" EndIf Else If $NewOS Then $strLocation = $SavePath $SavePath = "C:\Users\" & $UserName1 $LogPath = $strLocation $LogType = "\Restore.log" Else $strLocation = $SavePath $SavePath = "C:\Documents and Settings\" & $UserName1 $LogPath = $strLocation $LogType = "\Restore.log" EndIf EndIf If Not $NewOS Then $CopyPath[0] = "\Favorites" $CopyPath[1] = "\Application Data\Microsoft\Outlook" $CopyPath[2] = "\*.pst" $CopyPath[3] = "\My Documents" $CopyPath[4] = "\Desktop" Else $CopyPath[0] = "\Favorites" $CopyPath[1] = "\AppData\Roaming\Microsoft\Outlook" $CopyPath[2] = "\*.pst" $CopyPath[3] = "\Documents" $CopyPath[4] = "\Desktop" EndIf $Loop = 4 If $Redirect = 1 Then $Loop = 2 GUISetState(@SW_HIDE) $Form2 = GUICreate("Copying!", 300, ($Loop + 1) * 50) GUISetBkColor(0x0008000, $Form2) GUISetState() For $I = 0 To $Loop GUICtrlCreateLabel(StringMid($CopyPath[$I], 2), 50, 20 + $I * 30, 230, 20) GUICtrlSetFont(-1, Default, 800, Default) GUICtrlSetColor(-1, 0x0FFFFFF) Next $Test = _CheckValidPaths() If $Test = -1 Then Return If $LogFlag = $GUI_CHECKED Then _LogFile() For $ArrayItem = 0 To $Loop $Search = $strLocation & $CopyPath[$ArrayItem] _CopyFiles($Search, $ArrayItem) $Search = "" GUICtrlCreateLabel(Chr(214), 25, 20 + $ArrayItem * 30, 10, 10) GUICtrlSetFont(-1, Default, 800, Default, "Symbol") GUICtrlSetColor(-1, 0x0FFFFFF) GUICtrlCreateLabel(StringMid($CopyPath[$ArrayItem], 2), 50, 20 + $ArrayItem * 30, 230, 20) GUICtrlSetFont(-1, Default, 800, 8) GUICtrlSetColor(-1, 0x0FFFFFF) Next Sleep(2000) GUISetState(@SW_HIDE) If $Backup = 1 Then $SavedTo = "Your files were saved to " & $LogPath Else $SavedTo = "You have restored the files for " & $UserName1 EndIf _MsgBox($MB_ICONexclam, "&OK", "Done!", "We're done!" & @CRLF & @CRLF & $SavedTo, $Timeout, $Form2, $Vpos) GUISetState(@SW_SHOW, $Form1) EndFunc ;==>OKButton1Click #EndRegion BUR Functions #cs ;=========================================================================== This calls the Extended Message Box functions written by Melba23 and added as an #include ;=========================================================================== #ce Func _MsgBox($Icon, $Button, $Title, $Text, $iTimeout = 0, $hWin = "", $iVPos = 0) _ExtMsgBoxSet(1, 0, 0x02616FF, 0x02AFF6A, 10, "Consolas") _ExtMsgBox($Icon, $Button, $Title, $Text, $iTimeout, $hWin, $iVPos) EndFunc ;==>_MsgBox Func Form1Close() Exit EndFunc ;==>Form1Close





