Solved

text subtitution in multiple files

Posted on 2004-09-12
2
219 Views
Last Modified: 2010-04-14
I have to replace an IP address in about 100 files. Is there a way to script this in Windows?

ie. replace 192.168.2.2 by 10.0.0.2 in all the global.asa files under C:\Inetpub\*
0
Comment
Question by:danielvic
2 Comments
 
LVL 12

Assisted Solution

by:Gary Dewrell
Gary Dewrell earned 150 total points
Comment Utility
Hi danielvic,
Here is a free utility that will do what you want.
http://www.boolean.ca/replace/

God Bless
0
 
LVL 76

Accepted Solution

by:
David Lee earned 350 total points
Comment Utility
This script should get the job done.  Here's how to use it:

1.  Open Notepad.
2.  Copy the script and paste it into the Notepad.
3.  Save the file with a .vbs extension.  
4.  Open a command prompt (Start->Run->Cmd).
5.  Navigate to the fodler where you saved the script file.
6.  Run the script like this:
     Cscript ScriptName StartingPath
     Example:
         Cscript FixAsaFiles.vbs C:\Inetpub\
         Assumes you named the script FixAsaFiles.
         The starting path must end with a \
7.  The script will report the number of folders it traversed and the number of files it changed during its run.
8.  I tested the script myself and it fixed the ASA files replacing 192.168.2.2 with 10.0.0.2 as requested.  To be safe though I'd strongly recommend that you make a backup of the Inetpub folder before running the script against it.  



Dim objFSO, _
    intFolders, _
    intFiles, _
    strStartingPath

    strStartingPath = Wscript.Arguments.Item(0)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    ProcessFolder strStartingPath
    Set objFSO = Nothing
    Wscript.Echo "Folders Processed: " & intFolders & " Files Changed: " & intFiles
    Wscript.Quit

Sub ProcessFolder(strFolderPath)
    Dim objFolder, _
        objSubFolders
    'Create the starting folder object
    Set objFolder = objFSO.GetFolder(strFolderPath)
    'Call the ProcessFiles subroutine and pass it the folder object
    ProcessFiles objFolder
    intFolders = intFolders + 1
    'Create the sub-folders object
    Set objSubFolders = objFolder.SubFolders
    'Loop through the sub-folders in the folder object
    For Each objFolder In objSubFolders
        'Call the ProcessFiles subroutine and pass it the sub-folder object
        ProcessFolder objFolder.Path
    Next
    'Dispose of the created objects
    Set objSubFolders = Nothing
    Set objFolder = Nothing
End Sub

Sub ProcessFiles(objFolder)
    Dim objFile, _
        objOldTS, _
        objNewTS, _
        strFileType, _
        strBuffer
    'Loop through the files in the folder object
    For Each objFile In objFolder.Files
        strFileType = LCase(objFSO.GetExtensionName(objFile.Name))
        If strFileType = "asa" Then
            Set objOldTS = objFSO.OpenTextFile(objFile.Path)
            Set objNewTS = objFSO.CreateTextFile("Temp.Asa", 2)
            While Not objOldTS.AtEndOfStream
                strBuffer = objOldTS.ReadLine
                strBuffer = Replace(strBuffer, "192.168.2.2", "10.0.0.2")
                objNewTS.WriteLine strBuffer
            Wend
            objOldTS.Close
            ObjNewTS.Close
            objFSO.CopyFile "Temp.Asa", objFile.Path, True
            objFSO.DeleteFile "Temp.Asa"
            Set objOldTS = Nothing
            Set objNewTS = Nothing
            intFiles = intFiles + 1
        End If
    Next
    'Dispose of the object created in this subroutine
    Set objFile = Nothing
End Sub
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
A procedure for exporting installed hotfix details of remote computers using powershell
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now