3

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_<YYMMDD> and have NO file extension.

  • Do you want the script for MS-DOS 6, or Windows cmd.exe? If it's Windows, does it have to be a batch script? (Windows 7+ has more powerful scripting languages like PowerShell.) – user1686 Jan 7 '16 at 8:45
  • Hellothanks for reply. I need it to run on windows server 2008 R2. in fact i already have a script (not a powershell but jsut basic dos script) running to do the job, but the rename part rely on a snippet of code to find the previous date and append it to the file en. this lead me to an issue every monday, where the file needs to be renamed, not to the previous day (sunday) but to the last working day. Another issu arise when we have holidays.. – khiloc Jan 7 '16 at 9:44
  • This is why i want to change the way the script rename, instead of try to get the last working day (which i think will be ccomplicated to script....complicated for me...not for scirpiting gurus....) so my idea is , as the incoming original file already have the needed date in it filename, i just need to switch the original date writing to fit my needs thus doing the: FIL120_000112_DDMMYY ==> MYNAME_TLD_EXT_YYMMDD – khiloc Jan 7 '16 at 9:49
7

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.