Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Operating System - Microsoft
Showing results for 
Search instead for 
Did you mean: 

Changing a paragraph within a text file

Go to solution
Michael Campbell
Trusted Contributor

Changing a paragraph within a text file


I need to make a change to a config file on each client Windows PC.
Ideally, I would like to create a batch script that I could mail to users and they could run themselves to make the change.

The layout of the file is as follows:


The file may have 10 or 15 of these paragraphs, including other entries with the same hostnames and port numbers but the db_name in each paragraph is unique.

If is equal to ABCD, then I want to change to XYZ and to 1234.

Does anyone have any ideas on this?

Any Help Appreciated

Ganesh Babu
Honored Contributor

Re: Changing a paragraph within a text file

Hi Michael,
I am attaching a windows script file which u can test it out whether it meets your expectations..

I have hardcoded currently come of the values.. like file name & path and the word to search and replace for..

Ganesh Babu
Honored Contributor

Re: Changing a paragraph within a text file


If u r not able to download the file i am pasting the code here. In brief, what i am doing is, reading the file line by line and checking whether the DBname is there r not, if it is there replace with the new dbname and write to a new file
once all lines are over, i am copying the new file to the old filename and then deleteing then delete the new file.

U can alter the code if u want.. as i told earlier i have some values hardcoded, u can see them in teh code..


<script language="VBScript">

dim FSO
dim logfile, newfile

Set FSO = CreateObject("Scripting.FileSystemObject")

dim logfilename

logfilename = "e:\testing\1.txt"
newfilename = "e:\testing\2.txt"

Set logFile = fso.OpenTextFile(logfilename, 1, False)
Set newFile = fso.createtextfile(newfilename, True)

dim Rec,result

Do While logFile.AtEndOfStream <> True

Rec = logFile.ReadLine

if instr(Rec,"ABCD") <> 0 then

result = replace (Rec,"ABCD","XYZ")
newfile.writeline (Result)

newfile.writeline (Rec)
end if



set logfile = nothing
set newfile = nothing

fso.copyfile newfilename,logfilename
fso.deletefile newfilename

Ganesh Babu
Honored Contributor

Re: Changing a paragraph within a text file

Right now i have done this do replace only the db_name, if u need for hostname & port u have to modify the code for the same..

i think u should be able to do it.. if not let me know, i will do it for u..

Michael Campbell
Trusted Contributor

Re: Changing a paragraph within a text file

Thanks Ganesh