Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

text subtitution in multiple files

Posted on 2004-09-12
2
Medium Priority
?
228 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 600 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 1400 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
Take a look at these 6 Outlook Email management tools which can augment the working and performance of Microsoft Outlook to give you a more rewarding emailing experience.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

886 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