I want a windows bat file that renames all files in a folder, reversing a date stamp in the existing name.
For example, files are named as follows:
AccountStatement_10062023.pdf
AccountStatement_10072023.pdf
AccountStatement_10082023.pdf
AccountStatement_10092023.pdf
AccountStatement_10102023.pdf
And I want to rename these with running a simple bat file to as follows:
AccountStatement_20230610.pdf
AccountStatement_20230710.pdf
AccountStatement_20230810.pdf
AccountStatement_20230910.pdf
AccountStatement_20231010.pdf
I have a bat file which renames files with the system date stamp as follows:
forfiles /M * /C "cmd /c echo @fdate @ftime @file"
for /f "tokens=1-3" %%a IN ('forfiles /M NORM*.MP4 /C "cmd /C echo @fdate @ftime @file"') do (
set line1=%%a
set line2=%%b
set line1r=!line1:/=-!
set line2r=!line2::=-!
:echo print %%a %%b %%c
FOR /f "tokens=1-7 delims=/.:- " %%M in ("%%a %%b") DO (
SET Day=%%M
SET Month=%%N
SET Year=%%P
SET Hours=%%Q
SET Minutes=%%R
SET Seconds=%%S
set line3=%%O%%N%%M
set line4=%%P-%%Q
set line3r=!line3!
set line4r=!line4!
)
rename %%c "CTNORM !line3r! !line4r!.*"
)
But this is slightly different in that I need to use the existing file name, with the date format changed to be yyyymmdd from its current name which is in ddmmyyyy format.
Any help much appreciated.
Regards, Dave.
If you do not need to perform validation on the date portion of your filenames, then the following may be all you need, (stored inside the source directory and double-clicked).
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Delims=" %%G In ('Set "PATHEXT=" ^& %SystemRoot%\System32\where.exe
.:"AccountStatement_????????.pdf" 2^>NUL') Do (Set "_=%%~nG"
SetLocal EnableDelayedExpansion
For /F "Delims=" %%H In ("!_:~,-8!!_:~-4!!_:~-6,2!!_:~-8,2!"
) Do EndLocal & Ren "%%G" "%%H%%~xG")
I should add that you should be able to change the substring AccountStatement_
and the extension .pdf
to whatever suits your needs, without having to modify anything else. This is because the nested For /F
loop is only counting the last eight characters of the matching files' basenames.