Solved

Dos batch script to rename a file

Posted on 2011-02-23
4
598 Views
Last Modified: 2012-06-27
I am new to scripting and I need a solution to rename the following files.
We receive files from our vendors that look something like the ones listed
below.
PO_ordern1_20110223.txt
Emp_dir1_20110223.tx

I want to prepend "bcgsndr" to each of these files name.  For example,
bcgsndr_PO_ordern1_20110223.txt
bcgsndr_Emp_dir1_20110223.txt

How can I do this with a script.

Thanks
0
Comment
Question by:SyFy
4 Comments
 
LVL 12

Accepted Solution

by:
Amick earned 250 total points
ID: 34967323
for %i in (dir *.txt) do ren %i bcbg_%i
0
 
LVL 8

Expert Comment

by:jimsweb
ID: 34967325
Try thsi one:

@echo off 
ren ???????????????????.txt  bcgsndr???????????????????.txt
ren ?????????????????.txt  bcgsndr?????????????????.txt

Open in new window


Hope this also is helful to you:
http://en.kioskea.net/faq/6533-bat-command-to-insert-a-char-in-a-file-name
0
 
LVL 6

Assisted Solution

by:dax_bad
dax_bad earned 250 total points
ID: 34968150
In case you prefer using vbscript, this should do the trick

Change the path & save the snippet as .vbs
path = "" 'ie. C:\Temp:\FTP 
pattern1 = "PO_" 'first pattern to look for in a file
pattern2 = "Emp_" 'Second pattern to look for in a file
addString = "bcgsndr_"

Set objFS = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFS.GetFolder(path)

Set objRE1 = new RegExp
Set objRE2 = new RegExp
objRE1.Pattern = pattern1
objRE2.Pattern = pattern2
objRE1.IgnoreCase = True
objRE2.IgnoreCase = True

For Each objFile In objFolder.Files
	If objRE1.Test(objFile.Name) Then
   			objFile.Name = addString + objFile.Name
   	Else 
		If objRE2.Test(objFile.Name) Then
    			objFile.Name = addString + objFile.Name
   		End If
	End If
Next

Open in new window

0
 

Author Closing Comment

by:SyFy
ID: 34970231
Thanks Guys !
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now