Solved

text subtitution in multiple files

Posted on 2004-09-12
2
220 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
ID: 12040290
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
ID: 12040441
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

895 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

13 Experts available now in Live!

Get 1:1 Help Now