0

I have this code which copies from one location to another. Is there a method to add a date \ time to the file name. In the folder, there's only one csv file which the name changes once in a while. What I've found is that it doesn't copy the file if there's already a file with the same name. It would be nice to copy the .csv and add a date \ time somehow.

Robocopy "D:\BACnet-CSV\Temp" "D:\BACnet-CSV\Test csv files\Test1" *.csv /njh /njs /ts /np /ns /log+:D:\BACnet-CSV\Copy_Log.txt
echo Robocopy exit: %ERRORLEVEL% %DATE% %TIME% >>D:\BACnet-CSV\Copy_Log.txt

Looked for solutions on the web and tried couple of things without success.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Robocopy.exe is capable of copying files which have the same name, overwriting the existing one. You simply need to read its help and usage information, `%SystemRoot%\System32\Robocopy.exe /?`. Other than that, there are literally hundreds of examples on this site showing how to generate filenames with dates and/or times. – Compo Apr 08 '23 at 16:49
  • I don't want to override the same file. I want to take what ever the name of the .csv and add a date and time to it. I've looked and unless I'm looking at something incorrectly, which could be - there's no switch at append the file name with the date and time. I've looked on the web - if you know of a site please shoot it my way – Clinton Langan Apr 08 '23 at 19:32
  • The task to copy all `.csv` files with additionally changing the file name of each destination files cannot be done with `robocopy`. The usage help of `robocopy` lists no option to defined a mask for the destination files. There can be used `robcopy` to copy the files with creating a log file, but there must be run additionally a `for` loop in the destination folder to rename the CSV files. The answers on [Time is set incorrectly after midnight](https://stackoverflow.com/questions/60124351/) describes how get the current date in a suitable format for usage in file name. – Mofi Apr 08 '23 at 20:27
  • But if there must be always copied just one CSV file, there should be used the internal command `copy` of `cmd.exe` instead of `%SystemRoot%\System32\robocopy.exe` which is designed to copy/move many files/folders, i.e something like `if exist "D:\BACnet-CSV\Temp\*.csv" for /F "tokens=1-6 delims=/: " %%G in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do (for %%# in ("D:\BACnet-CSV\Temp\*.csv") do copy "%%#" "D:\BACnet-CSV\Test csv files\Test1\%%~n#_%%G-%%H-%%I_%%J_%%K_%L%%~nx#") & goto FileCopied` and next line being `:FileCopied`. The destination directory must exist. – Mofi Apr 08 '23 at 20:35

1 Answers1

0

Robocopy does not provide an option to rename a file while copying. If you want to have a timestamp on your copy you can do that in two ways - either say Robocopy to use a timestamped folder to hold the copy, or rename the copied files one by one with a FOR loop (which defeats the purpose of robocopy somewhat - I'll expand on this if need be).

Either way, you need to set the timestamp for this. If your task is working on a daily schedule, it will suffice to use the current date:

set TIMESTAMP=%DATE:/=.%
set TIMESTAMP=%TIMESTAMP:.=-%

Then use it to tell Robocopy where to place the file(s):

Robocopy "D:\BACnet-CSV\Temp" "D:\BACnet-CSV\Test csv files\Test-%TIMESTAMP%" *.csv /njh /njs /ts /np /ns /log+:D:\BACnet-CSV\Copy_Log.txt

NB: There are lots of possibilities to get a useful timestamp, I suggest to give this Filename timestamp in Windows CMD batch script getting truncated a good read.

yacc
  • 2,915
  • 4
  • 19
  • 33