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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Renaming a bunch of files 1

Status
Not open for further replies.

ylustrata

Technical User
Aug 13, 2002
80
US
Please help me with this little script for adding the artist's name to files in a folder:

Option Explicit
dim objFSO
dim objFolder
dim objFileAny
dim strDirectory
dim strFileID
dim strFileName
dim strExtension
dim strArtist
dim strNewName
dim strMsg

strArtist = InputBox("Enter Artist")
strDirectory = InputBox("Enter Folder Path")

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strDirectory)

strMsg = "New file names:" & vbNewLine
For Each objFileAny In objFolder.Files
strFileID = strDirectory & "\" & objFileAny.Name
strFileName = objFSO.GetBaseName(strFileID)
strExtension = objFSO.GetExtensionName(strFileID)
strNewName = strFileName & " - " & strArtist
strNewName = strNewName & "." & strExtension
objFileAny.Name = strNewName
strMsg = strMsg & strNewName & vbNewLine
Next

MsgBox strMsg


When I run the script commenting out "objFileAny.Name = strNewName" I get the results I want. When I put that line of code in SOME files seem to get renamed twice i.e. the artist's name is added twice e.g
"A Man Alone - Frank Sinatra - Frank Sinatra.wav
 
Hello ylustrata,

The file collection unfortunately seems not support item indexing. I would suggest you add this.
Code:
dim af(), i
redim af(objFolder.Files.count-1)
i=0
for each objFileAny in objFolder.Files
   af(i)=objFileAny.Name : i=i+1
next
Then the rest changes to process the array instead of collection.
Code:
strMsg = "New file names:" & vbNewLine
For i=0 to ubound(af)
    strFileID = strDirectory & "\" & af(i)
    strFileName = objFSO.GetBaseName(strFileID)
    strExtension = objFSO.GetExtensionName(strFileID)
    strNewName = strFileName & " - " & strArtist
    strNewName = strNewName & "." & strExtension 
    set objFileAny = objFSO.GetFile(strFileID)
    objFileAny.Name = strNewName
    set objFileAny = nothing
    strMsg = strMsg & strNewName & vbNewLine
Next

MsgBox strMsg
regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top