[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How To Rename Multiple Files

Posted on 2009-02-13
16
Medium Priority
?
419 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?
0
Comment
Question by:benc007
  • 6
  • 5
  • 2
  • +2
16 Comments
 
LVL 5

Expert Comment

by:winthropj
ID: 23639082
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

0
 
LVL 32

Expert Comment

by:_
ID: 23639227
0
 
LVL 32

Expert Comment

by:_
ID: 23639238
I don't think DOS can do it. It's fairly stupid about long filenames and special characters.
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:benc007
ID: 23639388
winthropj, can you explain how your code works?

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

Author Comment

by:benc007
ID: 23639399
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


0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23639490
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

0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23639499
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

0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23639502
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

0
 

Author Comment

by:benc007
ID: 23639623
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
0
 

Author Comment

by:benc007
ID: 23639633
winthropj - I think fileSystemObject is part of the answer but I don't know how to keep the extensions of the original files.
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23639750
Mine is a batch file. So the extension is .bat or .cmd
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23639759
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

0
 

Author Comment

by:benc007
ID: 23640126
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

0
 
LVL 8

Accepted Solution

by:
devil_himself earned 200 total points
ID: 23640230
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

0
 
LVL 5

Expert Comment

by:winthropj
ID: 23640406
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

0
 

Author Closing Comment

by:benc007
ID: 31546831
Thank you for your help.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month19 days, 2 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question