Batch file to rename files by adding different suffix to each file

1.2k views Asked by At

I want to create a batch file that will rename files from a folder my adding a different suffix to each file An example would be like this,

  • file1.mp4
  • file2.mp4
  • file3.mkv
  • file4.mkv

to these

  • file1 sandwich.mp4
  • file2 hot dog.mp4
  • file3 apple.mkv
  • file4 toast.mkv

I am hoping to put all these words in the batch file but putting it in a separate txt file would be more preferable

Note: I will put the same number of suffix in the txt file as there are files on the folder.

I just want a faster way of adding these suffix than doing it one by one manually

I have limited knowledge about these codes

1

There are 1 answers

3
Aacini On BEST ANSWER

The program below rename the files in the order given by dir command with the suffixes given in suffixes.txt file. If there are more files than suffixes, the last suffix will be used several times.

@echo off
setlocal EnableDelayedExpansion

< suffixes.txt (
   for /F "delims=" %%a in ('dir /B folder\*.*') do (
      set /P suffix=
      ECHO ren "%%~Fa" "%%~Na !suffix!%%~Xa"
   )
)

For example:

C:\> type suffixes.txt
sandwich
hot dog
apple
toast

C:\> test.bat
ren "file1.mp4" "file1 sandwich.mp4"
ren "file2.mp4" "file2 hot dog.mp4"
ren "file3.mkv" "file3 apple.mkv"
ren "file4.mkv" "file4 toast.mkv"

If the ren commands looks correct, remove the ECHO part in the last command in order to execute the ren commands.