Solved

Batch REN command

Posted on 2004-09-14
11
10,496 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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
Batch file to delete a folder 5 72
IBM T20 - can DOS and Win95 be installed and fully working? 29 177
Automated script task to send email 22 100
how to get the largest file size? 33 61
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

838 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