The following code can be used to get current date and time in format dd_MM_yyyy_hhmmss
without depending on which country is configured for the account used to run the batch file:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
if exist %SystemRoot%\System32\robocopy.exe for /F "tokens=1-6 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "CurrentDateTime=%%K_%%J_%%I_%%L%%M%%N" & goto CopyDirectory
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "CurrentDateTime=%%I"
set "CurrentDateTime=%CurrentDateTime:~6,2%_%CurrentDateTime:~4,2%_%CurrentDateTime:~0,4%_%CurrentDateTime:~8,6%"
:CopyDirectory
%SystemRoot%\System32\xcopy.exe "C:\temp\LR_Results\HTML_Report" "C:\temp\HTML_Reports\HTML_Report_%CurrentDateTime%\" /C /E /H /I /K /Q /R /Y >nul
endlocal
The code uses ROBOCOPY to get current date time region independent on being available which is the case by default for Windows Vista and Windows Server 2003 and all later Windows versions. The much slower solution with WMIC is used on Windows XP on which ROBOCOPY is not available by default.
For a detailed explanation of the three command lines used to get the current date and time region independent with either ROBOCOPY or WMIC read my answer on Time is set incorrectly after midnight.
See also my answer on single line with multiple commands using Windows batch file for an explanation of operator &
which is used to run command GOTO immediately after definition of environment variable CurrentDateTime
as a result of processing the first line output by ROBOCOPY with date and time resulting in exiting the loop before processing the next line.
It would be much better to use as date/time format yyyy-MM-dd_hhmmss
which is the international date format. It has the big advantage that multiple subdirectories in C:\temp\HTML_Reports
with name HTML_Reports_yyyy-MM-dd_hhmmss
displayed sorted alphabetically by name are at the same time also displayed sorted in chronological order. That really helps to get a better overview on the directories with the HTML reports.
The code necessary for date/time format yyyy-MM-dd_hhmmss
with not writing as much as possible on a single command line for most efficient execution:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
if exist %SystemRoot%\System32\robocopy.exe (
for /F "tokens=1-6 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do (
%SystemRoot%\System32\robocopy.exe "C:\temp\LR_Results\HTML_Report" "C:\temp\HTML_Reports\HTML_Report_%%I-%%J-%%K_%%L%%M%%N" /E /R:3 /W:2 /NDL /NFL /NJH /NJS
goto EndBatch
)
)
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "CurrentDateTime=%%I"
set "CurrentDateTime=%CurrentDateTime:~0,4%-%CurrentDateTime:~4,2%-%CurrentDateTime:~6,2%_%CurrentDateTime:~8,6%"
%SystemRoot%\System32\xcopy.exe "C:\temp\LR_Results\HTML_Report" "C:\temp\HTML_Reports\HTML_Report_%CurrentDateTime%\" /C /E /H /I /K /Q /R /Y >nul
:EndBatch
endlocal
This batch file uses ROBOCOPY to get region independent the current date and time and to copy the directory on being available. Otherwise WMIC is used to get region independent the current date and time and XCOPY is used to copy the directory.
The entire task can be done with a batch file containing just one command line on no compatibility with Windows XP must be taken into account and assuming that the required command extensions are enabled and not required delayed environment variable expansion is disabled as by default:
@for /F "tokens=1-6 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do @%SystemRoot%\System32\robocopy.exe "C:\temp\LR_Results\HTML_Report" "C:\temp\HTML_Reports\HTML_Report_%%I-%%J-%%K_%%L%%M%%N" /E /R:3 /W:2 /NDL /NFL /NJH /NJS & exit /B
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
echo /?
endlocal /?
exit /?
for /?
goto /?
if /?
robocopy /?
set /?
setlocal /?
wmic /?
wmic os /?
wmic os get /?
wmic os get localdatetime /?
xcopy /?