I need to backup an existing folder with date-time stamp and replace it (delete and recreate) with new content inside the folder.
Does anyone have a script to do this?
I tried the following code, where %ApplicationDeploymentFolderPath% = \\servername\foldername
IF EXIST %ApplicationDeploymentFolderPath%\Release (
REM Get current date time
@echo off
For /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set mydate=%%c_%%b_%%a)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
set backup_folder=%mydate%_%mytime%
MD %ApplicationDeploymentFolderPath%\%backup_folder%
REM Copy current folder to backup folder
Copy %ApplicationDeploymentFolderPath%\Release %ApplicationDeploymentFolderPath%\%backup_folder%
REM Delete Existing Release folder
RD %ApplicationDeploymentFolderPath%\Release /S /Q
)
MD %ApplicationDeploymentFolderPath%\Release
The command
date
with parameter/T
outputs the current date in format defined by configured country for current user account. Exactly the same date string can be accessed by referencing dynamic environment variableDATE
for example with%DATE%
.The command
time
with parameter/T
outputs the current time in format defined by configured country for current user account. Exactly the same time string can be accessed by referencing dynamic environment variableTIME
for example with%TIME%
.What happens on execution of this command line?
for
respectivelycmd.exe
processing the batch file starts in background one more command process using%ComSpec% /c
with the command line between'
. So executed in background is following with Windows installed inC:\Windows
:The output of command
date
to handle STDOUT of this command process in background is captured by FOR respectively Windows command processor instance executing the batch file.The captured line is split up into three substrings using
/
as string delimiter assigned to the loop variablesa
,b
andc
which are concatenated together in reverse order with underscore as delimiter.This task can be done much faster by replacing
'date /t'
by"%DATE%"
. In this case FOR processes the date string expanded by already runningcmd.exe
on parsing this command line before executing FOR. So there is no starting of one morecmd.exe
in background and capturing its output just to process the same date string which makes batch file execution a bit faster.The same is true for
'time /t'
which can be replaced by"%TIME%"
.But the two FOR loops could be completely optimized away by using string substitution as described for example by answer on What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean? and region dependent date and time format is well known for example by running in a command prompt window:
This command outputs on my computer with German date/time format according to configured country:
It can be seen on this output that the original code would not work on my Windows computer with my account because of date string contains
.
and not/
and time string contains a comma.So better would be using a region independent solution as explained very detailed in answer on Why does %date% produce a different result in batch file executed as scheduled task? The disadvantage is that execution of
wmic.exe
takes much longer thancmd.exe
needs to reformat date and time string toyyyy_MM_dd_HHmm
. However, the batch file is executed most likely not very often per day, and so it does not really matter if execution to get date/time in this format takes some milliseconds or about one second.Copying the entire folder is not really necessary in this case. It should be enough to rename it with:
The command
move
could be also used if commandren
cannot be used for unknown reasons.However, the main problem is missing knowledge about how and when to use delayed expansion. Open a command prompt, run
set /?
and read the output help explaining on an IF and a FOR example delayed environment variable expansion.The issue here is that
backup_folder
is not defined on executing the command lines referencing it with%backup_folder%
because of all occurrences of%variable%
are replaced by Windows command processor already on parsing entire command block starting here with(
on IF condition at top by current value of the referenced environment variable before executing the command IF.So executed on existing release folder is:
This can be seen by debugging the batch file.
See also: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
The solution is here avoiding the command block by changing the first IF condition.
Fast region dependent solution:
Slower region independent solution:
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
goto /?
if /?
md /?
ren /?
set /?
setlocal /?
wmic /?
wmic os /?
wmic os get /?
wmic os get localdatetime /?