I have a macro that renames files by removing unwanted characters (to make the file name SharePoint compliant) in a folder and its subfolders. However, if after removing those characters and making some other changes to the file name, that filename then matches an existing filename in the folder, the script breaks. I would like the script to add the number 1 (or 2, 3, 4 etc.) to the end of a file (before the extension) if it turns out that that new filename already exists in the folder.
For example, the modified filename after running the script to delete characters is now "happy_presentation.ppt". However, it can't save this file because that name already exists in the folder so it will instead name it "happy_presentation1.ppt". If that filename also already exists in the folder...then it would name it "happy_presentation2.ppt" instead. Is this possible?
Here is the current script:
Set objFSO = CreateObject("Scripting.FileSystemObject")
nCount = 0
currentDirectory = Left(WScript.ScriptFullName,(Len(WScript.ScriptFullName)) - (Len(WScript.ScriptName)))
Set rootFolder = objFSO.GetFolder(currentDirectory)
enumFolders rootFolder
wscript.echo nCount & " Files have been renamed."
Function enumFolders(folder)
renameFiles folder
For Each sFolder in folder.subfolders
enumFolders sFolder
Next
End Function
Function renameFiles(nFolder)
For Each oFile in nFolder.files
newFileName = oFile.name
If InStr(newFileName,"#") > 0 Then newFileName = Replace(newFileName,"#","_")
If InStr(newFileName," ") > 0 Then newFileName = Replace(newFileName," ","_")
If InStr(newFileName,"&") > 0 Then newFileName = Replace(newFileName,"&","_")
If InStr(newFileName,"~") > 0 Then newFileName = Replace(newFileName,"~","_")
If InStr(newFileName,"\") > 0 Then newFileName = Replace(newFileName,"\","_")
If InStr(newFileName,"/") > 0 Then newFileName = Replace(newFileName,"/","_")
If InStr(newFileName,":") > 0 Then newFileName = Replace(newFileName,":","_")
If InStr(newFileName,"*") > 0 Then newFileName = Replace(newFileName,"*","_")
If InStr(newFileName,"?") > 0 Then newFileName = Replace(newFileName,"?","_")
If InStr(newFileName,Chr(34)) > 0 Then newFileName = Replace(newFileName,Chr(34),"_")
If InStr(newFileName,"<") > 0 Then newFileName = Replace(zFile.name,"<","_")
If InStr(newFileName,">") > 0 Then newFileName = Replace(newFileName,">","_")
If InStr(newFileName,"|") > 0 Then newFileName = Replace(newFileName,"|","_")
If InStr(newFileName,"{") > 0 Then newFileName = Replace(newFileName,"{","_")
If InStr(newFileName,"}") > 0 Then newFileName = Replace(newFileName,"}","_")
If InStr(newFileName,"%") > 0 Then newFileName = Replace(newFileName,"%","_")
If InStr(newFileName,"+") > 0 Then newFileName = Replace(newFileName,"+","_")
If InStr(newFileName,"__") > 0 Then
Do until InStr(newFileName ,"__") = 0
newFileName = Replace(newFileName,"__","_")
Loop
End If
If Left(newFileName, 1) = "_" Then newFileName = Mid(newFileName,2)
If newFileName <> oFile.name Then
nCount = nCount + 1
newFilePath = Left(oFile.path,Len(oFile.path) - Len(oFile.name)) & newFileName
objFSO.MoveFile oFile.path, newFilePath
End If
Next
End Function
For example, the modified filename after running the script to delete characters is now "happy_presentation.ppt". However, it can't save this file because that name already exists in the folder so it will instead name it "happy_presentation1.ppt". If that filename also already exists in the folder...then it would name it "happy_presentation2.ppt" instead. Is this possible?
Here is the current script:
Set objFSO = CreateObject("Scripting.FileSystemObject")
nCount = 0
currentDirectory = Left(WScript.ScriptFullName,(Len(WScript.ScriptFullName)) - (Len(WScript.ScriptName)))
Set rootFolder = objFSO.GetFolder(currentDirectory)
enumFolders rootFolder
wscript.echo nCount & " Files have been renamed."
Function enumFolders(folder)
renameFiles folder
For Each sFolder in folder.subfolders
enumFolders sFolder
Next
End Function
Function renameFiles(nFolder)
For Each oFile in nFolder.files
newFileName = oFile.name
If InStr(newFileName,"#") > 0 Then newFileName = Replace(newFileName,"#","_")
If InStr(newFileName," ") > 0 Then newFileName = Replace(newFileName," ","_")
If InStr(newFileName,"&") > 0 Then newFileName = Replace(newFileName,"&","_")
If InStr(newFileName,"~") > 0 Then newFileName = Replace(newFileName,"~","_")
If InStr(newFileName,"\") > 0 Then newFileName = Replace(newFileName,"\","_")
If InStr(newFileName,"/") > 0 Then newFileName = Replace(newFileName,"/","_")
If InStr(newFileName,":") > 0 Then newFileName = Replace(newFileName,":","_")
If InStr(newFileName,"*") > 0 Then newFileName = Replace(newFileName,"*","_")
If InStr(newFileName,"?") > 0 Then newFileName = Replace(newFileName,"?","_")
If InStr(newFileName,Chr(34)) > 0 Then newFileName = Replace(newFileName,Chr(34),"_")
If InStr(newFileName,"<") > 0 Then newFileName = Replace(zFile.name,"<","_")
If InStr(newFileName,">") > 0 Then newFileName = Replace(newFileName,">","_")
If InStr(newFileName,"|") > 0 Then newFileName = Replace(newFileName,"|","_")
If InStr(newFileName,"{") > 0 Then newFileName = Replace(newFileName,"{","_")
If InStr(newFileName,"}") > 0 Then newFileName = Replace(newFileName,"}","_")
If InStr(newFileName,"%") > 0 Then newFileName = Replace(newFileName,"%","_")
If InStr(newFileName,"+") > 0 Then newFileName = Replace(newFileName,"+","_")
If InStr(newFileName,"__") > 0 Then
Do until InStr(newFileName ,"__") = 0
newFileName = Replace(newFileName,"__","_")
Loop
End If
If Left(newFileName, 1) = "_" Then newFileName = Mid(newFileName,2)
If newFileName <> oFile.name Then
nCount = nCount + 1
newFilePath = Left(oFile.path,Len(oFile.path) - Len(oFile.name)) & newFileName
objFSO.MoveFile oFile.path, newFilePath
End If
Next
End Function