Reorder file name in folder with batch script

1.5k Views Asked by At

I have many files that I would like to reorder the name of.

Currently the file names read as

XXX-YYYYY-ZZZ-A1B2C3

I would like to reorder A1B2C3 to ABC123.

XXX-YYYYY-ZZZ-ABC123

The XYZ portion of the string will be variable lengths for each file, the A1B2C3 will always be at the end of the string.

Being able to batch over all files in the current folder is a good starting point, and batching over all files in the current folder and all subdirectories would be even better.

Any help is much appreciated!

2

There are 2 best solutions below

2
On BEST ANSWER
@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%f in ('dir /A-D /B *.*') do (
   for /F "tokens=1-4 delims=-" %%a in ("%%~Nf") do (
      set "last=%%d"
      set "new=!last:~0,1!!last:~2,1!!last:~4,1!!last:~1,1!!last:~3,1!!last:~5,1!"
      ECHO ren "%%f" "%%a-%%b-%%c-!new!%%~Xf"
   )
)

Output example:

C:\> test.bat
ren "XXX-YYYYY-ZZZ-A1B2C3.ext" "XXX-YYYYY-ZZZ-ABC123.ext"
ren "XXX-YYYYY-ZZZ-X9Y8Z7.ext" "XXX-YYYYY-ZZZ-XYZ987.ext"

Some points about this code:

  • Your description say nothing about file extension so I assumed that the files have one. If not, just remove the %%~Xf part in the ECHO ren command.

  • The last command just show in the screen the REN commands. If the REN commands looks correct, remove the ECHO part in order to execute the REN. You may also duplicate this line in order to see the REN commands when they are executed.

  • To also process all subdirectories, add /S switch in DIR command this way: dir /A-D /B /S *.*

  • If you have also other files that don't needs renaming, you may select just these files changing the *.* wild-card in DIR command by *-*-*-*.*

4
On

You haven't mentioned which operating system you're using. I'll presume you're using *nix (Linux, OSX, BSD, etc); on Windows you could always install Perl and/or Cygwin.

Using the following rename script, you can write Regular Expressions to rename your files:

#!/usr/bin/perl -w                                                           

# rename - fix up file names                                                    
# examples:                                                                     
# rename 's/\.orig$//' *.orig                                                   
# rename 'tr/A-Z/a-z/ unless /^Make/' *                                         
# rename '$_ .= ".bad" *.f                                                      

$op = shift or die "Usage: rename expr [files]\n";                              
chomp(@ARGV = <STDIN>) unless @ARGV;                                            
for (@ARGV)                                                                     
{                                                                               
  $was = $_;                                                                    
  eval $op;                                                                     
  die $@ if $@;                                                                 
  rename($was, $_) unless $was eq $_;                                           
}  

(Note: I didn't write this rename script - it's been kicking around the internet for years).

For example, to remove the XXX- from the start of all your files, you would do:

~/bin/rename 's/^XXX-//' *

The regex for renaming A1B2C3 to ABC123 is left as an exercise for the reader.