Vbscript / Files And Folders / Rename Files Find And Remove Text
This script will rename all files in a folder and all subfolders to remove the specified text strings. The target folder defaults to the script location and both target folder and text strings to be replaced can be specified before operations with the scipt onscreen prompt.
Dim objFSO, objFolder, objFile, filename, char, nchar, pchar Set objFSO = CreateObject("Scripting.FileSystemObject") defaultStrings = "--,- -,- -" current = objFSO.getAbsolutePathName(".") targetPath = InputBox("Enter the path of the target folder:","Target Folder",current) If targetPath = "" Then Wscript.Quit End If deletes = InputBox("Add strings to delete. Separate with a comma (,) and do not add spaces unless intended.","Strings to Delete", defaultStrings)
If deletes = "" Then Wscript.Quit End If Set objFolder = objFSO.GetFolder(targetPath) fileRename objFolder Sub fileRename(folder) For Each objFile In folder.Files
filename = objFile.Name ext = objFSO.getExtensionName(objFile) safename = Left(filename, Len(filename) - Len(ext) - 1) safename = Replace(safename, "."," ") safename = Replace(safename, "_"," ") safename = Replace(safename, "[","- ") safename = Replace(safename, "]"," -") safename = Replace(safename, "(","- ") safename = Replace(safename, ")"," -")
delStrings = Split(deletes, ",") For Each delString in delStrings If delString <> "" Then safename = Replace(safename, delString,"",1,1,1) End If Next If Right(safename,1) = "-" Then safename = Left(safename,Len(safename)-1) End If safename = trim(safename) On Error Resume Next If filename <> safename & "." & ext Then objFSO.MoveFile objFile.Path, objFile.ParentFolder.Path & "" & safename & "." & ext End If If Err.Number <> 0 Then WScript.Echo "Error renaming: " & filename.path & "Error: " & Err.Description Err.Clear End If Next For Each Subfolder In folder.SubFolders fileRename Subfolder Next End Sub Wscript.echo "Done" Add Comment
Add Comment
Please note that a disclaimer applies to any code on this page.
|