18 Mar

batch script to add remove prefix zero pad bulk file rename

This post has two batch scripts:
1. Batch script to rename files with zero padded number series-prefix

2. Batch script to remove prefix of perticular length

1. Batch script to rename files with zero padded number series-prefix

This script will accept file-type to be searched and lenght of zero-padded prefix to be attached.

Usage: RenZeroPad.bat

Consider you have to rename / arrange a lot of mp3 files in a perticular sequence:

File names before execution:
fileX.mp3
fileY.mp3
fileZ.mp3

Command on dos prompt: RenZeroPad.bat mp3 4

File names with attached prefix:
0001 fileX.mp3
0002 fileY.mp3
0003 fileZ.mp3

[ad#ad-2-300×250]

The Batch Script to add zero pad digits prefix:

@echo off
setLocal EnableDelayedExpansion

set /a cnt=1
for %%i in (*.%1) do (
call :Set0Pad %2

set newName=!str! %%i
ren “%%i” “!newName!”
)

:Set0Pad
set padcntr=0000000000%cnt%
set str=%padcntr:~-%1%
set renstr=%str%
set /a cnt+=1

** Download available at the end of the page.

2. Batch script to remove prefix of perticular length

This script will accept file-type to be searched and lenght of prefix to be removed.

Usage: RemovePrefix.bat

Consider you have to rename / remove prefixes from a bunch of files:

0001_fileX.doc
0002_fileY.doc
0003_fileZ.doc

Command on dos prompt: RemovePrefix.bat mp3 5

Files will be renamed with removed prefixes as follows:
fileX.doc
fileY.doc
fileZ.doc

The Batch Script to remove prefix:

@echo off
setLocal EnableDelayedExpansion

set /a cnt=1
for %%i in (*.%1) do (
set str=%%i
set newstr=!str:~%2!
ren “%%i” “!newstr!”
)

Download: RemovePrefix.bat and RenZeroPad.bat

3 thoughts on “batch script to add remove prefix zero pad bulk file rename

  1. I need to create an batch file that will
    rename a file on my F: drive

    named MyBackup 00001.qic

    to MyBackup nnnnn.qic (nnnnn = next available file number).

    For example if files exist:
    MyBackup 00002.qic
    MyBackup 00003.qic
    MyBackup 00004.qic
    MyBackup 00005.qic

    then rename MyBackup 00001.qic to MyBackup 00006.qic

    Yes, the file names always have a space just before the 5 digit number.

    Since I am just learning how to write .bat files for my Windows2000 Computers,

    File Renamer

  2. You may read from and write to a file to remember last file name using batch script.
    Create a counter.txt to store the last variable value
    As per my script use following line to assign last counter value.

    set /p cnt=counter.txt

    Consider this:-

    Step 1: Create counter.txt in same directory of script.
    step 2: Use below script updated to read counter value from a text file and rename from that number.

    @echo off
    setLocal EnableDelayedExpansion

    :: set /a cnt=1
    set /p cnt=counter.txt
    )

    :Set0Pad
    set padcntr=0000000000%cnt%
    set str=!padcntr:~-%1!
    set renstr=%str%
    set /a cnt+=1

  3. I am looking to just rename *.jpg files to a fixed length. They all need to be 7 left of .jpg?
    Can your example pad script be modified to do that easily?
    Examples:
    1234567.jpg ok already.
    123456.jpg needs to be 0123456.jpg
    And of course they could start with 26.jpg (one or more numbers left of jpg) for example 0000026.jpg

    tia.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

-- Kedar Vaijanapurkar --