Thursday, May 2, 2019

Building a DOS batch script to rename files


I'm currently stuck on an issue with a DOS batch script I’ve been working on, and I need help renaming the files.


Here is the renaming scheme I’m searching for:


Original filenames


FIL120_000112_DDMMYY ==> MYNAME_TLD_EXT_YYMMDD

I need to take the original filename last portion (DDMMYY) and turn it into YYMMDD.


It should go like this


FIL120_000112_301215 ==> MYNAME_TLD_EXT_151230
FIL120_000112_311215 ==> MYNAME_TLD_EXT_151230
FIL120_000112_040116 ==> MYNAME_TLD_EXT_151230
FIL120_000112_050116 ==> MYNAME_TLD_EXT_151230

Please note that it willnot fit current need to get the renaming done using the current date with some command in that part of the file name.


I want to know if it is possible with a batch script to get the last file name part characters to switch positions so essentially YY and DD will swap places.


Additionally, I need the other parts before the fixed YYMMDD to be replaced with static characters of MYNAME_TLD_EXT_ rather than what it may be before the switch, so the final result will be MYNAME_TLD_EXT_ and have NO file extension.


Answer




Building a DOS script to rename files


Original filenames:


FIL120_000112_DDMMYY ==> MYNAME_TLD_EXT_YYMMDD


I need to take the original filename last portion (DDMMYY) and turn it
into YYMMDD.



Below is a Windows batch script that will do just what you list in your question—if you'd like an explanation of what this script is doing, let me know and I'll add comments to the logic to clarify exactly if you're not sure based on what I have in it.


You will need to change the SET SourceDir= to be the full path of the locations where the files you need to rename are located.


I used the .txt file extension of these files in my example, but you can change that to any other extension in the DIR /B "%SourceDir%\*_*_*.txt" part of the logic below—I'll help with that if you have trouble so just let me know.


Also, if your files do not have any extensions at all, just let me know and I'll add another example to this answer without any file extensions that will still complete this for you.


Please note that I tested and confirmed that this worked just as expected with the above examples and explanation in your question above.




Windows Batch Script Examples


File Name Parse and Rename Files with Extensions


@ECHO ON
SET SourceDir=C:\PathForFilesToRename
FOR /F "TOKENS=*" %%A IN ('"DIR /B "%SourceDir%\*_*_*.txt""') DO (
CALL :RenameLogic "%SourceDir%\%%~NXA" "%%~NA" "%%~XA"
)
GOTO :EOF
:RenameLogic
SET FnameNoExt=%~2
FOR /F "TOKENS=1-3 DELIMS=_" %%R IN ("%FnameNoExt%") DO (SET FnamePart1=%%R_%%S)
SET FnameDtPart=%FnameNoExt:~-6%
SET DD=%FnameDtPart:~0,2%
SET MM=%FnameDtPart:~2,2%
SET YY=%FnameDtPart:~-2%
SET Extension=%~3
REN "%~1" "%FnamePart1%_%YY%%MM%%DD%%Extension%"
GOTO :EOF

File Name Parse and Rename Files No Extensions


@ECHO ON
SET SourceDir=C:\PathForFilesToRename
FOR /F "TOKENS=*" %%A IN ('"DIR /B "%SourceDir%\*_*_*""') DO (
CALL :RenameLogic "%SourceDir%\%%~NXA" "%%~NA"
)
GOTO :EOF
:RenameLogic
SET FnameNoExt=%~2
FOR /F "TOKENS=1-3 DELIMS=_" %%R IN ("%FnameNoExt%") DO (SET FnamePart1=%%R_%%S)
SET FnameDtPart=%FnameNoExt:~-6%
SET DD=%FnameDtPart:~0,2%
SET MM=%FnameDtPart:~2,2%
SET YY=%FnameDtPart:~-2%
REN "%~1" "%FnamePart1%_%YY%%MM%%DD%"
GOTO :EOF

File Name Parse and Rename Files No Extensions with Static Rename Part


(Important Note: If two files will have the same YYMMDD name when renamed with the static name, then that'll be a problem but as long as no two files in the same folder will have the same YYMMDD name then this should work without a problem. If there's a file that was already renamed with that static name though and then the next one tries to rename with that same static name, then that'll be a problem.)


Variable Note: In the below SET StaticFnamePart= you can set that to whatever you want it to be set to be for the static rename part of the file moving forward and that'll be what's used to put in that part of the renamed file.


@ECHO ON
SET SourceDir=C:\PathForFilesToRename
SET StaticFnamePart=MYNAME_TLD_EXT
FOR /F "TOKENS=*" %%A IN ('"DIR /B "%SourceDir%\*_*_*""') DO (
CALL :RenameLogic "%SourceDir%\%%~NXA" "%%~NA"
)
GOTO :EOF
:RenameLogic
SET FnameNoExt=%~2
FOR /F "TOKENS=1-3 DELIMS=_" %%R IN ("%FnameNoExt%") DO (SET FnamePart1=%%R_%%S)
SET FnameDtPart=%FnameNoExt:~-6%
SET DD=%FnameDtPart:~0,2%
SET MM=%FnameDtPart:~2,2%
SET YY=%FnameDtPart:~-2%
REN "%~1" "%StaticFnamePart%_%YY%%MM%%DD%"
GOTO :EOF

Further Resources and Reading


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...