Solved

Batch REN command

Posted on 2004-09-14
11
10,494 Views
Last Modified: 2010-05-18
I have a bunch of files in a folder that need to all be renamed to include "1_" in front of them.

I need to run a RENAME command that will take each filename in a folder and rename it to the same name it currently is and place a number with an underscore in front of it. like so:

Old File Name: myfile.txt
New File Name: 1_myfile.txt

It should do this for every file in the folder.

I have already tried

REN C:\TEST\* 1_*

But that seems to take the first two characters of the filename and replace them with "1_". So the file ends up looking like this "1_file.txt", notice the "my" has been replaced with "1_". I need to keep the existing name after placeing the "1_" in front of it.

Any help is greatly appriciated.

Paul
0
Comment
Question by:paulfryer
  • 5
  • 4
  • 2
11 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 12056992
What operating system?
Try

@echo off
For %%a in (*.*) do ren %%a 1_%%a

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 12057010
You'll need to include a line before the FOR command to change to the required folder, or else, the current folder will have this implemented...

You can also select just txt files if you want by changing *.* to *.txt
0
 
LVL 1

Author Comment

by:paulfryer
ID: 12057151
I will be doing this on a win2000 terminal server.

Also, there is one other little issue; some of the files don't have file extensions. The files are uploads by web users and some people just don't know how to create documents. Example some people uploaded files named similar to "myresume" with no period and file extension.

I need to rename a lot of different file types (pdf, doc, txt, rtf, more..).

If I am using @echo off should I create a .BAT file? If I need to use a .BAT file, is the above code exactly what I should include?

Thanks for the quick response.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 9

Expert Comment

by:DrWarezz
ID: 12057804
Yea, points to sirbounty, to change directory before hand, use 'cd', like so:

cd c:\directory

Use a batch file  (ie;  filename.bat ). And to do several file types, (other than using *.* to do ALL file types), try this:

@echo off
cd c:\directory
set types=pdf;doc;txt;rtf;

for /f "tokens=4 delims=;" %%i in (types) do call :PROCESS %%i
exit

:PROCESS
for %%i in (*.%1) do ren %%i 1_%%i
exit /b

Best of luck :)
[r.D]
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12057823
set types=pdf;doc;txt;rtf;
(as you can see, you seperate each file type with a semicolon, then change the number 4 (tokens=4) to the number of extensions that you put).

:)
0
 
LVL 1

Author Comment

by:paulfryer
ID: 12058116
I tried pasting the code in a file and naming it renamefile.bat, I then went to the command line and executed it, the command window closed but the files where not renamed. Any thoughts?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 12058609
Hmm - I think you can get by without a batch file - wasn't sure if you needed one or not.  To do so without creating a batch file (i.e, only using this one time), remove the additional % from the variables listed above.
The *.* should encompass files without extensions as well.
To perform this maneuver on all files in C:\MyUploads, type the following commands:

C: <Enter>
CD\MyUploads <Enter>
for /f %a in (*.*) do ren %a 1_%a <Enter>
Dir <Enter>
0
 
LVL 1

Author Comment

by:paulfryer
ID: 12059136
I typed this exactly:

for /f %a in (*.*) do ren %a 1_%a

and got an error that said "The system could not find the file *.*." I then tried running the command without the "(' or the ")" and got an error that said "*.* was unexpected at this time."

Am I doing something wrong?
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 12059273
Doh!  My mistake - I was half trying to follow DrWarezz's suggestion and trying to post my original - follow my first post, but without the double %%

for %%a in (*.*) do ren %a 1_%a
0
 
LVL 1

Author Comment

by:paulfryer
ID: 12060184
What ended up doing the trick was:

for %a in (*.*) do ren "%a" "1_%a"

Thanks for the help folks!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 12060369
Aha - long file names will do it every time... : )
Glad you resolved it. Thanx
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
BAT FILE ERROR LOG Question 17 61
Create a specific short cut with a parameter. 2 197
Batch: check service status 11 90
How to Skip in FOR command if a Directory does not exist. 6 79
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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