Solved

text subtitution in multiple files

Posted on 2004-09-12
2
222 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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…
Starting your own business is always a daunting process, and for most people it is brand new experience. Avoid the common pitfalls by following these tips to start on the road to success.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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