Copy and Move with auto date/timestamp

Oct 18, 2009
351
17
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.
 
Apr 2, 2011
1,290
3
53
North Carolina, USA
Could use TCC's build in Date / time routines to retrieve the current date and time....
 
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.

Code:
@echo off
::------------------------------------------------------------------------
::
:: RENIMAGE.BTM
::
::    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:
::       RENIMAGE NAME
::
::------------------------------------------------------------------------
setlocal
set BATCHFILE=%TEMP%\batchfile.btm
del /qe %BATCHFILE%
echo echo Executing %BATCHFILE% >> %BATCHFILE%

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

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

::
:: 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%
   endiff
)

call %BATCHFILE%
quit


: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%]
      gosub SETDIGIT %COUNT% %DIGITS%

      ::
      :: 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%
         endiff
      endiff
   )
return


:SETDIGIT [count digits]
   set TMP1=000%%COUNT%
   set TMP2=%@right[%DIGITS%,%TMP1%]
   set COUNT=%TMP2
return
 
Oct 18, 2009
351
17
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."