kedar.nitty-witty.com
Thursday September 9th 2010

batch script to add remove prefix zero pad bulk file rename

Subscribe to SMS Subscribe to RSS
Subscribe

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

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

Bookmark and Share

Related posts:

  1. Stored procedure to add-remove prefix by rename table mysql Here is one more procedure – (this time) for...
  2. bat – batch file to create formatted date time (ddmmyyyy) directory I required to make a batch script which needs...
  3. Using VLookup and batch script to compare two excel / csv and retrive result Using Vlookup: I have two csv files; File1 has...

Reader Feedback

3 Responses to “batch script to add remove prefix zero pad bulk file rename”

  1. Renamer says:

    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. Kedar says:

    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=

    After you're done with renaming reset the value in counter.txt as follows:
    echo !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= echo !cnt!
    for %%i in (*.%1) do (
    call :Set0Pad %2

    set newName=!str! %%i
    ren "%%i" "!newName!"
    echo !cnt!>counter.txt
    )

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

  3. It’s a good blog…

    I will make sure and bookmark this page and be back to follow you more….

Leave a Reply