Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ctrl + h in multiple files 1

Status
Not open for further replies.

causative

Technical User
Mar 6, 2009
12
PL
Hi
This is Richard Mueller script MVP ADSI ( it replace 1 string to another 1 in multiple files in directory )

Code:
Option Explicit

Dim objFSO, strFolder, objFolder, objFile
Dim strOldValue, strNewValue, objRead, strContents, objWrite

Const ForReading = 1
Const ForWriting = 2

strFolder = "c:\Changing"
strOldValue = "[URL unfurl="true"]http://test.example.com"[/URL]
strNewValue = "test"

' Retrieve specified folder.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)

' Enumerate all files in the folder.
For Each objFile In objFolder.Files
    ' Read file with textstream object.
    Set objRead = objFSO.OpenTextFile(objFile.Path, ForReading)
    ' Trap error if file is empty or cannot read.
    On Error Resume Next
    strContents = objRead.ReadAll
    If (Err.Number <> 0) Then
        On Error GoTo 0
        Wscript.Echo "Cannot read: " & objFile.Path
        strContents = ""
    End If
    On Error GoTo 0
    objRead.Close
    ' Check contents for specified string.
    If (InStr(strContents, strOldValue) > 0) Then
        ' Open file with textstream object to overwrite.
        strContents = Replace(strContents, strOldValue, strNewValue)
        Set objWrite = objFSO.OpenTextFile(objFile.Path, ForWriting)
        objWrite.Write strContents
        objWrite.Close
    End If
Next

How is it possible to change it to replace old value with new value but only in *.txt files ( not all files )?
 
Hi [peace]
Try this code :

Code:
Option Explicit
Const ForReading = 1
Const ForWriting = 2
Dim objFSO, strFolder, objFolder, objFile
Dim strOldValue, strNewValue, objRead, strContents, objWrite
strFolder = "c:\Changing"
strOldValue = "[URL unfurl="true"]http://test.example.com"[/URL]
strNewValue = "test"

' Retrieve specified folder.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)

' Enumerate all files in the folder.
For Each objFile In objFolder.Files
	If IsTXTFile(objFile.Name) Then
' Read file with textstream object.
		Set objRead = objFSO.OpenTextFile(objFile.Path, ForReading)
' Trap error if file is empty or cannot read.
		On Error Resume Next
		strContents = objRead.ReadAll
		If (Err.Number <> 0) Then
			On Error GoTo 0
			Wscript.Echo "Cannot read: " & objFile.Path
			strContents = ""
		End If
		On Error GoTo 0
		objRead.Close
' Check contents for specified string.
		If (InStr(strContents, strOldValue) > 0) Then
' Open file with textstream object to overwrite.
			strContents = Replace(strContents, strOldValue, strNewValue)
			Set objWrite = objFSO.OpenTextFile(objFile.Path, ForWriting)
			objWrite.Write strContents
			objWrite.Close
		End If
	End If	
Next 
'**************************************************************************
Function IsTXTFile(sFile)
	IsTXTFile = (InStr(1, Right(UCase(sFile), InStrRev(sFile, ".")), "TXT") > 0)
End Function
'**************************************************************************
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top