Reputation: 55
I tried a lot of searching and working around, but I am unable to get around this sticky problem and hence requesting for help. Almost all the solutions talk about file rename, but I am looking for a folder copy and rename. (I am not a batch or cmd expert.)
I have a requirement as below where I have two directories:
Source: C:\temp\LR_Results\
Destination: C:\temp\HTML_Reports\
I have a folder with name HTML_Report
inside source directory C:\temp\LR_Results\
which I want to copy to destination directory C:\temp\HTML_Reports\
.
The requirement is that a timestamp should be added to the folder name HTML_Report
on being copied to the destination C:\temp\HTML_Reports\
from the source C:\temp\LR_Results\
.
So the resulting directory structure should look like:
C:\temp\HTML_Reports\HTML_Report_dd_mm_yyyy_hhmmss
The timestamp should be the current date/time.
How can this be done with a batch file?
Upvotes: 0
Views: 5533
Reputation: 49086
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 /?
Upvotes: 1
Reputation: 55
So the solution that worked for me is as below.
Yes, it only has rename
command with the current date time stamp. to copy the folder, I used the inbuild commandline interface solution that comes with Team City Server, and then I made a call to the rename.bat file which did the trick for me.
Below is the rename Code:
@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
CD C:\temp\HTML_Results
Rename HTML_Report HTML_Report_%mydate%_%mytime%
Definitely not the most efficient but works for me.
Upvotes: 0
Reputation: 71
I am sharing date formatting mechanism below. This will definitely help you create your solution.
set foldername=folder1
echo %foldername%_%date:~-4,4%%date:~-7,2%%date:~-10,2%%time:~0,2%%time:~3,2%
For further reading on Batch Scripting Refer its documentation or any tutorial such as this.
Upvotes: 0