Search code examples
linuxwindowsshellbatch-rename

How to rename all files in a folder and create a renaming map


Note: I have access to both Linux and Windows platform so answers for any of these platforms are fine.

I have a folder which contains less than 10K .png files. I would like to:

1. rename all files as follows:

    <some_filename>.png to 0001.png
    <some_other_name>.png to 0002.png
    <another_name>.png to 0003.png
    and so on...

2. keep this name mapping in a file (see 1 for mapping)

Solution

  • In Windows: This should sort the list alphabetically and rename them all with numbers, padded to 4 characters.

    It writes the bat file that does the renaming. You can examine it before renaming and running it, and doubles as a map of the filenames.

    Filenames with ! characters will probably be an issue.

    @echo off
    setlocal enabledelayedexpansion
    set c=0
    for %%a in (*.png) do (
    set /a c=c+1
    set num=0000!c!
    set num=!num:~-4!
    >>renfile.bat.txt echo ren "%%a" "!num!%%~xa"
    )