-1

I'm trying to rename a list of 470 files with the name p01, p02 etc to a specific name for each file. So I've got a list (.txt or .xls) in the correct order for those filenames, but I'm unable to figure out a for loop to rename this in a batch.
I've checked other threads on this forum and Google, but unable to get the result i need.
Hopefully someone has a solution for this... Python or cmd window are both possible.

So in short:

p01.pdf --> Blabla.pdf

p02.pdf --> Othertext.pdf
....

Community
  • 1
  • 1
JeBo
  • 187
  • 1
  • 3
  • 12
  • Does your list also contain the old name or just the new ones in correct order? – winklerrr Sep 21 '18 at 08:29
  • Whatever works, I've got an excel with the old and new name next to eacher other in the correct order – JeBo Sep 21 '18 at 08:30
  • I've used cmd to create a list of the files and after used Excel to put those in the correct order. Based on a list of names I've put to next to the correct `p0x`, thats the reason it's currently in Excel. But it can be exported to any list – JeBo Sep 21 '18 at 08:34
  • You cannot brute force an excel file with batch. you need text based file, i.e `txt` or `csv` – Gerhard Sep 21 '18 at 08:34
  • `for filename in os.listdir('path/to/files'):` will loop through all the files in your directory. Then you can use if to check the old file name and rename it. – Ninad Gaikwad Sep 21 '18 at 08:35
  • So if you used a text file, seperate the filenames by comma. i.e `"oldname.pdf","newname.pdf"` then simply use batch string. `for /f "tokens=1,2 delims=," %%i in (filenames.txt) do ren %%i %%j` – Gerhard Sep 21 '18 at 08:39

2 Answers2

2

Python 3

  1. First export your old and new filenames to a filenames.txt file. Its content should be formatted like old_filename.pdf,new_filename.pdf:

    p01.pdf,blabla.pdf
    p02.pdf,foobar.pdf
    
  2. Then create a python file rename.py in the same directory.

    The script reads line per line from that file and uses os.rename(src, dest) to rename your files accordingly:

    import os
    
    with open("filenames.txt", mode="r") as open_file:
        filenames = open_file.readlines()
    
    for filename in filenames:
        separator_index = filename.find(",")
        old_filename = filename[:separator_index]
        new_filename = filename[separator_index + 1:] # +1 to exclude the comma from the new filename
    
        os.rename(old_filename, new_filename)
    
  3. Now, to execute your script, open cmd.exe in that directory and type:

    $ python3 rename.py
    

Different Paths

If your files aren't all in the same directory just add their paths to the filenames.txt like so:

path/to/old_filename.pdf,path/to/new_filename.pdf

Pro-Tip: Navigate to that folder in Windows Explorer where all the files are, press CTRL + L to highlight the navigation bar, write cmd and confirm by pressing ENTER. cmd.exe should now directly open with the correct path to your folder already set.

winklerrr
  • 13,026
  • 8
  • 71
  • 88
1

For a batch solution, create text file and add the names seperated by comma and let's call it something like filenames.txt:

p01.pdf,Blabla.pdf
p02.pdf,Othertext.pdf
...

Then do as a batch file:

for /f "tokens=1,2 delims=," %%i in (filenames.txt) do ren "%%i" "%%j"

or directly from console (cmd.exe) This is the exact same, we just use a single % instead of double %%

for /f "tokens=1,2 delims=," %i in (filenames.txt) do ren "%i" "%j"
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Thanks for your reply! But when I run this, I get the following error: `%%i was unexpected at this time` filenames.txt is in the same folder and looks exactly like you mentioned. – JeBo Sep 21 '18 at 08:55
  • Did you run it from cmdline or batch file seems you ran the batch option (first script line) from cmdline, that will not work from cmdline as it needs only one `%` See complete answer please. – Gerhard Sep 21 '18 at 08:56
  • sorry I forgot to refresh for commenting and didn't notice the edit. – JeBo Sep 21 '18 at 09:00