Batch Script Help Pls.............

Discussion in 'Scripting' started by ramg1967, Mar 28, 2015.

  1. ramg1967

    ramg1967 MDL Member

    Sep 13, 2012
    237
    86
    10
    Hello All,


    We are replacing Lenovo hardware with Dell. In the process of upgrading 150 users. I would like to use a script that can backup user profile data to USB stick and then use the script to restore backed up data from USB stick onto Dell machine.

    This is what my users will do.

    1. Use the script to backup their data to USB stick
    2. After they get new Dell machine they will run the script to restore the files from the USB stick

    All my users are currently on Lenovo T520 running Windows 7 Entp.

    I am seeking help to create a script because, I do not have any scripting skills/knowledlge to complete above task. Appreciate, if any one can help me here.

    When user run the script it should prompt for USERID, so that only data pertaining to the USERID are backed up. I don't want any other users data backed up.


    Following are the folders/files that the script should backup to USB stick.


    c:\users\xxxxxxx\contacts
    c:\users\xxxxxxx\desktop
    c:\users\xxxxxxx\documents
    c:\users\xxxxxxx\downloads
    c:\users\xxxxxxx\favorities
    c:\users\xxxxxxx\links
    c:\users\xxxxxxx\music
    c:\users\xxxxxxx\pictures
    C:\users\xxxxxxx\appdata\roaming\microsoft\signatu res
    C:\users\xxxxxxx\appdata\roaming\microsoft\sticky notes
    C:\users\xxxxxxx\appdata\local\microsoft\outlook - (I just want only the PST files)


    Is it possible to help me in creating a script to backup and restore the data.


    All my users have 64GB USB stick. They will use USB stick to backup the data using the script and use the script to restore (from the USB stick back to c:\users) after, I give them DELL machine with Windows 7 and all apps installed.


    Thanks and look forward to your help.


    Ram

    P.S: I posted above request under Window 7 category. After posting this request, I did find a PS script that, I am tweaking to get the script work.

    If you have any other solution on how to get the script ready, please do share. I will study the script, understand and see how to tweak and make it work.

     
  2. taviruni

    taviruni MDL Senior Member

    May 8, 2010
    261
    248
    10
    #2 taviruni, Mar 28, 2015
    Last edited: Mar 28, 2015
  3. ramg1967

    ramg1967 MDL Member

    Sep 13, 2012
    237
    86
    10
    Thanks taviruni. I wish the GUI had an option to add additional folders and files, it would have been great. Any way, I will give it a try and also in the process of getting a script ready.
     
  4. Soulfate

    Soulfate MDL Member

    Feb 14, 2011
    185
    51
    10
    #4 Soulfate, Mar 28, 2015
    Last edited by a moderator: Apr 20, 2017
    Hé hop !
    Don't forget to change the "set BackupFolder=" (the fourth line). See the memo (REM) in the code to learn a bit more.
    Example of a valid backup folder (no double quotes, do not end with "\" character): D:\Backup
    Code:
    @echo off
    
    REM Add a backup folder here to auto-backup your files into this folder:
    set BackupFolder=
    
    if not defined BackupFolder (
    echo Enter you backup folder ^(without double quotes^):
    set /p BackupFolder= Example: D:\Backup -^> 
    )
    if exist "%BackupFolder%\" (
    echo It seems that a backup already exists ^(at "%BackupFolder%\"^).
    echo Do you want to restore this backup ^(Y for Yes, N for No and backup your files^) ?
    set /p BackupChoice= -^> 
    )
    if /i "%BackupChoice%"=="y" (call :restore) else (call :backup)
    echo.
    echo Everything is done.
    pause
    exit /b
    
    
    :backup
    echo Backing up of your files...
    xcopy /cheriky /g "%USERPROFILE%\contacts" "%BackupFolder%\contacts"
    xcopy /cheriky /g "%USERPROFILE%\desktop" "%BackupFolder%\desktop"
    xcopy /cheriky /g "%USERPROFILE%\documents" "%BackupFolder%\documents"
    xcopy /cheriky /g "%USERPROFILE%\downloads" "%BackupFolder%\downloads"
    xcopy /cheriky /g "%USERPROFILE%\favorites" "%BackupFolder%\favorites"
    xcopy /cheriky /g "%USERPROFILE%\links" "%BackupFolder%\links"
    xcopy /cheriky /g "%USERPROFILE%\music" "%BackupFolder%\music"
    xcopy /cheriky /g "%USERPROFILE%\pictures" "%BackupFolder%\pictures"
    xcopy /cheriky /g "%APPDATA%\microsoft\signatures" "%BackupFolder%\APPDATA\microsoft\signatures"
    xcopy /cheriky /g "%APPDATA%\microsoft\sticky notes" "%BackupFolder%\APPDATA\microsoft\sticky notes"
    for /r "%LOCALAPPDATA%\microsoft\outlook" %%a in (*.pst) do xcopy /cheriky /g "%%a" "%BackupFolder%\LOCALAPPDATA\microsoft\outlook\%%~nxa"
    echo Backup done.
    exit /b
    
    :restore
    echo Restoring up of your files...
    xcopy /cheriky /g "%BackupFolder%\contacts" "%USERPROFILE%\contacts"
    xcopy /cheriky /g "%BackupFolder%\desktop" "%USERPROFILE%\desktop"
    xcopy /cheriky /g "%BackupFolder%\documents" "%USERPROFILE%\documents"
    xcopy /cheriky /g "%BackupFolder%\downloads" "%USERPROFILE%\downloads"
    xcopy /cheriky /g "%BackupFolder%\favorites" "%USERPROFILE%\favorites"
    xcopy /cheriky /g "%BackupFolder%\links" "%USERPROFILE%\links"
    xcopy /cheriky /g "%BackupFolder%\music" "%USERPROFILE%\music"
    xcopy /cheriky /g "%BackupFolder%\pictures" "%USERPROFILE%\pictures"
    xcopy /cheriky /g "%BackupFolder%\APPDATA\microsoft\signatures" "%APPDATA%\microsoft\signatures"
    xcopy /cheriky /g "%BackupFolder%\APPDATA\microsoft\sticky notes" "%APPDATA%\microsoft\sticky notes"
    xcopy /cheriky /g "%BackupFolder%\LOCALAPPDATA\microsoft\outlook" "%LOCALAPPDATA%\microsoft\outlook"
    echo Restoration done.
    exit /b
    (don't forget to rename Backup_restore.cmd.txt to Backup_restore.cmd)
     

    Attached Files:

  5. ramg1967

    ramg1967 MDL Member

    Sep 13, 2012
    237
    86
    10
    #5 ramg1967, Mar 28, 2015
    Last edited: Mar 28, 2015
    (OP)
    Thanks a lot Soulfate. I will make necessary change , test the script at work on Monday and post the outcome here.

    By the way, I will change the backup folder to F because we have C for Operating System and D for DVD Drive. If, I make this particular change and save the script - it should work - right??
    set /p BackupFolder=F:\Backup -^>

    I believe cheriky is an example of USERID. If, I understand correctly the script will take current logged in USERID and perform the backup - right??

    Looked at the meaning of REM - The REM command lets you place a remark or comment in a batch file. Batch file comments are useful for documenting the purpose of a batch file and the procedures you have used.
     
  6. Soulfate

    Soulfate MDL Member

    Feb 14, 2011
    185
    51
    10
    #6 Soulfate, Mar 29, 2015
    Last edited by a moderator: Apr 20, 2017
    The "D:\Backup" line in my script is a textual example (for the form) that help the user to type a good path. You can use this script as it is and enter in the command prompt F:\Backup when it is asked (there may be 2 questions, it's the first question asked by the script). The MEM part is if you want that the script set automatically the backup folder without intervention of the user, which may save a lot of your time. In this case, the backup is automatic and the script only will ask if you want to restore backup, IF IT ALREADY EXISTS.

    If you want to automatically set the backup folder to F:\Backup, change the following line:
    Code:
    set BackupFolder=
    to
    Code:
    set BackupFolder=F:\Backup
    You have to run this script with a logged user, maybe with administrator rights, but you can test without. It could work with simple user privileges (not sure for the APPDATA part).

    /cheriky /g are arguments for the xcopy command, it is NOT an USERID and you don't have to change it. The USERID is %USERPROFILE% (a environnement variable), same for %APPDATA% and %LOCALAPPDATA%.
    Type set in cmd and you will discover other variables like them.


    EDIT:
    I've made a stupid mistake in the script that I've just fixed (the restoration of outlook files). Please actual it if you have already copied it.
     
  7. Chibi ANUBIS

    Chibi ANUBIS MDL Chibi Developer

    Apr 28, 2014
    1,237
    911
    60
    Nice job Soulfate :thumbsup:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. ramg1967

    ramg1967 MDL Member

    Sep 13, 2012
    237
    86
    10
    I will take the updated copy of your script and test at work Monday. Will keep youu posted.

    Once again thanks for all your help.
     
  9. Soulfate

    Soulfate MDL Member

    Feb 14, 2011
    185
    51
    10
    I'm waiting for feedback. I've already tested on my computer some folders to backup (my desktop, an AppData folder) and it worked. I hope it will work for you to.
    Note that this script is compatible with Vista, Windows 7, Windows 8/8.1 and Windows 10. Some folders don't exist anymore on Windows 8 and newer (Sticky Notes, maybe Signatures).

    It's a pleasure for me :) I know nothing else than MS BATCH, but I like to create some tools with it (portable games, portable programs, auto-update of some tools as AdwCleaner, script to convert some file into another, like WIM to ESD, ESD to WIM, etc.).
    My computer is full of automated scripts, it's a Giant Clock :biggrin:
     
  10. Compo

    Compo MDL Member

    Mar 29, 2011
    136
    106
    10
    #10 Compo, Mar 29, 2015
    Last edited by a moderator: Apr 20, 2017
    Just to let you know that there is no safe way to copy your PST file if it is open, (probably not even with shadow copy), so any script should ensure that Outlook is closed before copying begins.

    @Soulfate - Some comments:
    There is no need to escape the parentheses in the line above.
    Code:
    Echo;Type the name of your backup folder (without double quotes):
    Two problems with this code above:
    1. If you are checking the existence of a folder/directory then you should add a backslash
      Code:
      If Exist "%BackupFolder%\" (
    2. You shouldn't be checking for the existence of the folder, you should checking its contents.

    Also:
    Based upon the OS stated, why not use Choice?
    Code:
    Choice/M "Do you want to restore this backup (Press Y for Yes, N for No.)"
     
  11. Soulfate

    Soulfate MDL Member

    Feb 14, 2011
    185
    51
    10
    #11 Soulfate, Mar 29, 2015
    Last edited by a moderator: Apr 20, 2017
    Hi Compo :)

    For
    Code:
    echo Enter you backup folder ^(without double quotes^):
    , it could cause some problems if you want to use this ECHO into a FOR command (that happened to me some times). So it's not needed in this actual case, but it's prevention for potential modifications. And when it's textual, I prefer to automatically prevent bugs caused by symbols.

    Code:
    If Exist "%BackupFolder%\" (
    I'm ok with the backslash ;) I will edit my code. But with some commands (I can't remember which exactly...), using a backslash can cause errors. It's maybe the case of COPY or XCOPY.

    Do you really think that checking the content of the backup folder is needed (and reliable) ? There are only simple XCOPY operations and they don't matter if the backup folder is empty or not. Plus, I could use a more complex code to check if there is at least 1 file in this backup folder, but should we trust that more complex code ? Checking a simple folder is reliable enough to be trust.

    I'm ok with the CHOICE command suggestion. But CHOICE is an external command and not included into native commands. I cannot explain exactly why I prefered to use SET /P instead of CHOICE, maybe less (external) dependencies ? The result, for this case, is the same, so it's good enough :)
    I cannot invoque the "Windows XP" reason because I don't support it anymore and it is clearly NOT our case.


    Thanks for your coms ;) I enjoy sharing knowledges :tea:
     
  12. ramg1967

    ramg1967 MDL Member

    Sep 13, 2012
    237
    86
    10
    #12 ramg1967, Mar 29, 2015
    Last edited: Mar 29, 2015
    (OP)
    Thanks Compo and Soulfate. We are currently on Windows 7 so this script should work fine without any issue with regard to Folders/Files.

    By the way when users backup there is no backup folder existence on the flash drive. So, as pointed by Soulfate there is no need to check the content of backup folder.

    With regard to outlook - I have already instructed USERS to close all programs, IE and anything they are working before starting the backup. So outlook pst backyp should be fine.

    I will post my feedback tomrorow evening EST.

    Soulfate - I have saved your final edited script as userbackup.bat. Is that fine with you.

    Thanks to all of you for sharing the knowledge and helping me through this.
     
  13. Compo

    Compo MDL Member

    Mar 29, 2011
    136
    106
    10
    Sorry, but you should assume that your end user is incapable of following that instruction.
    You should at the very least specifically tell them in the script to close Outlook, or better still check if it's running, and if so, ask them to close it or warn them that you're going to close it programmatically.
    It's not really responsible to use 'I told you so' to any personnel who have lost data, especially when it's a simple thing to add to the script.
     
  14. Soulfate

    Soulfate MDL Member

    Feb 14, 2011
    185
    51
    10
    #14 Soulfate, Mar 30, 2015
    Last edited by a moderator: Apr 20, 2017
    Here is a batch code to detect, warn the user and close an opened program :
    Code:
    REM Enter the name of the program (without extension) you want to detect and close before the backup process here:
    set ProgramToBeClosed=
    tasklist | find /i "%ProgramToBeClosed%" >nul &&(
    echo An instance of %ProgramToBeClosed% has been detected.
    echo Please close any instance of %ProgramToBeClosed% before the backup process.
    echo If you push on any keyboard button to continue, %ProgramToBeClosed% will be closed automatically.
    pause
    taskkill /F /IM "%ProgramToBeClosed%.exe" /T
    )
    Change set ProgramToBeClosed= to set ProgramToBeClosed=THE_REAL_NAME_OF_YOUR_PROGRAM (if it's Outlook.exe, just write Outlook).

    You can insert this code just after the first line (@echo off).
     
  15. ramg1967

    ramg1967 MDL Member

    Sep 13, 2012
    237
    86
    10
    You are amazing. I have included above code into the script to close outlook and have saved the script as .cmd Going to test this today at work and report the feedback in the evening PM.

    Thanks for all your effort.

    Ram
     
  16. ramg1967

    ramg1967 MDL Member

    Sep 13, 2012
    237
    86
    10
    Soulfate - The script worked out the way it should. The backup was successful and the script did report anything that was not found. On the test machine sticy notes was not present and the script reported as NOT FOUND. Goood Job batch script....

    I did include few other folders within the script to backup (such as OneNote from AppData\Local\Microsoft\OneNote). The script took care of it. This script will come very handy for me during hardware upgrade process. I will create a short document for the users to follow and complete the backup and restore.

    Thanks a lot.

    Ram
     
  17. Soulfate

    Soulfate MDL Member

    Feb 14, 2011
    185
    51
    10
    A lot of commands check if the action is possible, before doing it. If not, then they send an error message and the script can continue. That's what actually happened.

    If you're interested, I can write something to output errors and success into physical log files. That could be handy for reading results later.
    I could rewrite the restoration process to have a script that doesn't ask anything to the user, so backup and restoration shouldn't need any intervention. That could save you the time and the risk to explain to users how to use this tool, and with log files, that should be more appropriate for an "massive" maintenance ;)
     
  18. ramg1967

    ramg1967 MDL Member

    Sep 13, 2012
    237
    86
    10
    #18 ramg1967, Mar 31, 2015
    Last edited: Apr 1, 2015
    (OP)
    I would appreciate, if you can write a log files for the action performed and the backup/restoration fully automated. It is not required immediately. For now, I am imaging 150 laptops using SCCM. It will take 5-10 days to complete.
     
  19. Soulfate

    Soulfate MDL Member

    Feb 14, 2011
    185
    51
    10
    #19 Soulfate, Apr 2, 2015
    Last edited by a moderator: Apr 20, 2017
    Ok. With the help of a jar of Nutella and some small bread sticks, I coded all this (including the part to close programs):
    Code:
    @echo off
    
    REM Add a backup folder here to auto-backup your files into this folder, so this script doesn't ask the user to enter a path:
    set BackupFolder=F:\Backup
    REM Here is the file from where this script will read source folders, backup folders and extension to filter:
    set BackupList=%~dp0Backup_list.txt
    REM Here is the log file where some outputs will be written:
    set LogFile=%~dpn0.log
    REM Here is the max size in Bytes that the backup should be. If the calculated backup size is over this limit, this script will warn the user. Max Integer is 2147483647999:
    set MaxSizeB=60000000000
    REM Enter the name of the programs you want to detect and close before the backup process. The symbol "semicolon" (symbol ;) allows to separate several programs:
    set ProgramToBeClosed="Outlook"
    
    REM Run this script with -restore or -backup arguments to force a backup or a restoration.
    if /i "%~1"=="-restore" set BackupChoice=y
    if /i "%~1"=="-backup" set BackupChoice=n
    REM You can automatically force a restoration over a backup, if a backup folder exists, by adding this line: set BackupChoice=y
    REM You can automatically force a backup, even if a backup folder already exists, by adding this line: set BackupChoice=n
    
    echo %DATE% - %TIME%
    echo.
    echo ################ BACKUP - RESTORE ################>>"%LogFile%"
    echo PLEASE READ THIS FILE WITH OEM 850 ENCODAGE!>>"%LogFile%"
    echo %DATE% - %TIME%>>"%LogFile%"
    echo.>>"%LogFile%"
    
    for %%a in (%ProgramToBeClosed%) do (
    tasklist | find /i "%%~na" >nul &&(
    echo An instance of %%~na has been detected.
    echo Please close any instance of %%~na before the backup process.
    echo If you push on any keyboard button to continue, %%~na will be closed automatically.
    if not defined BackupChoice pause
    echo.
    echo Trying to close %%~na...>>"%LogFile%"
    taskkill /F /IM "%%~na.exe" /T>>"%LogFile%" 2>&1||(
    echo TaskKill wasn't able to close %%~na. Please close it yourself before continuing.
    if not defined BackupChoice pause
    )
    )
    )
    
    if not defined BackupFolder (
    echo Enter you backup folder ^(without double quotes^):
    set /p BackupFolder= Example: F:\Backup -^> 
    )
    if not defined BackupChoice if exist "%BackupFolder%\" (
    echo It seems that a backup already exists ^(at "%BackupFolder%\"^).
    echo Do you want to restore this backup ^(Y for Yes, N for No and backup your files^) ?
    set /p BackupChoice= -^> 
    )
    
    if /i "%BackupChoice%"=="y" (call :restore) else (call :backup)
    
    :EndOfScript
    endlocal
    set BackupChoice=
    echo Everything is done. End of script.
    echo Everything is done. End of script.>>"%LogFile%"
    echo.>>"%LogFile%"
    pause
    exit /b
    
    
    :backup
    setlocal enabledelayedexpansion
    for /f "usebackq eol=; tokens=1,2,* delims=:" %%f in ("%BackupList%") do (
    call :size_calculation "%%~f" %%h
    )
    
    set TotalSizeB=%TotalSizeB:~-3%
    if "%TotalSizeB:~1%"=="" (set TotalSizeB=00%TotalSizeB%) else (if "%TotalSizeB:~2%"=="" set TotalSizeB=0%TotalSizeB%)
    set TotalSizeB=%TotalSizeKB%%TotalSizeB%
    echo Total size of backup: %TotalSizeB%/%MaxSizeB% Bytes
    echo.
    echo Total size of backup: %TotalSizeB%/%MaxSizeB% Bytes>>"%LogFile%"
    echo.>>"%LogFile%"
    set RestSizeB=%TotalSizeB:~-3%
    if "%RestSizeB:~0,1%"=="0" set RestSizeB=%RestSizeB:~1%
    if "%RestSizeB:~0,1%"=="0" set RestSizeB=%RestSizeB:~1%
    set RestMaxSizeB=%MaxSizeB:~-3%
    if "%RestMaxSizeB:~0,1%"=="0" set RestMaxSizeB=%RestMaxSizeB:~1%
    if "%RestMaxSizeB:~0,1%"=="0" set RestMaxSizeB=%RestMaxSizeB:~1%
    set MaxSizeChoice=y
    if %TotalSizeKB% EQU %MaxSizeB:~0,-3% if %RestSizeB% GEQ %RestMaxSizeB% call :max_size_warn
    if %TotalSizeKB% GTR %MaxSizeB:~0,-3% call :max_size_warn
    if /i not "%MaxSizeChoice%"=="y" exit /b
    
    echo Backing up of your files...
    echo.
    echo Backing up of your files...>>"%LogFile%"
    echo.>>"%LogFile%"
    
    for /f "usebackq eol=; tokens=1,2,* delims=:" %%f in ("%BackupList%") do (
    call :backup_xcopy "%%~f" "%%~g" %%h
    )
    
    echo Backup done.
    echo.
    echo Backup done.>>"%LogFile%"
    echo.>>"%LogFile%"
    exit /b
    
    :restore
    echo Restoring up of your files...
    echo.
    echo Restoring up of your files...>>"%LogFile%"
    echo.>>"%LogFile%"
    
    for /f "usebackq eol=; tokens=1,2 delims=:" %%f in ("%BackupList%") do (
    call :restore_xcopy "%%~f" "%%~g"
    )
    
    echo Restoration done.
    echo.
    echo Restoration done.>>"%LogFile%"
    echo.>>"%LogFile%"
    exit /b
    
    :size_calculation
    if not defined TotalSizeKB set /a TotalSizeKB=0
    if not defined TotalSizeB set /a TotalSizeB=0
    set /a FolderSizeKB=0
    set /a FolderSizeB=0
    if not "%~2"=="" (
    for /r "%~1" %%j in (*%~2) do (
    set FileSize=%%~zj
    if "!FileSize:~0,-3!"=="" (set /a FolderSizeB=!FolderSizeB!+!FileSize:~-3!) else (
    set /a FolderSizeKB=!FolderSizeKB!+!FileSize:~0,-3!
    set RestSizeB=!FileSize:~-3!
    if "!RestSizeB:~0,1!"=="0" set RestSizeB=!RestSizeB:~1!
    if "!RestSizeB:~0,1!"=="0" set RestSizeB=!RestSizeB:~1!
    set /a FolderSizeB=!FolderSizeB!+!RestSizeB!
    )
    )
    for /f "delims=" %%j in ('dir /b /s /a:-d /a:h "%~1"') do if "%%~xj"=="%~2" (
    set FileSize=%%~zj
    if "!FileSize:~0,-3!"=="" (set /a FolderSizeB=!FolderSizeB!+!FileSize:~-3!) else (
    set /a FolderSizeKB=!FolderSizeKB!+!FileSize:~0,-3!
    set RestSizeB=!FileSize:~-3!
    if "!RestSizeB:~0,1!"=="0" set RestSizeB=!RestSizeB:~1!
    if "!RestSizeB:~0,1!"=="0" set RestSizeB=!RestSizeB:~1!
    set /a FolderSizeB=!FolderSizeB!+!RestSizeB!
    )
    )
    echo Size of "%~1\*%~2": !FolderSizeKB! KiloBytes
    ) else (
    for /r "%~1" %%j in (*) do (
    set FileSize=%%~zj
    if "!FileSize:~0,-3!"=="" (set /a FolderSizeB=!FolderSizeB!+!FileSize:~-3!) else (
    set /a FolderSizeKB=!FolderSizeKB!+!FileSize:~0,-3!
    set RestSizeB=!FileSize:~-3!
    if "!RestSizeB:~0,1!"=="0" set RestSizeB=!RestSizeB:~1!
    if "!RestSizeB:~0,1!"=="0" set RestSizeB=!RestSizeB:~1!
    set /a FolderSizeB=!FolderSizeB!+!RestSizeB!
    )
    )
    for /f "delims=" %%j in ('dir /b /s /a:-d /a:h "%~1"') do (
    set FileSize=%%~zj
    if "!FileSize:~0,-3!"=="" (set /a FolderSizeB=!FolderSizeB!+!FileSize:~-3!) else (
    set /a FolderSizeKB=!FolderSizeKB!+!FileSize:~0,-3!
    set RestSizeB=!FileSize:~-3!
    if "!RestSizeB:~0,1!"=="0" set RestSizeB=!RestSizeB:~1!
    if "!RestSizeB:~0,1!"=="0" set RestSizeB=!RestSizeB:~1!
    set /a FolderSizeB=!FolderSizeB!+!RestSizeB!
    )
    )
    echo Size of "%~1": !FolderSizeKB! KiloBytes
    )
    set /a TotalSizeKB=%TotalSizeKB%+%FolderSizeKB%
    if not "%FolderSizeB:~0,-3%"=="" set /a TotalSizeKB=%TotalSizeKB%+%FolderSizeB:~0,-3%
    set /a TotalSizeB=%TotalSizeB%+%FolderSizeB%
    exit /b
    
    :backup_xcopy
    echo - Backup of %1 -^> %2, %3
    echo - Backup of %1 -^> %2, %3:>>"%LogFile%"
    if not "%~3"=="" (
    for /f "delims=" %%j in ('dir /b /s /a:-d "%~1"') do if "%%~xj"=="%~3" (
    set TempBackupFile=%%~dpj
    call set TempBackupFile=!TempBackupFile:%~1=%~2!
    xcopy /cheriky /g "%%~j*" "!TempBackupFile!">>"%LogFile%" 2>&1
    set TempBackupFile=
    )
    ) else (
    xcopy /cheriky /g "%~1" "%~2">>"%LogFile%" 2>&1
    )
    echo.
    echo.>>"%LogFile%"
    exit /b
    
    :restore_xcopy
    echo - Restoration of %2 -^> %1
    echo - Restoration of %2 -^> %1:>>"%LogFile%"
    xcopy /cheriky /g "%~2" "%~1">>"%LogFile%" 2>&1
    echo.
    echo.>>"%LogFile%"
    exit /b
    
    :max_size_warn
    echo Attention. The total size of files to backup exceeds the maximal size.
    echo It would be possible that the backup folder have NOT enough space to allow a total copy.
    echo Please contact your administrator before continuing.
    echo Would you like to continue ^(Y/N^)?
    echo Attention. The total size of files to backup exceeds the maximal size.>>"%LogFile%"
    set /p MaxSizeChoice= -^> 
    if /i "%MaxSizeChoice%"=="y" (
    echo Script will continue...
    echo.
    echo Script will continue...>>"%LogFile%"
    echo.>>"%LogFile%"
    ) else (
    echo Script will NOT continue...
    echo.
    echo Script will NOT continue...>>"%LogFile%"
    echo.>>"%LogFile%"
    )
    exit /b
    All variables that you could edit are the first lines. MEM lines will help you to know exactly what to write, but I've already set everything to fit to your needs. If you want to use accentuated characters (é, à, ë, etc.), please edit this file with OEM 850 to get corrects characters (command prompt use OEM 850 encoding). Note that %~dp0 means "script dir"\.

    All folders to copy have to be into a separated TXT file ("script dir"\Backup_list.txt, but can be modified with variables):
    Code:
    ;Important! Please use OEM 850 encoding when you edit this file.
    ;Backup folder variable is %BackupFolder%.
    ;Source folder (without double quote):Backup folder (without double quote):file extension filter (.ext)
    ;
    %USERPROFILE%\contacts:%BackupFolder%\contacts
    %USERPROFILE%\desktop:%BackupFolder%\desktop
    %USERPROFILE%\documents:%BackupFolder%\documents
    %USERPROFILE%\downloads:%BackupFolder%\downloads
    %USERPROFILE%\favorites:%BackupFolder%\favorites
    %USERPROFILE%\links:%BackupFolder%\links
    %USERPROFILE%\music:%BackupFolder%\music
    %USERPROFILE%\pictures:%BackupFolder%\pictures
    %APPDATA%\microsoft\signatures:%BackupFolder%\AppData\Roaming\microsoft\signatures
    %APPDATA%\microsoft\sticky notes:%BackupFolder%\AppData\Roaming\microsoft\sticky notes
    %LOCALAPPDATA%\microsoft\OneNote:%BackupFolder%\AppData\Local\microsoft\OneNote
    %LOCALAPPDATA%\microsoft\outlook:%BackupFolder%\AppData\Local\microsoft\outlook:.pst
    Lines beginning with ";" symbol are skipped. It's like REM in a BATCH file. You will also need to edit this file in OEM 850 if you want to use accentuated characters. First lines explain how to enter correct folder lines, but I've already wrote what you need.

    It's really important to know that there is a folder size calculation system. If the total is over than the max defined size (in this case, the MaxSizeB variable is set to 60000000000 Bytes), a prompt will warn the user and ask him/her if he/she want to continue to backup.

    If you want run a forced backup, use the -backup argument when you call the script:
    Code:
    "Backup_restore.cmd" -backup
    For a forced restoration, -restore:
    Code:
    "Backup_restore.cmd" -restore
    No prompt or pause are involved when you force a backup or a restoration via these args, excepted if total size is over the limit (defined by the MaxSizeB variable). So, be sure that this script is elevated (ran as admin) before forcing a backup/restoration.

    And I introduce the log file system:
    A log file is created when you run the script. Default log path is "script dir"\Backup_restore.log, but you can change it by modifying variables. Note that %~dp0 means "script dir"\.

    Bugs and Bugfixes:
    -A bug correction when an extension was filtered; now the script recreate correctly the backup folder tree.
    -It wasn't easy to correctly calculate folders size. Batch can only manipulate 32-bit integers so I've written a workaround and now the script can handle a total of 2 TB (2,147,483,647,999 Bytes exactly). It was the first time I needed to use such workaround, but now, I will know!
     
  20. ramg1967

    ramg1967 MDL Member

    Sep 13, 2012
    237
    86
    10
    Thanks a lot Soulfate. I will test above script on Fri or Monday and post the result here. I appreciate your effort.

    This script will be of great help during hardware upgrade.