A few words of thanks would be greatly appreciated.

DOS script to rename files

If there are multiple files in a document which a user would like to change the extension of, the individual can use a batch command, which will change the extension of all similar files to the desired extension. For example, if one wants to change files with an extension of .avi to .bmp, one would need to give two arguments with the 'from' extension and the 'to' extension. If the file name is test.bat, the command should look something like this: test *.avi *.bmp. This command will change all the .avi extension files to .bmp extension files. Alternatively, download biterscripting from biterscripting.com to change file extensions.


I would like to know what batch commands I can use to rename files in the directory called \gbr1w001data_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 beginning of the file and insert MSG_IN_ after next the 14 characters.

Please help!



 /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).

Solution 2

The following biterscripting script will do just that. To download biterscripting free, go to their website at biterscripting.com

Save the script in a file called C:/X.txt. Start biterscripting interactive and call the script exactly as follows:

script "C:/X.txt" dir("\gbr1w001\data_transfer_gb77") pattern("INVPRT_") 

All dir, file name, pattern are examples, use your own.

You can call this script on any pattern you describe. Or, you can create a master script to repetitively call this script for each pattern. You can do this in batch mode by calling biterscripting
from another program or DOS.

stex=string extractor, chex=character extracter, chin=character inserter, -p=preserve original string, ]=upto and including, etc. Do a help on commands to get the details. These are rather powerful editor commands.



# Declare input arguments.   
var str dir pattern   

# Collect a list of files matching the pattern.   
var str list   
find -f $pattern $dir >$list   

# The list of files is in $list. Process one at a time.   
while ($list <> "")   
# Get the next file.   
var str file, path, old_name, new_name   
lex "1" $list > $file   

# Remove path. This will give us just the file name in $old_name.   
stex -p "^/^l[" $file > $old_name # After (but excluding) the last /   

# Drop the first 7 characters.   
chex -p "7]" $old_name > $new_name   

# Insert MSG_IN_ after 14 characters.   
chin "14" "MSG_IN_" $new_name > null # We are using > null because we dont want to see the output   
# We want to insert MSG_IN_ into $new_name (thus no -p option).   
script SS_SlashBack.txt ospath($file) >$file   

# Rename $file to $new_name   
system rename $file $new_name   



Thanks to satya for this tip on the forum.


A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
This document, titled « DOS script to rename files », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).