Batch renaming multiple files with conditions

38 views Asked by At

[[Solved]]

I am needing to rename several files in a specific format using a batch script. It will be ran at the same time every day so the date and times have already been figured out and tested. I need conditions to look at the prefix of the files so they can be renamed correctly. The source folder won't have but one or two of these files every day.

They start like this

  • FILEtypeA.123456789.EXT.MMDDHHMMSS
  • FILEtypeB.123456789.EXT.MMDDHHMMSS
  • FILEtypeC.123456789.EXT.MMDDHHMMSS
  • FILEtypeD.123456789.EXT.MMDDHHMMSS

(the prefix of the file is important, middle sections and time stamp are not)

They have to end like this

  • 000000000.TSMMDDYY_HHMMSS.FILEtypeA.dat
  • 000000000.TSMMDDYY_HHMMSS.FILEtypeB.dat
  • 000000000.TSMMDDYY_HHMMSS.FILEtypeC.dat
  • 000000000.TSMMDDYY_HHMMSS.FILEtypeD.dat

(the prefix stays the same, the time stamp will be the current time it is renamed, the suffix is important)

[rev1] I have fixed the renaming syntax and the wildcard also works now. adding the other file types to see if it renames each properly without mixing them up.

[rev2] Solved this by setting the stamp as it will be needed later. I was also having problems with it leaving a space instead of the leading zero when run before 10am.

@echo
::The script runs every day so it will not run into duplicates
::Sets hour to zero-fill
    SET HOUR=%TIME:~0,2%
    IF "%HOUR:~0,1%" == " " SET HOUR=0%HOUR:~1,1%
::Sets timestamp for file
    SET STAMP=%DATE:~4,2%%DATE:~7,2%%DATE:~12,2%_%HOUR%%TIME:~3,2%%TIME:~6,2%
::Renames each of the files per their prefix to their correct suffix with the correct timestamp format
    ren "C:\Users\myself\Desktop\Batch\FILEtypeA.*" 000000000.TS%STAMP%.FILEtypeA.dat
    ren "C:\Users\myself\Desktop\Batch\FILEtypeB.*" 000000000.TS%STAMP%.FILEtypeB.dat
    ren "C:\Users\myself\Desktop\Batch\FILEtypeC.*" 000000000.TS%STAMP%.FILEtypeC.dat
    ren "C:\Users\myself\Desktop\Batch\FILEtypeD.*" 000000000.TS%STAMP%.FILEtypeD.dat
Exit
0

There are 0 answers