@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