Solved

Renaming a filename

Posted on 2012-12-22
9
384 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 54

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 54

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 54

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Bulk File Rename 3 128
find and replace 5 51
Robocopy command line 6 69
Seatools For Dos (Just shows FreeDos) 21 78
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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

685 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