I do not really want to move the files I just need to rename them. If it would work just to rename them can you show me how you would use it in my loop? I tried and I am not getting the results I want.
Main Topics
Browse All TopicsHello,
I am newer to vbscript and need some guidance. I basically have some files that I need to rename. I so far have this which loops through all the files in my directory but I do not know what I need to do to change their filenames. I wanted to use some string functions like len and left/right etc to shorten the filename to what I desire and then save it as the same file type. The files are all .xls and and example would be
9898 - John Doe - 58 - convertedV3.2.xls
and I just want to rename the file as
9898 - John Doe.xls
Set fso = CreateObject("Scripting.Fi
Set f = fso.GetFolder("C:\mydir")
For Each file In f.Files
Wscript.Echo file.Name
Next
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
How about:
Dim fso,f,pos,arrFil
Set fso = CreateObject("Scripting.Fi
Set f = fso.GetFolder("C:\mydir")
For Each file In f.Files
pos = Instr(file.Name,"-")
if pos > 0 Then
pos = pos + 1
if Instr(pos,file.Name,"-") > 0 Then
arrFil = Split(file.Name,"-")
fso.MoveFile "C:\mydir\" & file.Name, "C:\mydir\" & arrFil(0) & " - " & arrFil(1) & ".xls"
end if
end if
Next
Set fso = Nothing
Hi,
The way roonaan described is how you rename files. There are two methods in the fso object on is copyfile which retains the old file and the other is movefile which deletes the originating file after moving it (renaming). Here is how I would do a loop. In this code you define the folder you would like to rename files in...you can even define what file extension should be renamed.
Dim objFSO
Set objFSO = CreateObject("Scripting.Fi
Dim strCurrentFolder
strCurrentFolder = "c:\source folder"
Dim objFolder
Set objFolder = objFSO.GetFolder(strCurren
ListDirectory objFolder
Sub ListDirectory(objFolder)
Dim objFile
Dim objSubFolder
For Each objFile In objFolder.Files
If UCase(Right(objFile.Path, 3)) = "PGP" Then
objFSO.MoveFile objFile.Path, strcurrentfolder & NewFileName
wscript.quit
End If
Next
End Sub
Business Accounts
Answer for Membership
by: RoonaanPosted on 2005-02-10 at 08:21:06ID: 13276978
Try:
fso.MoveFile oldFileName, newFileName
-r-