Link to home
Start Free TrialLog in
Avatar of benc007
benc007Flag for United States of America

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?
Avatar of winthropj
winthropj

Not DOS but will work.
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.
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)
					'newFileName =  Left(objFile.Name,(objFile.name.Len - 14))
					objFile.Name = newName
    			Next

Open in new window

I don't think DOS can do it. It's fairly stupid about long filenames and special characters.
Avatar of benc007

ASKER

winthropj, can you explain how your code works?

I saved file.vbs and changed the path.  What do I do now?
Avatar of benc007

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


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!"
)

Open in new window

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!"
)

Open in new window

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!"
)

Open in new window

Avatar of benc007

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
Avatar of benc007

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!"
)

Open in new window

Avatar of benc007

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

ASKER CERTIFIED SOLUTION
Avatar of devil_himself
devil_himself
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I stripped that off and meant to add it back but forgot that step.
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

Open in new window

Avatar of benc007

ASKER

Thank you for your help.