Reputation: 1331
I have a folder full of images which have a format filename.com_XXX_IMG_000.jpg. The issue comes because the file name has a .com in it, it confuses the the software that I am using to upload it to a webspace.
I need to create a batch file that gets all the images in a folder and renames all of them from filename.com_XXX_IMG_000.jpg => filename_XXX_IMG_000.jpg.
Any help would be greatful, thanks in advance.
Upvotes: 0
Views: 1453
Reputation: 338158
Since you want to do it in a batch file:
@echo off
for /f "delims=. tokens=1,2,3" %%f in ('dir /b *.jpg') do (
setlocal enabledelayedexpansion
set part=%%g
if "!part:~0,3!"=="com" (
set oldname=%%f.%%g.%%h
set newname=%%f.!part:~4!.%%h
echo "!oldname!" -^> "!newname!"
ren "!oldname!" "!newname!"
)
endlocal
)
A few notes
for
loop variables are single letter, like this: %f
%
must be escaped, so %f
becomes %%f
delims=.
splits the filenames at the .
, in your case into three partstokens=1,2,3
returns three variables containing the individual name parts (%f
, %g
and %h
)enabledelayedexpansion
switches on dynamic variable handling%foo:~0,3%
returns the first three characters of %foo%
.!
instead of %
>
must be escaped or echo
won't print it, hence ^>
Upvotes: 1
Reputation: 29339
read HELP FOR
and try the following....
@echo off
setlocal enabledelayedexpansion
for %%a in (*.jpg) do (
set fn=%%~na
set fn=!fn:.com=!
echo REN "%%a" "!fn!.jpg"
)
you need to enable delayed expansion because you need to expand a variable inside the for loop.
the loops iterates over all the jpg files in the current directory and for each file it extracts its filename using the ~n
syntax, and then it removes all the occurences of .com
by replacing them with an empty string. Read HELP SET
after careful testing, remove the echo
command
Upvotes: 1