Solved

text subtitution in multiple files

Posted on 2004-09-12
2
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

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…
Deploying our service is a grudge match between customer benefits and customer pain. In one corner, rolling out fixes (yay!) and delivering new features (double yay!). In the other corner, training on new features (boo – sounds like work), and chan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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 antispam), the admini…
Suggested Courses

739 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