Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Batch REN command

Posted on 2004-09-14
11
Medium Priority
?
10,503 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

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…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

647 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