We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How To Rename Multiple Files

benc007
benc007 asked
on
Medium Priority
468 Views
Last Modified: 2012-05-06
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?
Comment
Watch Question

CERTIFIED EXPERT

Commented:
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

_

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

Author

Commented:
winthropj, can you explain how your code works?

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

Author

Commented:
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


Top Expert 2009

Commented:
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

Top Expert 2009

Commented:
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

Top Expert 2009

Commented:
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

Author

Commented:
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

Author

Commented:
winthropj - I think fileSystemObject is part of the answer but I don't know how to keep the extensions of the original files.
Top Expert 2009

Commented:
Mine is a batch file. So the extension is .bat or .cmd
Top Expert 2009

Commented:
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

Author

Commented:
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

try this

@echo off 
setLocal enabledelayedexpansion
set src=C:\Source
set rem=-abc-def_1
pushd "%src%"
for /f "tokens=*" %%a in ('dir /b /a-d "*-abc-def_1*.*"') do (
    set nfname=%%~na
    set nfname=!nfname:%rem%=!%%~xa
    ren "%%a" "!nfname!"
)

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT

Commented:
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

Author

Commented:
Thank you for your help.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.