Solved

Renaming a filename

Posted on 2012-12-22
9
383 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 53

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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 53

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 53

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

777 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