Code Script .co.uk

Home | Scripts | Tutorials | Disclaimer | Sitemap | Contact

   Find And Replace Text In Files
 

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

Find and replace text in all files and folders in the specified directory and all subdirectories.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject ("Shell.Application")
Set objFolder = objShell.BrowseForFolder (0, "Select Target Folder", (0))

'Max number of times to replace string
strCount = 999
'Comparison type: 0 = case sensitive, 1 = case insensitive
strCompare = 1


targetPath = objFolder.Items.Item.Path

If targetPath = "" Then
Wscript.Quit
End If

find = InputBox("Add string to find.","Find", "")

If find = "" Then
Wscript.Quit
End If

strReplace = InputBox("Add string to replace '" & find & "' with.","Replace", "")

Wscript.Echo "Replace '" & find & "' with '" & strReplace & "' in all files (including subfolders) in '" & targetPath & "'?"


fileReplace objFSO.GetFolder(targetPath)

Sub fileReplace(folder)

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

Set objTxtFile = objFSO.OpenTextFile(objFile, 1)
strCont = objTxtFile.ReadAll
strOldCont = strCont
objTxtFile.Close

'While inStr(strCont, find)
strStart = 1
strCont = Replace(strCont, find,strReplace,strStart,strCount,strCompare)
'Wend

strPath = objFile.Path

'Only edit if new content is different to original content
If strOldCont <> strCont Then
Wscript.Echo "Replacing"

objFile.Delete

Wscript.Echo "Writing " & strPath & " with: " & strCont

Set objNewTxtFile = objFSO.OpenTextFile(strPath, 8,True)

objNewTxtFile.Write(strCont)

End If

If Err.Number <> 0 Then
WScript.Echo "Error editing: " & objFile.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
fileReplace 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
          - 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