Code Script .co.uk

Home | Scripts | Tutorials | Disclaimer | Sitemap | Contact

   Rename Files Find And Remove Text
 

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
Your Name:
Comment:
Add Comment
Your Name:
Comment:




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