Solved

Dos batch script to rename a file

Posted on 2011-02-23
4
600 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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