I'm trying to use a batch to rename a file from "Rapp_20220628150328_001.tif" to "Rapp_YYYY_MM_GG_001.tif"
adding "Rap_" and the date is not a problem, but I don't understand how to keep the last 8 characters "_001.tif"
thanks in advance
I'm trying to use a batch to rename a file from "Rapp_20220628150328_001.tif" to "Rapp_YYYY_MM_GG_001.tif"
adding "Rap_" and the date is not a problem, but I don't understand how to keep the last 8 characters "_001.tif"
thanks in advance
For renaming the files
Rapp_20220628150328_001.tif
Rapp_20220628150436_002.tif
in the current directory to
Rapp_2022_06_28_001.tif
Rapp_2022_06_28_002.tif
would be needed only a batch file with the following command lines:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F %%I in ('dir "Rapp_*_*.tif" /B') do (
set "FileName=%%I"
ren "%%I" "!FileName:~0,9!_!FileName:~9,2!_!FileName:~11,2!!FileName:~19!"
)
endlocal
But that simple solution does not work for a directory with the following files:
Image_1.Develop & Test(!)_100%_2022-06-27_01.tif
Image_1.Develop & Test(!)_100%_20220628150328_02.tif
Rapp_2022-06-27_001.tif
Rapp_20220628150328_003.tif
Rapp_20220628150436_002.tif
The file names in this directory should be named finally:
Image_1.Develop & Test(!)_100%_2022-06-27_01.tif
Image_1.Develop & Test(!)_100%_2022_06_28_02.tif
Rapp_2022-06-27_001.tif
Rapp_2022_06_28_002.tif
Rapp_2022_06_28_003.tif
There are following criteria to take into account:
yyyyMMddHHmmss
is variable.cmd.exe
on processing a batch file even on being enclosed in double quotes like !
and %
.In this case the solution with a batch file requires a more complex code like:
@echo off
setlocal EnableDelayedExpansion DisableDelayedExpansion
(for /F "eol=| delims=" %%I in ('dir "*_*_*.*" /A-D /B 2^>nul ^| %SystemRoot%\System32\findstr.exe /R "_[12][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][012][0123456789][012345][0123456789][012345][0123456789]_"') do set "FileName=%%~nI" & call :RenameFile "%%~xI") & goto EndBatch
:RenameFile
set "ModifiedName=%FileName:.=|%
for %%J in ("%ModifiedName:_=.%") do set "FileNumber=%%~xJ" & for %%K in ("%%~nJ") do set "DateTime=%%~xK" & set "FirstPart=%%~nK"
set "FirstPart=%FirstPart:.=_%"
ren "%FileName%%~1" "%FirstPart:|=.%_%DateTime:~1,4%_%DateTime:~5,2%_%DateTime:~7,2%_%FileNumber:~1%%~1"
goto :EOF
:EndBatch
endlocal
This universal solution is slower than the simple solution on execution, but it really works as long as the file names end with _yyyyMMddHHmmss_n.*
whereby n
is a number with one or more digits. The number is not verified by the regular expression as the search string would become too long for FINDSTR.
Both batch files do not handle the use case that a file should be renamed to a name which another file or directory has already in the current directory. It does also not handle the use case that the file rename fails because of the file is currently opened by an application with shared access denied for other processes.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
call /?
cmd /?
dir /?
echo /?
endlocal /?
findstr /?
for /?
goto /?
ren /?
set /?
setlocal /?
Read the Microsoft documentation about Using command redirection operators for an explanation of |
and 2>nul
. The redirection operators |
and >
must be escaped with caret character ^
on FOR command line to be interpreted as literal characters when Windows command interpreter processes this command line before executing command FOR which executes the embedded command line with dir
and findstr
with using a separate command process started in background with option /c
and the command line appended as additional arguments.
See also single line with multiple commands using Windows batch file for an explanation of the unconditional command operator &
on being found in a command line outside a double quoted argument string and also not escaped with ^
to be interpreted as literal character.