Search : in
By :

Dos script to rename files

Last answer on Oct 1, 2009 2:56:02 pm BST gbr00435, on Aug 21, 2008 4:29:20 pm BST 
 Report this message to moderators

Hello,
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

Configuration: Windows XP
Internet Explorer 7.0

Best answers for « dos script to rename files » in :
Renaming multiple files in batch ShowRenaming multiple files in batch Native features Using a third-party program Renaming a large number of files can quickly become tedious. Fortunately, there are tools to automate this task in many cases: Native features On Windows...
[MS-Dos]List the contents of a directory in a file Show[MS-Dos]List the contents of a directory in a file Intro Explanation Intro It may be convenient to make the listing of one or more directories in a file, to make a catalog of files. Under MS-DOS (or Windows by opening a...
Inserting text in a file ShowInserting text in a file Introduction Syntax Introduction It is sometimes useful to insert text in the header of one or more document (s) or in a specific part of the document (before or after a specific line). The publisher of...
Download Rename Master ShowRename Master is a utility which allows you to rename several files or file groups with some clicks. This type of software is very useful for those who have a large collection of music, video or photo to be renamed or for those who must delete or...
MS DOS Files and directories ShowFiles in MS DOS In a computers data is stored in files. When you run a program, MS-DOS processes the data stored in the file and passes it to the system. In MS-DOS a file can be any size, however the file name is more restricted, it can only have...
UNIX - Files ShowIntroduction to UNIX files In UNIX systems any element is represented in the form of a file. All files are architectured around a single tree structure where the base, called the root, is written "/". File types UNIX systems define different...
Introduction to DOS ShowThe development of MS-DOS MS-DOS is the most well known operating system, the most commercialised version is that of Microsoft, christened "MS-DOS" (there are other versions such as DR-DOS). MS-DOS was created in 1981 when it was used on an IBM...

1

Jeff, on Dec 4, 2008 3:32:10 am GMT

Use antrenamer

Reply to Jeff

2

Gibson86, on Dec 22, 2008 10:24:19 am GMT
  • +3

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
:begin
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
pause
goto begin
:exist
echo PATH CONFIRMED
pushd \\%variable%\C$\documents and settings\
goto skipit
:notfound
SET /p userid=[Enter User ID]
:skipit
if exist "\\%variable%\C$\documents and settings\%userid%" goto found
echo ID NOT FOUND... please re-enter
pause
goto notfound
:found
Echo modifying...
rename "%userid%" $"%userid%"$"%XDate%"$
Echo ***SUCCESS***
popd
Echo $%userid%$%XDate%$
pause

Reply to Gibson86

3

satya, on Jan 27, 2009 11:10:46 am GMT
  • +11

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 :))

Reply to satya

5

AP, on Mar 27, 2009 9:13:56 pm GMT
  • +1

Hey Satya,

Thanks for the dos script to rename files.
I was stuck on that, and had to rename a large directory with 5000 files distributed in several subdirectories.

And your script worked just perfect!!!!

Thanks
AP

Reply to AP

10

Harshal, on Jul 14, 2009 6:02:06 am BST

Hi ,

i am trying to use following command for renaming a1.txt, a2.txt ...aN.txt files to a1-v1.xt ...aN-vN.txt
by running
bathc file run
test *.txt *-v1.txt
contents of batch file
For /r %%x in (%1) do ren "%%x" %2

now my files are getting renamed like a1.txt-v1 which i don't want . I want a1-v1.txt
Please can you tell me the dos command for that.

Thanks in advacne

Reply to Harshal

14

 claudiu, on Oct 1, 2009 2:56:02 pm BST
  • +1

Dear all,

I need your help for a bat script.
I have a file called ABC.txt and i want to rename that file to ABC-T.txt
Can anyone help me pls?
Waiting for your answers.




Best regards

Reply to claudiu

4

Sen Hu, on Feb 5, 2009 3:15:02 pm GMT
  • +5

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 repeatitively 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.

Sen


# START OF SCRIPT

# 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 <> "")
do
# 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

# END OF SCRIPT

Reply to Sen Hu

6

ITPERSON, on Apr 20, 2009 9:18:39 am BST

Hi All,

i need to ping an IP for ex.. ping 10.10.10.1

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

:end
ping host



how can i call the result of the ping in the if command !!!?

Reply to ITPERSON

7

SenHu, on Apr 21, 2009 8:47:51 pm BST
  • +2

You can use the following script in biterscripting.

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

Sen

Reply to SenHu

8

erasmus, on Apr 24, 2009 4:50:11 am BST

@Sen,

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?

Cheers,

Reply to erasmus

9

SenHu, on May 1, 2009 3:50:26 pm BST
  • +1

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.

# START SCRIPT
var str output
system ping "10.10.10.1" > $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)
do
# Yes, packets were lost.
# Insert your additional code here.
done
# END SCRIPT

Sen

The above script is in biterscripting ( http://www.biterscripting.com/install.html ) .

Reply to SenHu

11

SenHu, on Jul 14, 2009 4:12:55 pm BST
  • +1

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 <> "")
do
    # 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+"\"")
done



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 http://www.biterscripting.com . It is free. Don't forget to change C:\folder in the script to the correct folder path.

Good luck.

Sen

Reply to SenHu

12

BUGYXXX, on Aug 12, 2009 8:53:43 am BST

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

Reply to BUGYXXX

13

SenHu, on Aug 13, 2009 4:21:35 pm BST

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


BUGYXXX:

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 http://en.kioskea.net/forum/affich-100607-batch-rename-using­-text-file-for-new-name . 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.

Sen

Reply to SenHu