Like a lot of you, I take a lot of pictures with my cell phone which I like to upload to my desktop computer. Besides pictures from the cell phone, I also have a Canon PowerShot camera that I like to use as well. When I upload the pictures, I like to have easier control of how the files are named for sharing purposes. Below is a VBScript I use to rename an entire folder's worth of pictures. For ease-of-use, I use textboxes to enter the folder path and the filename prefix. The script then adds unique numbers, with leading zeroes, to preserve the current sort. It's written to handle up to 9999 files in a folder, which for all practical purposes, is enough.
Option Explicit
Dim folderPath
Dim filePrefix
Dim FSO
Dim FLD
Dim file
Dim oldFilename
Dim newFilename
Dim LeadingZeroString
Dim count
Dim uniqueNumber
On Error Resume Next
folderPath = InputBox("Enter the folder path where your files are located:")
filePrefix = InputBox("Enter the new filename prefix:")
' path needs to end with a path separator
If Right(folderPath,1) <> "\" Then
folderPath = folderPath & "\"
End If
' Replace any characters, illegal or otherwise.
filePrefix = Replace(Replace(filePrefix," ","_"),"\","_")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FLD = FSO.GetFolder(folderPath)
If Err.Number < 1 Then
' set the correct # of leading zeros based on number of files
If FLD.Files.Count > 999 Then
LeadingZeroString = "0000"
Elseif FLD.Files.Count > 99 Then
LeadingZeroString = "000"
Else
LeadingZeroString = "00"
End If
'loop through the file collection, renaming files
For Each file in FLD.Files
oldFilename = file.Path
count = count + 1
uniqueNumber = Right(LeadingZeroString & CStr(count), Len(LeadingZeroString))
newFilename = folderPath & filePrefix & "_" & uniqueNumber & "." & FSO.GetExtensionName(oldFilename)
'rename the file
FSO.MoveFile oldFilename, newFilename
Next
Set FLD = Nothing
Set FSO = Nothing
Else
MsgBox (Err.Description)
End If
No comments:
Post a Comment