Solved

Batch REN command

Posted on 2004-09-14
11
10,489 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
Comment Utility
What operating system?
Try

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

0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
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
Comment Utility
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
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:paulfryer
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Aha - long file names will do it every time... : )
Glad you resolved it. Thanx
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

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…
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

9 Experts available now in Live!

Get 1:1 Help Now