Solved

Renaming a filename

Posted on 2012-12-22
9
380 Views
Last Modified: 2012-12-27
I have a program that generates bunch of files automatically. i don't have control on the base program. I need to be able to change the names of the files. Here is a sample.

tblEmployee.cs -> Employeedto.cs
tblDept.cs -> Deptdto.cs

I need to remove 'tbl' from the prefix and add 'dto' as the suffix before the file extension.

can someone please provide me the code for this renaming in MS DOC batch file?


Thanks a bunch,
Chandra
0
Comment
Question by:comtec100
  • 3
  • 2
  • 2
9 Comments
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 38716455
Give this a try.

@echo off
Set local enabledelayedexpansion
Pushd c:\yourdir
For %%A in (tbl*.cs) do (
  Set Name=%%~nA
  Ren "%%~A" "!Name:~3!dto.cs"
)
Popd

Open in new window

~bp
0
 

Author Comment

by:comtec100
ID: 38716535
Script:
@echo off
Set local enabledelayedexpansion
Pushd c:\Users\Chandra\temp
  For %%A in (tbl*.cs) do (
  Set Name=%%~nA
  Ren "%%~A" "!Name:~3!dto.cs"
)
Popd


Result:
C:\Users\Chandra\temp>changename.bat
LOCALAPPDATA=C:\Users\Chandra\AppData\Local
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.

Can you please help?
0
 

Author Comment

by:comtec100
ID: 38716962
I've requested that this question be closed as follows:

Accepted answer: 0 points for comtec100's comment #a38716535

for the following reason:

script didn't quite work, but gave the framework. Thank you
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 43

Expert Comment

by:Steve Knight
ID: 38716683
The script would work as is IMO except there is a spurious space on the set local line.  Changed to just setlocal it should be fine.

Steve
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38716956
Sorry for the extra space in setlocal, worked this question on a mobile device and it was nice enough to put in that space on its own, argh.

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38716963
Given the posters final comment, shouldn't http:#a38716455 be accepted as the solution or at least a points scoring assist, in which case the poster should post their modified working version.

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38717663
Agreed give Bill the points, was only a space added!

My mobile seems to auto-complete words really annoyingly and doesn't seem to allow turning it off in a browser text box, you hit a space after a word in a piece of code and it helpfully adds the last bit of text you had after that combination etc.  Must delve a bit for the "off" button on that I suppose!

Steve
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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

22 Experts available now in Live!

Get 1:1 Help Now