Simplifying batch backup script - windows

I've made this script (minus all my ECHO for your readability) to backup certain user folders to an external device. It's working flawlessly, but I'm wondering if anyone has any ideas as to how I could simplify it (eg. more 'clever').
I'm new to this site and coding. Please bear with me!
All help appreciated.
#ECHO OFF
SET driveLetter=%~d0
:CHOOSE
SET /P CHOOSE=Are you sure you want to continue [Y/N]?
IF /I "%CHOOSE%" == "Y" GOTO :chooseYes
IF /I "%CHOOSE%" == "N" GOTO :chooseNo
GOTO :CHOOSE
:chooseYes
MKDIR %driveLetter%\Desktop
MKDIR %driveLetter%\Documents
MKDIR %driveLetter%\Favorites
MKDIR %driveLetter%\Pictures
MKDIR %driveLetter%\Downloads
TIMEOUT /T 1 /NOBREAK >NUL
ROBOCOPY %USERPROFILE%\Desktop\ %driveLetter%\Desktop /E /COPYALL /ZB /MT:20 /XJ /R:2 /W:5
ROBOCOPY %USERPROFILE%\Documents\ %driveLetter%\Documents /E /COPYALL /ZB /MT:20 /XJ /R:2 /W:5
ROBOCOPY %USERPROFILE%\Favorites\ %driveLetter%\Favorites /E /COPYALL /ZB /MT:20 /XJ /R:2 /W:5
ROBOCOPY %USERPROFILE%\Pictures\ %driveLetter%\Pictures /E /COPYALL /ZB /MT:20 /XJ /R:2 /W:5
ROBOCOPY %USERPROFILE%\Downloads\ %driveLetter%\Downloads /E /COPYALL /ZB /MT:20 /XJ /R:2 /W:5
CLEANMGR /C: /SAGERUN:65535 /SETUP
TIMEOUT /T 1 /NOBREAK >NUL
DEFRAG /C /H /V /W
PAUSE
EXIT
:chooseNo
TIMEOUT /T 3 /NOBREAK >NUL
Best regards.

If possible, don't repeat yourself
:chooseYes
for %%a in ( Desktop Documents Favorites Pictured Downloads ) do (
robocopy "%userprofile%\%%a" "%driveLetter%\%%a" /E /COPYALL /ZB /MT:20 /XJ /R:2 /W:5
)
CLEANMGR /C: /SAGERUN:65535 /SETUP
TIMEOUT /T 1 /NOBREAK >NUL
DEFRAG /C /H /V /W
PAUSE
EXIT
note: the mkdir has been supressed as the robocopy command will create the target folder

Related

batch for loop file search

#ECHO OFF
SET driveLetter=%~d0
FOR /R %%x IN (*.pst) DO (
ROBOCOPY %%x %driveLetter%\%%x /E /COPYALL /ZB /MT:20 /XJ /R:2 /W:5
)
PAUSE
EXIT
I can't get this working. What I want to do is search the user folder (incl. subfolders) for any .pst files and then copy these to a flash drive.
Pretty simple actually just use xcopy:
xcopy c:\users\%username%\*.pst F:\ /e
(replace F:/ with drive location if its not F).
#ECHO OFF
SET driveLetter=%~d0
ROBOCOPY %USERPROFILE% %driveLetter%\Outlook *.pst /S /COPYALL /ZB /XJ /MT:20 /R:2 /W:5
PAUSE
EXIT
Solution. Thanks to everybody.

CMD Syntax for local command

I would like to run a script, copy my folder and start rundll32 in my copied folder.
mkdir "c:\SmartCard_Treiber"
xcopy "%~DP0SmartCard_Treiber\*" "c:\SmartCard_Treiber" /i /s
cmd /c cd "c:\SmartCard_Treiber" & RUNDLL32.EXE AdvPack.dll,LaunchINFSection Gemalto.inf,,0
TIMEOUT /T 30 /NOBREAK
rd "C:\SmartCard_Treiber" /Q /S
mkdir "c:\SmartCard_Treiber"
xcopy "%~DP0SmartCard_Treiber\*" "c:\SmartCard_Treiber" /i /s
pushd "c:\SmartCard_Treiber"
RUNDLL32.EXE AdvPack.dll,LaunchINFSection Gemalto.inf,,0
TIMEOUT /T 30 /NOBREAK
popd
rd "C:\SmartCard_Treiber" /Q /S
Change to the needed directory (pushd) and when finished return to the previous one (popd)

How to use multiple commands in batch using forfiles command?

#ECHO OFF
SET backdir=backup
SET snapshotdir=snapshots
SET worldprefix=world_
SET itdate=%date:~10,4%-%date:~4,2%-%date:~7,2%
SET hour=%time:~0,2%
IF "%hour:~0,1%" == " " SET hour=0%hour:~1,1%
echo Current date: %itdate%. Current hour: %hour%. Current Minute:Second: %time:~3,2%:%time:~6,2%
forfiles /m "%worldprefix%*" /c (
echo Copying World: #path
cmd /c xcopy /e /c /h /i /v /r /y /q #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%
cmd /c xcopy /e /c /h /i /v /r /y /q #file %backdir%\%itdate%D\worlds\#file
)
echo Copying Plugins
xcopy /e /c /h /i /v /r /y /q plugins %backdir%\%itdate%D\plugins\
xcopy /e /c /h /i /v /r /y /q %backdir%\%itdate%D %backdir%\%itdate%-%hour%H\
echo Backup Complete (assuming no errors above). Attempting to remove old files..
forfiles /p "%snapshotdir%" /c "cmd /c rmdir /s /q #path" /d -7
forfiles /p "%backdir%" /m "*H" /c "cmd /c rmdir /s /q #path" /d -2
forfiles /p "%backdir%" /m "*D" /c "cmd /c rmdir /s /q #path" /d -14
PAUSE
I am trying to copy all files with "world_" as a prefix. I run into a problem when I try to use multiple commands in a loop. I have attempted to write the batch script I want above.
The two commands have absolutely nothing in common, so no, you cannot use parentheses like that.
You must execute all the commands within a single CMD /C. You can concatenate commands on one line using &. I've defined a simple XCOPY "macro" to save a bit of typing.
set XCOPY=xcopy /e /c /h /i /v /r /y /q"
forfiles /m "%worldprefix%*" /c "cmd /c echo Copying World: #path&%XCOPY% #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%&%XCOPY% #file %backdir%\%itdate%D\worlds\#file"
If you escape the quotes, then you can use line continuation to split the logical line accross multiple lines. But then you must also escape the &.
set XCOPY=xcopy /e /c /h /i /v /r /y /q"
forfiles /m "%worldprefix%*" /c ^"cmd /c ^
echo Copying World: #path ^&^
%XCOPY% #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2% ^&^
%XCOPY% #file %backdir%\%itdate%D\worlds\#file^"
Or you could put the & in the front of the line so that you don't need to escape it. The line continuation also escapes the first character of the next line:
set XCOPY=xcopy /e /c /h /i /v /r /y /q"
forfiles /m "%worldprefix%*" /c ^"cmd /c ^
echo Copying World: #path^
&%XCOPY% #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%^
&%XCOPY% #file %backdir%\%itdate%D\worlds\#file^"
I realize this is an old post, but another way of doing it is to call a batch file and pass it the parameters that it needs. And I do not believe that there is any limitations in what can be in that batch file. For example:
forfiles /M "*.img" /C "cmd /c ProcessFile.bat #file #fdate #ftime"

Batch: Copying from computer to network drive

I've written a batch file that will do some tests then copy the results to a network drive. This use to work fine for me but over the last few days I have noticed it doesn't work for me and other people have said it has never worked for them.
This is the section that I am calling to copy to M: which all computers on our network can see. The section is being called and the archived results folder is being created but nothing is being sent to the destination drive.
Can anybody see what I've done wrong?
:CopyResults
SET CLASSPATH=%CLASSPATH_ORIGINAL%
For /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set mydate=%%a-%%b-%%c)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
set source="target"
set Archive= "Archived Results\Test Results - %CLIENTCHOICE% %mydate% At %mytime% Using %BROWSEROPTION%"
set ScreenshotDestination= "Archived Results\Test Results - %CLIENTCHOICE% %mydate% At %mytime% Using %BROWSEROPTION%\Screenshots"
mkdir %Archive%
echo %Archive%
echo d | xcopy %source% %Archive% /k /e /d /y /h /i
rem copy screenshots
set screenshots="Screenshots"
echo d | xcopy %screenshots% %ScreenshotDestination% /k /e /d /y /h /i
%Archive%\site\index.html
rem copy accross to M:
set Destination= "M:\ProductTesting\AutomationResults\%CLIENTCHOICE%\%mydate% At %mytime% Using %BROWSEROPTION% Ran By %username%"
set ScreenshotDestination= "M:\ProductTesting\AutomationResults\%CLIENTCHOICE%\%mydate% At %mytime% Using %BROWSEROPTION%\Screenshots"
set source="target"
mkdir %Destination%
echo %Destination%
echo d | xcopy %source% %Destination% /k /e /d /y /h /i
rem copy screenshots
set screenshots="Screenshots"
echo d | xcopy %screenshots% %ScreenshotDestination% /k /e /d /y /h /i
GOTO Del

How can I remove quotes from forfiles variable fname

Can anyone explain me how to do this?
Problem is #fname contains quotes so concatting %source% and #fname gives an error...
forfiles /P "%source%" /M %file%.* /D -1 /C "cmd /c if exists %source%\#fname.pdf del #path"
The double quotes are not the issue. You've got a syntax error in your command line: instead of if exists … it should go if exist ….
For anyone interested this is the full script.
#echo off
set ERRORLEVEL=0
::variables
set source=C:\ASWFORM\argus
set ps2pdf=C:\Progra~1\gs\gs9.07\gs\lib\ps2pdf
::parameters
if [%1]==[] goto hell
if [%2]==[] goto hell
set file=%1
set hotfolder=%2
:: *********************************************************************************************
::delete files from yesterday
forfiles /P "%source%" /M %file%.* /D -1 /C "cmd /c if exist %source%\#fname.pdf del #path"
::create pdf files
forfiles /P "%source%" /M %file%.ps /C "cmd /c call %ps2pdf% #path"
::move files to hotfolder
xcopy /Y /V %source%\%file%.pdf %hotfolder%
xcopy /Y /V %source%\%file%.xml %hotfolder%
xcopy /Y /V %source%\%file%.adj %hotfolder%
forfiles /P "%source%" /M %file%.* /C "cmd /c if exist %source%\#fname.pdf del #fname.ps"
goto heaven
:hell
echo Usage argus.bat [filename without extension] [path to archive hotfolder]
:heaven
exit 0
:: *********************************************************************************************

Resources