Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help to rename part of file

Status
Not open for further replies.

dport1968

Technical User
Oct 9, 2019
4
US
Good morning all,

I have hundreds of pdf files like V-45X-0892-0.pdf, V-45X-0893-0.pdf, etc...
I would like to strip the -0 on end of every filename to look like V-45X-0892.pdf, V-45X-0893.pdf, etc..

Any help would be greatly appreciated.

Below is what I have but not working:

folderspec = "C:\Dave\"
strRename = ""

set fs = CreateObject("Scripting.FileSystemObject")
set f = fs.GetFolder(folderspec)

for each f1 in f.files
Filename = folderspec & f1.name
NewFilename = replace(Filename, "*-0.pdf" "*.pdf", strRename)
fs.MoveFile Filename, NewFilename
next

msgbox "All Done"

Thank you in advance,
David
 
Something like the below:

Code:
[blue]folderspec = "C:\Dave\"
strRename = ""

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)

For Each f1 In f.Files
    f1.Move fs.BuildPath(f1.ParentFolder, Replace(f1.Name, "-0.", "."))
Next

MsgBox "All Done"[/blue]
 
strongm,

That did the trick! Thank you so much! I really appreciate it.

David
 
Maybe this is faster, processing names and renaming only those files that have to be renamed:
Code:
For Each f1 In f.Files
    Filename = f1.Name
    If InStr(Filename, "-0.pdf") > 0 Then f1.Name = Replace(Filename, "-0.pdf", ".pdf")
Next f1


combo
 
combo, thank you for your input but it's not working. "Expected end of statement" error. Line:4
Char:6
 
Seems that VBscript does not accept counter after Next, so try single [tt]Next[/tt] in last line.

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top