Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10504
  • Last Modified:

Batch REN command

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
paulfryer
Asked:
paulfryer
  • 5
  • 4
  • 2
1 Solution
 
sirbountyCommented:
What operating system?
Try

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

0
 
sirbountyCommented:
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
 
paulfryerAuthor Commented:
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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
DrWarezzCommented:
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
 
DrWarezzCommented:
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
 
paulfryerAuthor Commented:
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
 
sirbountyCommented:
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
 
paulfryerAuthor Commented:
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
 
sirbountyCommented:
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
 
paulfryerAuthor Commented:
What ended up doing the trick was:

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

Thanks for the help folks!
0
 
sirbountyCommented:
Aha - long file names will do it every time... : )
Glad you resolved it. Thanx
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now