user2849978
user2849978

Reputation: 1

Dos batch file - renaming some files

I only know of the basic command lines in DOS. I've heard that there is a variable you can use to rename files in a directory. What I have is the following:

C:\Temp\874540_MyVacation1_x.jpg

C:\Temp\451157_MyVacation2_x.jpg

C:\Temp\874211_MyVacation3_x.jpg

C:\Temp\652120_MyVacation4_x.jpg

C:\Temp\541547_MyVacation5_x.jpg

C:\Temp\321778_MyVacation6_x.jpg

I'm trying to get rid of the first 7 characters, and replace the x with bz. So that it looks like this:

C:\Temp\MyVacation1_bz.jpg

C:\Temp\MyVacation2_bz.jpg

C:\Temp\MyVacation3_bz.jpg

C:\Temp\MyVacation4_bz.jpg

C:\Temp\MyVacation5_bz.jpg

C:\Temp\MyVacation6_bz.jpg

I'm sure there's a lot of windows based (freeware) applications that can rename several files at once. I'm just trying to improve my DOS command knowledge.

I know this is really awful - but can someone point me in the right direction here?

@ echo
cd\
c:
cd temp
ren "%[1-9]%_MyVacation%_x.jpg"  ????

Upvotes: 0

Views: 134

Answers (2)

dbenham
dbenham

Reputation: 130819

Here is one way to do it.

@echo off
cd /d c:\temp
for /f "delims=" %%F in ('dir /b /a-d^|findstr /rx "[0-9]*_MyVacation[0-9]*_x\.jpg") do (
  for /f "delims=_ tokens=2" %A in ("%%F") do ren "%%F" "%%A_bz.jpg"
)

Upvotes: 2

Endoro
Endoro

Reputation: 37569

solution with sed for Windows:

cd /d C:\TEMP
dir /b *.jpg|sed -nr "s/^[0-9]+_([[:alnum:]_]+)x.jpg/ren \"^&\" \"\1bz.jpg\"/ie"

..output example:

C:\Temp>DIR /B *.JPG
451157_MyVacation2_cindy_x.jpg
451157_MyVacation2_x.jpg
874211_MyVacation3_Terri_x.jpg
874211_MyVacation3_x.jpg
874540_MyVacation1_bob_x.jpg
874540_MyVacation1_x.jpg

C:\Temp>dir /b *.jpg|sed -nr "s/^[0-9]+_([[:alnum:]_]+)x.jpg/ren \"^&\" \"\1bz.jpg\"/ie"

C:\Temp>DIR /B *.JPG
MyVacation1_bob_bz.jpg
MyVacation1_bz.jpg
MyVacation2_bz.jpg
MyVacation2_cindy_bz.jpg
MyVacation3_bz.jpg
MyVacation3_Terri_bz.jpg

Upvotes: 0

Related Questions