Code Script .co.uk

Home | Scripts | Tutorials | Disclaimer | Sitemap | Contact

   Rename Files Find And Replace Text
 

Vbscript / Files And Folders / Rename Files Find And Replace Text

Find and Replace text in filenames. Operates on all files in a folder and all subfolders.


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)
'deletes = defaultStrings & "," & delete

If deletes = "" Then
Wscript.Quit
End If

  Set objFolder = objFSO.GetFolder(targetPath)

fileRename objFolder

Sub fileRename(folder)

  'Loop through the files in the folder
  For Each objFile In folder.Files

filename = objFile.Name
ext = objFSO.getExtensionName(objFile)
safename = Left(filename, Len(filename) - Len(ext) - 1)

'General Name Operations
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

'Replace specified string (Once, case insensitive)
If delString <> "" Then
safename = Replace(safename, delString,"",1,1,1)
End If

Next

If Right(safename,1) = "-" Then
'WScript.Echo Left(safename,Len(safename)-1)
safename = Left(safename,Len(safename)-1)
End If

safename = trim(safename)

On Error Resume Next

'Only rename if new name is different to original name
If filename <> safename & "." & ext Then

'Uncomment the line below to prompt before each rename (kill the process to exit)
'Wscript.Echo "Renaming "  & vbCRLF & objFile.Name & " to " & vbCRLF & safename & "." & ext

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

'Uncomment lines below to act on files in all subfolders
'For Each Subfolder In folder.SubFolders
'fileRename Subfolder
'Next

End Sub

Wscript.echo "Done"





Please note that a disclaimer applies to any code on this page.
 
   Actions
  Go Back
  Bookmark
  Print Page


   Menu
 
- Links
- Reference
- Script Editors
- Tutorials
- Vbscript
     - Active Directory
     - Exchange
     - Files And Folders
          - Auto Copy Files From Cdrom
          - Backup My Documents
          - Cleanup Filenames
          - Create Remote File Share
          - File Properties
          - File Server Report
          - File Type Report Scan
          - Find And Replace Filename Text
          - Find And Replace Text In Files
          - Folder Size Scanner
          - Rename Files Find And Remove Text
          - Rename Files Find And Replace Text
          - Replace Folder Ntfs Security Permissions
          - Simple File Report
          - Sort And Move Files To Folders
          - Windows File Locations
     - General
     - Ms Office
     - Operating System
     - Processes And Services
     - Text Processing
     - User Interaction
     - Web Servers