0

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?

Mofi
  • 38,783
  • 14
  • 62
  • 115
  • 2
    `move`, `copy` and `ren` work with both files and folders, so I can't imagine, what your problem is. Can you please show your not-yet-working code and explain what exactly doesn't work as intended? – Stephan Jun 21 '20 at 08:52
  • Unable to add that code in the edit :( getting error. Actually I did try move copy and ren command seperately but was not sure how to add time stamp to it and how to get all of them in a bat file.Now that @Mofi has profided the links I will try to integrate the timestamp code and see how it goes from there – Pankaj Harde Jun 21 '20 at 09:02
  • Thanks @Mofi for the links and Stephan for those simple tricks, I was actually working on a complicated code to do all in one. Now I have a seperate bat file to copy the folder and another one to rename the folder with timestamp using the code from the link shared by Mofi – Pankaj Harde Jun 21 '20 at 09:32

3 Answers3

1

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 & goto :EOF

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 /?
  • robocopy /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?
  • xcopy /?
Mofi
  • 38,783
  • 14
  • 62
  • 115
0

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.

VIX
  • 61
  • 1
  • 5
0

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.

Stephan
  • 47,723
  • 10
  • 50
  • 81
  • 2
    you should `Rename "HTML_Report" "HTML_Report_%mydate%_%mytime%"`. `%time%` will have a space before 10 am, which leads to a syntax error without the quotes. (Also I hope your `%time%` is in 24h format. 12h format (with AM/PM) may lead to unintended results. – Stephan Jun 21 '20 at 10:47
  • 2
    On my computer `date /t` gives me `21.06.2020`. That's a good reason to use a [language independent method to get a date/time string](https://stackoverflow.com/questions/7727114/batch-command-date-and-time-in-file-name/18024049#18024049) – Stephan Jun 21 '20 at 11:00