Copy and Move with auto date/timestamp

Oct 18, 2009
Copy and Move /TS (or similar) automatically adds a datestamp or date+timestamp to the target filename, e.g.

Copy /TS=D MyFile.txt

The copy is MyFile-2020-09-18.txt

Move /TS=DT MyFile.txt NewLoc.txt

The moved file is NewLoc-2020-09-18-12-23.txt

Maybe NewLoc_2020 etc instead, probably allow using one of TCC's standard format indicators.
I wrote this a few weeks back to do something similar. I had a need to rename a bunch of JPG and GIF files I had, to include the creation date (not time) in the filename for a downstream image processor.

The usage would be "renimage *.jpg JOB1", and would rename X.JPG to be JOB1_2020_09_18_01.jpg and Y.JPG to be JOB1_2020_09_18_02.jpg, for example. If you extract the file time as well as the date, and change the rename operation to a copy, it does pretty much what the requested /TS operation above does.

@echo off
::    Rename image files to include creation date and time.
:: Author:        William de Haan
:: Creation date: Sun, 14 Jun 2020 at  8:16pm
:: Last Revision: Sun, 14 Jun 2020 at 10:00pm
:: Usage:
set BATCHFILE=%TEMP%\batchfile.btm
del /qe %BATCHFILE%
echo echo Executing %BATCHFILE% >> %BATCHFILE%

iff [%1] == [] then
   echo usage: %0 name [filespec]

set NAME=%1
iff [%FILESPEC%] == [] then
   set FILESPEC=*.jpg *.jpeg *.gif *.png

:: Iterate through files by date, processing each date independently
set OLDDATE=1980-01-01
for /o:d f in (%FILESPEC%) do (
   set FD=%@filedate[%F%,w,4]
   iff not [%FD%] == [%OLDDATE%] then
      set OLDDATE=%FD%
      gosub PROCESSDATE %FD%


   set DATECOUNT=0
   for /[d%FD%,%FD%] f in (%FILESPEC%) do (
      set DATECOUNT=%@inc[%DATECOUNT%]

   echo Processing date %FD%, count: %DATECOUNT%

   set DIGITS=2
   if %DATECOUNT% GT 99  set DIGITS=3
   if %DATECOUNT% GT 999 set DIGITS=4

   set COUNT=0
   for /[d%FD%,%FD%] f in (%FILESPEC%) do (
      set COUNT=%@inc[%COUNT%]

      :: Write to a batch file rather than renaming the file directly. If
      :: the file is renamed here, then the newly renamed file appears in
      :: the for loop, and it will be processed again, infinitely.
      set FD1=%@replace[-,_,%@filedate[%F%,w,4]]
      iff isfile "%@unquote[%F%]" then
         iff not isfile "%@unquote[%NAME%]_%FD1%_%COUNT%.*" then
            echo ren /qe "%@unquote[%F%]" "%@unquote[%NAME%]_%FD1%_%COUNT%.*" >> %BATCHFILE%

:SETDIGIT [count digits]
   set TMP1=000%%COUNT%
   set TMP2=%@right[%DIGITS%,%TMP1%]
   set COUNT=%TMP2
I realize these can be done with custom coding, but my suggestion is for an addition, and with fairly simple syntax.

I do have some routines that add a date and timestamp for a specific Excel *.xlam file. But that has to get the full path, parse the extension, construct the addition, create the new target path, and then do the copy under the new target filename.

When I started writing the suggestion I just thought, "Okay, copy and add -CCYYMMDD".

Then I thought, "What if they want to move instead of copy?" Then, "Wait, people might need the time too." Then, "They might only want the time." Then, "They probably would want to specify the format--and TCC already supports multiple formats."

Similar threads