English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Ok, I need to write a batch file to rename all files in a folder.
Called: 1.jpg_s.jpg 2.jpg_s.jpg and so on...
I just want to rename these to: 1.jpg 2.jpg 3.jpg... etc
possibly by using a 'for' loop becuase there is no way of telling the total files to rename.

2007-11-30 10:37:56 · 2 answers · asked by JJ 1 in Computers & Internet Programming & Design

(HAS to be a batch file)

2007-11-30 10:46:34 · update #1

2 answers

You can use the 'for' command to read in the .jpg files using the 'dir' command. Using a period as the delimiter, you can split apart the names, then reassemble them properly with a rename command. One line will do it,

from a commandline (this will wrap on Yahoo, it's just one line)

for /f "tokens=1-3 delims=." %i in ('dir *.jpg /b') do ren %i.%j.%k %i.%k

from a batch file you will need to double up the % signs, like this,

for /f "tokens=1-3 delims=." %%i in ('dir *.jpg /b') do ren %%i.%%j.%%k %%i.%%k

Notes:
1) It would be easiest to run the commandline or batch in the same directory as the files. Or, you would have to add the full path to the "dir" and then to the "ren"s

2) Test how the rename command will _look_ first by adding an @echo command to the "do". Like this, from a commandline,

for /f "tokens=1-3 delims=." %i in ('dir *.jpg /b') do @echo ren %i.%j.%k %i.%k

3) This will run on _every_ jpg file in the directory so take care before removing that "@echo" from your test.

Explanation,
I'm using "for /f" to read in the contents of the command "dir *.jpg /b" ... a bare listing of the jpg filenames.

I'm using the "." as a delimiter to split the filename up. So "1.jpg_s.jpg" becomes "1 jpg_s jpg" and it assigns variables(%i %j and %k) to each of the three sections or "tokens".

Finally, I specifiy
ren %i.%j.%k %i.%k

Which is removing the %j token and basically says,
rename 1.jpg_s.jpg to 1.jpg

2007-11-30 11:20:07 · answer #1 · answered by Kevin 7 · 7 0

Is this homework? You can actually use Renamer (really powerful)

http://www.snapfiles.com/get/denrenamer.html

2007-11-30 18:41:36 · answer #2 · answered by fretless 4 · 0 0

fedest.com, questions and answers