benc007
asked on
How To Rename Multiple Files
I have a bunch of files in the same directory with the following names:
fsaffsa-abc-def_1.jpg
dsfsdfdsfasfdsf--abc-def_1 .jpg
324dssa-abc-def_1.jpg
dfasf23423-abc-def_1.jpg
I would like to remove -abc-def_1 from the filenames for all of these files in windows DOS. How can I do this?
fsaffsa-abc-def_1.jpg
dsfsdfdsfasfdsf--abc-def_1
324dssa-abc-def_1.jpg
dfasf23423-abc-def_1.jpg
I would like to remove -abc-def_1 from the filenames for all of these files in windows DOS. How can I do this?
See if Renamer will do what you need
http://www.download3k.com/Web-Authoring/Reference-Tutorials/Download-Renamer-random.html
http://www.download3k.com/Web-Authoring/Reference-Tutorials/Download-Renamer-random.html
I don't think DOS can do it. It's fairly stupid about long filenames and special characters.
ASKER
winthropj, can you explain how your code works?
I saved file.vbs and changed the path. What do I do now?
I saved file.vbs and changed the path. What do I do now?
ASKER
winthropj, I understand your code now and it seems to work except there is an extension problem. I need to keep the existing file extension. How do I do this?
eg.
fsaffsa-abc-def_1.jpg -> fsaffsa.jpg
dsfsdfdsfasfdsf-abc-def_1. jpg -> dsfsdfdsfasfdsf.jpg
324dssa-abc-def_1.jpg -> 324dssa.jpg
234324s-abc-def_1.gif- > 234324s.gif
dfgadfsdfefaads-abc-def_1. gif -> dfgadfsdfefaads.gif
sdfsdafdsaf-abc-def_1.bmp- > sdfsdafdsaf.bmp
eg.
fsaffsa-abc-def_1.jpg -> fsaffsa.jpg
dsfsdfdsfasfdsf-abc-def_1.
324dssa-abc-def_1.jpg -> 324dssa.jpg
234324s-abc-def_1.gif- > 234324s.gif
dfgadfsdfefaads-abc-def_1.
sdfsdafdsaf-abc-def_1.bmp-
Give this a try.
SETLOCAL ENABLEDELAYEDEXPANSION
Set folder=C:\Folder
for /f %%a in ('dir /a-d /b "%Folder%\*-abc-def_1.jpg"') do (
NewFile=%%~na
NewFile=!NewFile:~0,-10!%%~xa
ren "%Folder%\%%a" "!NewFile!"
)
Or if you want something a little more dynamic.
SETLOCAL ENABLEDELAYEDEXPANSION
Set folder=C:\Folder
Set RemoveFromFilename=-abc-def_1
for /f "tokens=*" %%a in ('dir /a-d /b "%Folder%\*-abc-def_1*.*"') do (
NewFile=%%~na
NewFile=!NewFile:%RemoveFromFilename%=!%%~xa
ren "%Folder%\%%a" "!NewFile!"
)
Opps. We should put in the Tokens=* just in case some filenames have spaces for the first example too.
SETLOCAL ENABLEDELAYEDEXPANSION
Set folder=C:\Folder
for /f "tokens=*" %%a in ('dir /a-d /b "%Folder%\*-abc-def_1.jpg"') do (
NewFile=%%~na
NewFile=!NewFile:~0,-10!%%~xa
ren "%Folder%\%%a" "!NewFile!"
)
ASKER
AmazingTech - I am getting a compilatiion error for your lastest script:
Line: 2
Char: 4
Error: Expected statement
Code: 800A0400
Source: Microsoft VBScript compilation error
I am also not sure how your code will work if it hardcodes all files to .jpg
Line: 2
Char: 4
Error: Expected statement
Code: 800A0400
Source: Microsoft VBScript compilation error
I am also not sure how your code will work if it hardcodes all files to .jpg
ASKER
winthropj - I think fileSystemObject is part of the answer but I don't know how to keep the extensions of the original files.
Mine is a batch file. So the extension is .bat or .cmd
Try this one.
SETLOCAL ENABLEDELAYEDEXPANSION
Set folder=C:\Folder
Set RemoveFromFilename=-abc-def_1
for /f "tokens=*" %%a in ('dir /a-d /b "%Folder%\*-abc-def_1*.*"') do (
NewFile=%%~na
NewFile=!NewFile:%RemoveFromFilename%=!%%~xa
ren "%Folder%\%%a" "!NewFile!"
)
ASKER
I put your code into a .bat file and the images are in C:\Folder, and I am still getting an error:
ren "C:\Folder\%a" "!NewFile!">
File Not Found
ren "C:\Folder\%a" "!NewFile!">
File Not Found
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I stripped that off and meant to add it back but forgot that step.
They this.
They this.
Const strSourceFolderName = "C:\Test"
Set objFSO = CreateObject("Scripting.fileSystemObject")
Set objFolder = objFSO.GetFolder(strSourceFolderName)
For Each objFile in objFolder.Files
name = objFile.Name
num = Len(name) - 14
newName = Left(name,num) & ".jpg"
objFile.Name = newName
Next
ASKER
Thank you for your help.
Paste into notepad and save with .vbs extension. Use quotes when saving "file.vbs"
Change the path in the first line to point to your folder.
Open in new window