-1

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

  • A couple of questions that I think would help some folks understand what you are trying to do: (1) do you really need spaces in the suffix? (2) are you saying you have a mapping from a set of prefixes to a set of suffixes? – David Elson Jun 13 '15 at 21:35
  • umm yes i do need spaces ... and i don't understand your second question, i have very limited knowledge about all these. Could you explain your question? – Krishneel Chand Jun 13 '15 at 21:46
  • You could put the mapping in a text file, then have a script that builds up an associative array (requires bash >= 4.0) at the start. Then you can easily attack the actual problem. Perl (or something similar) could also be used. The allowed content of the suffix would determine the syntax of the mapping file. – David Elson Jun 13 '15 at 22:16
  • The question is if you want to assign the first suffix ("sandwich") to the first file in `dir` order, etc., or if is there a more complex rename scheme... What happen if there are more files than suffixes? Please, do _not_ answer "that will not happen"; describe what to do in such a case! **Note**: do NOT post such information here in a comment; modify the original question instead! – Aacini Jun 13 '15 at 23:00
  • i corrected the question.. :) any more doubts? – Krishneel Chand Jun 14 '15 at 00:50

1 Answers1

1

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.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • thank you for the answer but I seem to have done something wrong, I get the error "the system cannot find the file specified" – Krishneel Chand Jun 14 '15 at 01:03
  • i changed it to this but only the first mp4 file got renamed '@echo off setlocal EnableDelayedExpansion < suffixes.txt ( for /F "delims=" %%a in ("D:\test\*.mp4") do ( set /P suffix= ren "%%~Fa" "%%~Na !suffix!%%~Xa" ) )' – Krishneel Chand Jun 14 '15 at 01:37
  • If you review your question you will realize that you did not specified the name of the folder nor the name of the suffix file, so the names I used in my code are _just examples_. It should be obvious that when you use the posted code _is up to you_ to use the correct names to avoid "file not found" errors, and do not modify the code in a way that cause errors, like removing a `'dir /B ...'` command! If you have any doubt, you should ask for a further clarification on how to insert the real names in the code... – Aacini Jun 14 '15 at 06:25