Dos script to rename files

Registration date
Wednesday August 20, 2008
Last seen
August 21, 2008
 rajesh kanna -
I would like to know what batch commands I can to to rename files in directory called \\gbr1w001\data_transfer_gb77$

INVPRT_5023949000004_20080818061329_0000979.GB ---> 5023949000004_MSG_IN_20080818061329_0000979.GB

PPRHDR_5000119000006_20080721061424_00000981.GB_m ---> 5000119000006_MSG_IN_20080721061424_00000981.GB_m

SLSRPT_5023949000004_20080721061317_00000978.GB ---> 5023949000004_MSG_IN_20080721061317_00000978.GB

Basically I would like to drop 7 characters at the begining of the file and insert MSG_IN_ after next 14 characters.

Any help on this will be most appreciated.

Thank you

13 replies

for /r %%x in (%1) do ren "%%x" %2

this will rename file recursively :-)

save in a file give 2 arguments from extension and to extension.

ex: file name is test.bat
command : test *.avi *.bmp

it renames all files with extension avi to bmp (in all subfolders :))
Thank you

A few words of thanks would be greatly appreciated. Add comment

CCM 2942 users have said thank you to us this month

Navigate to the folder with the files to rename in dos, then run this:

rename *.jpg *.mp3

You'll have to do it one folder at a time. You could probably build a batch file to run this command in every folder under some root folder.
Thanks satya for this this command.
I had some difficult to understand it but now I get it.

Perhaps you should be more explicit for newbies like me.
I would say it like this:

Create a bat file (test.bat, for example) with this command:

for /r %%x in (%1) do ren "%%x" %2 

Then, open cmd.exe and from the test.bat folder run (for example):

test *.avi *.bmp
v nice sol;)
Hi Satya,
Is there a way to rename the files with spaces. For example, how do I rename 12345-020-111.pdf to 12345-020-111_Rev A.pdf. There is a space in between Rev and A.
How to change 00001_A-01.txt, 00001_A-02.txt to 00001_b-01.txt, 00001_b-02.txt
Heres my example of renameing a folder in a directory supplied by the user in a batch file:

@echo off
ECHO Auto Profile Reset by AG
SET /p variable=[Enter Workstation ID]
SET /p userid=[Enter User ID]
for /f "tokens=1-3 delims=/- " %%a in ('date /t') do set XDate=%%a-%%b-%%c
for /f "tokens=1-2 delims=: " %%a in ('time /t') do set XTime=%%a.%%b
if exist "\\%variable%\C$\documents and settings\" goto exist
echo PATH NOT FOUND... please try again
goto begin
pushd \\%variable%\C$\documents and settings\
goto skipit
SET /p userid=[Enter User ID]
if exist "\\%variable%\C$\documents and settings\%userid%" goto found
echo ID NOT FOUND... please re-enter
goto notfound
Echo modifying...
rename "%userid%" $"%userid%"$"%XDate%"$
Echo ***SUCCESS***
Echo $%userid%$%XDate%$

is it possible to validate the type of output from ping? Ia dmit I haven't yet tested, but it seems the above will trigger a 'yes' even if the output is timeout, packet response or no host?

Maybe pattern matching as the output is always the same?

Hi Eramus:

Yes, You are right. The original posted question was "if the result give reply then goto another command". So, I assumed the poster was encountering a situation where there was absolutely no output.

I typically look for lost packets such as ".... Lost = 0 ...". So to extract the first numberafter "Lost =", I use
something like the following.

var str output
system ping "" > $output
# Strip off output upto "Lost = ".
stex -c "^Lost = ^]" $output > null
# Extract the first word.
var str word ; wex "1" $output > $word
# Convert to integer
var int lostcount ; set $lostcount = makeint(str($word))
if ($lostcount > 0)
# Yes, packets were lost.
# Insert your additional code here.


The above script is in biterscripting ( ) .
Registration date
Sunday April 19, 2009
Last seen
April 20, 2009

Hi All,

i need to ping an IP for ex.. ping

and I want to write an if command that if the result give reply then goto another command like goto end

ping host

how can I call the result of the ping in the if command !!!?
You can use the following script in biterscripting.

var str output ;
system ping "" > $output
# Did we get any output ?
if ($output <> "")
# Yes, we got some output from ping.
# Insert your additional code here.

Registration date
Friday May 22, 2009
Last seen
February 2, 2010
Reworded quote
i want to rename files a1.txt, a2.txt ...aN.txt files to a1-v1.xt ...aN-vN.txt .

This bitescript will do it. I will assume your files are in folder C:\folder.

# Change dir to C:\folder.
cd "C:\folder"

# Get a list of files a*.txt.
var str list ; lf -n "a*.txt" > $list

# Process files one by one.
while ($list <> "")
    # Get the next file in the list.
    var str file ; lex "1" $list > $file

    # Get the sequence number. It is between a and .txt
    var str seq ; stex -p -r -c "^a&\.txt^" $file > $seq ; stex -c "^a^]" $seq > null ; stex -c "[^.txt^" $seq > null

    # New file name is a$seq-v$seq.txt
    var str newname ; set $newname = "a"+$seq+"-v"+$seq+".txt"

    # Rename file
    echo -e "Renaming file " $file " to " $newname
    system rename ("\""+$file+"\"") ("\""+$newname+"\"")

Save the script as C:\Scripts\rename.txt. Start biterscripting. Enter the following command.

script rename.txt

That's it. Donwload biterscripting if necessary from . It is free. Don't forget to change C:\folder in the script to the correct folder path.

Good luck.

I want to rename existing about 200 directories (full with data) with new names which are in a .txt file.
Please help me
I want to rename existing about 200 directories (full with data) with new names which are in a .txt file.
Please help me


Can you elaborate on that ? You have 200 directories and you have 200 new names for them in a .txt file. How are the old names and new names to be corelated ? If you have both old and new names in the .txt files, see the solution at . The system rename command works for both files and folders, so that script should work for directories too. Make sure you have full paths of the old folders in the .txt file.

If you are looking for the easy way to batch Rename Files folders by the thousands? RenameMaestro is probably the easiest batch file renamer you'll find to instantly rename multiple files.
the code to rename files in a directory is

@echo off
ren ("File path including name") ("File path including new name")
I want to exit c:\documents and setting\abc in dos command »
I have an answer
& I want to write it
use software called file renamer