Here is my commented batch file solution which of course is very similar to solution posted by LotPings.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "Folder=%CD%"
if "%Folder:~-1%" == "\" set "Folder=%Folder:~0,-1%"
rem Determine current local date using a region independent method in
rem basic international format yyyyMMdd according to ISO 8601:2004.
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "CurrentDate=%%I"
set "CurrentDate=%CurrentDate:~0,8%"
rem Process all non hidden files with three underscores in file name and of
rem which file name ends with ALARM_INPUT and having file extension JPG.
rem On each iteration of the loop first is determined the third underscore
rem delimited part from file name which contains the date in first 8 chars.
rem If the current file name has not the current date in file name, the
rem file name is modified to required format.
for /F "delims=" %%I in ('dir "%Folder%\*_*_*_ALARM_INPUT.JPG" /A-D /B /ON 2^>nul') do (
for /F "tokens=3 delims=_" %%J in ("%%I") do set "FileDateNumber=%%J"
if not "!FileDateNumber:~0,8!" == "%CurrentDate%" ren "%Folder%\%%I" "Cam 1_!FileDateNumber!.jpg"
)
endlocal
This batch file runs by default in current directory. The directory can be changed to directory of the batch file by modifying %CD%
to %~dp0
in second line. And it is of course also possible to specify any other directory in second line.
This solution above works only with no file having an exclamation mark in file name.
Otherwise the batch code below using a subroutine is required for this file renaming task.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Folder=%CD%"
if "%Folder:~-1%" == "\" set "Folder=%Folder:~0,-1%"
rem Determine current local date using a region independent method in
rem basic international format yyyyMMdd according to ISO 8601:2004.
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "CurrentDate=%%I"
set "CurrentDate=%CurrentDate:~0,8%"
rem Process all non hidden files with three underscores in file name and of
rem which file name ends with ALARM_INPUT and having file extension JPG.
for /F "delims=" %%I in ('dir "%Folder%\*_*_*_ALARM_INPUT.JPG" /A-D-H /B /ON 2^>nul') do call :FileRename "%Folder%\%%I"
endlocal
goto :EOF
rem The subroutine FileRename first gets third underscore delimited part
rem from file name which contains the date in first 8 characters. If the
rem current file name has not the current date in file name, the subroutine
rem renames the file to required format.
:FileRename
for /F "tokens=3 delims=_" %%J in ("%~n1") do set "FileDateNumber=%%J"
if not "%FileDateNumber:~0,8%" == "%CurrentDate%" echo ren %1 "Cam 1_%FileDateNumber%.jpg"
goto :EOF
For details on first FOR loop used to get current date independent on country set in Windows Region and Language settings for currently used account see my answer on Why does %date% produce a different result in batch file executed as scheduled task?
Both batch files use a string comparison for comparing the dates as I do not expect that a file contains a date in future in file name. It is of course possible to replace ==
by LSS
and remove not
in IF command line to run an integer comparison on comparing date in file name with current date. The integer comparison is a bit slower although not noticeable slower in comparison to total execution time.
The solutions use command DIR to process by FOR a captured list of file names to avoid possible problems like skipping some file names on using directly FOR for searching for files matching the wildcard pattern in case of the files are stored on a FAT32 or exFAT partition.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
ren /?
set /?
setlocal /?
See also answer on Where does GOTO :EOF return to?