I'd like to rename a large number of files within a folder on a WinXP system, preferably using a batch file.
The files are currently named like this:
Vacation2010 001.jpg
Vacation2010 002.jpg
Vacation2010 003.jpg
And I'd like to change them to:
December 001.jpg
December 002.jpg
December 003.jpg
How can I perform this operation??
This question is related to
windows
batch-file
rename
cmd
dir /b *.jpg >file.bat
This will give you lines such as:
Vacation2010 001.jpg
Vacation2010 002.jpg
Vacation2010 003.jpg
Edit file.bat in your favorite Windows text-editor, doing the equivalent of:
s/Vacation2010(.+)/rename "&" "December \1"/
That's a regex; many editors support them, but none that come default with Windows (as far as I know). You can also get a command line tool such as sed or perl which can take the exact syntax I have above, after escaping for the command line.
The resulting lines will look like:
rename "Vacation2010 001.jpg" "December 001.jpg"
rename "Vacation2010 002.jpg" "December 002.jpg"
rename "Vacation2010 003.jpg" "December 003.jpg"
You may recognize these lines as rename commands, one per file from the original listing. ;) Run that batch file in cmd.exe.
@ECHO off & SETLOCAL EnableDelayedExpansion
SET "_dir=" REM Must finish with '\'
SET "_ext=jpg"
SET "_toEdit=Vacation2010"
SET "_with=December"
FOR %%f IN ("%_dir%*.%_ext%") DO (
CALL :modifyString "%_toEdit%" "%_with%" "%%~Nf" fileName
RENAME "%%f" "!fileName!%%~Xf"
)
GOTO end
:modifyString what with in toReturn
SET "__in=%~3"
SET "__in=!__in:%~1=%~2!"
IF NOT "%~4" == "" (
SET %~4=%__in%
) ELSE (
ECHO %__in%
)
EXIT /B
:end
This script allows you to change the name of all the files that contain Vacation2010
with the same name, but with December
instead of Vacation2010
.
If you copy and paste the code, you have to save the .bat
in the same folder of the photos.
If you want to save the script in another directory [E.G. you have a favorite folder for the utilities] you have to change the value of _dir
with the path of the photos.
If you have to do the same work for other photos [or others files changig _ext
] you have to change the value of _toEdit
with the string you want to change [or erase] and the value of _with
with the string you want to put instead of _toEdit
[SET "_with="
if you simply want to erase the string specified in _toEdit
].
you can do this easily without manual editing or using fancy text editors. Here's a vbscript.
Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder="c:\test"
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
If objFS.GetExtensionName(strFile) = "jpg" Then
strFileName = strFile.Name
If InStr(strFileName,"Vacation2010") > 0 Then
strNewFileName = Replace(strFileName,"Vacation2010","December")
strFile.Name = strNewFileName
End If
End If
Next
save as myscript.vbs and
C:\test> cscript //nologo myscript.vbs
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=Vacation2010
SET new=December
for /f "tokens=*" %%f in ('dir /b *.jpg') do (
SET newname=%%f
SET newname=!newname:%old%=%new%!
move "%%f" "!newname!"
)
What this does is it loops over all .jpg files in the folder where the batch file is located and replaces the Vacation2010 with December inside the filenames.
You don't need a batch file, just do this from powershell :
powershell -C "gci | % {rni $_.Name ($_.Name -replace 'Vacation2010', 'December')}"
Source: Stackoverflow.com