I tried the search but was unable to find my answer, so please forgive me if this has been answered.
All I want to do is replace the words on a line in a text file with other words.
The line number of the file is always 12.
So how do I change line 12 of a 103 lined text file to read "npoint 3" from "npoint 2"
That's it. I can't find out how to do this. Help please!
Thanks
-Joe
how to edit text in a file?
Moderators: JRL, Dorian (MJT support)
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
Two methods come to mind: 1 - Use a text editor, 2 - Do it all in Macro Scheduler.
=======================
Option 1 - Use a text editor:
Open a text editor program like TextPad, NotePad, WordPad.
Open file to be edited
Go to line 12
Search/Replace "npoint 3" with "npoint 2" this line only.
Save File.
Close text editor.
=============================
Option 2 - Do it all in Macro Scheduler.
No time for complete code, but here is part of it. Loop for reading each line is missing here. This ooption will probably be much slower than option 1, length of files might be biggest time factor. This routine uses VB Replace on one specific line.
=======================
Option 1 - Use a text editor:
Open a text editor program like TextPad, NotePad, WordPad.
Open file to be edited
Go to line 12
Search/Replace "npoint 3" with "npoint 2" this line only.
Save File.
Close text editor.
=============================
Option 2 - Do it all in Macro Scheduler.
No time for complete code, but here is part of it. Loop for reading each line is missing here. This ooption will probably be much slower than option 1, length of files might be biggest time factor. This routine uses VB Replace on one specific line.
Quick and dirty code examples to give the overall concept. Not tested because it will not work as is! Just trying to get you in the right direction.VBStart
VBEnd
Let>Old=npoint 3
Let>New=npoint 2
//Create a loop to read each line in OldFile, and count first 12 lines
//Read OldFile lines to "OldLine" variable, check for line number
//If line number is not 12, then write existing line to a NewFile
//When line number is 12, use VB Replace to replace old text with new text
VBEval>Replace("%OldLine%","%Old%","%New%"),NewLine
//Write %NewLine% to NewFile
//Continue to read and write remaining lines to NewFile until line=##EOF##
//Need to have NewFile replace OldFile? Use CopyFile with CF_OVERWRITE variable or MoveFile with MF_RENAME variable.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!
Hi,
When you change a text file you are actually replacing the file with a new one. Using basic MacroScript you would therefore read the file you are changing line by line, writing out to a new file each time. At the end you would delete the old file and rename the new one to the old file name.
In your case you would output each line as is but at line 12 you would output your desired text:
//***************************
Let>infile=c:\blabla\inputfile.txt
Let>outfile=c:\blabla\outputfile.txt
Let>line=1
Label>start
ReadLn>infile,line,theline
If>theline=##EOF##,done
If>line=12,change,nochange
Let>line=line+1
Goto>start
Label>done
DeleteFile>infile
MoveFile>outfile,infile
SRT>change
WriteLn>outfile,res,"npoint 3" from "npoint 2"
END>change
SRT>nochange
WriteLn>outfile,res,theline
END>nochange
//**********************
Alternatively use VBScript's FileSystemObject to read the entire file into an array, change the 12th element and then write the file out again in one piece.
As you can see there are many options.
When you change a text file you are actually replacing the file with a new one. Using basic MacroScript you would therefore read the file you are changing line by line, writing out to a new file each time. At the end you would delete the old file and rename the new one to the old file name.
In your case you would output each line as is but at line 12 you would output your desired text:
//***************************
Let>infile=c:\blabla\inputfile.txt
Let>outfile=c:\blabla\outputfile.txt
Let>line=1
Label>start
ReadLn>infile,line,theline
If>theline=##EOF##,done
If>line=12,change,nochange
Let>line=line+1
Goto>start
Label>done
DeleteFile>infile
MoveFile>outfile,infile
SRT>change
WriteLn>outfile,res,"npoint 3" from "npoint 2"
END>change
SRT>nochange
WriteLn>outfile,res,theline
END>nochange
//**********************
Alternatively use VBScript's FileSystemObject to read the entire file into an array, change the 12th element and then write the file out again in one piece.
As you can see there are many options.
MJT Net Support
[email protected]
[email protected]
Thanks guys,
I was hoping for an easy solution like the WriteLn function supporting a line number so it could just be inserted in the text file. Sort of like EditIniFile.
What I ended up doing is using SED. It's a UNIX utility ported to windows. Does a quick search and replace that I can save to a file then delete the old and rename to the original. Seems to work well and very fast. I just have to shell out to windows to accomplish it
Thanks for the suggestions!
I was hoping for an easy solution like the WriteLn function supporting a line number so it could just be inserted in the text file. Sort of like EditIniFile.
What I ended up doing is using SED. It's a UNIX utility ported to windows. Does a quick search and replace that I can save to a file then delete the old and rename to the original. Seems to work well and very fast. I just have to shell out to windows to accomplish it

Thanks for the suggestions!
Hi,
What you describe SED as doing is exactly what all our suggestions here do also. The difference is that our suggestions don't require shelling out to windows. But it's your choice.
What you describe SED as doing is exactly what all our suggestions here do also. The difference is that our suggestions don't require shelling out to windows. But it's your choice.
MJT Net Support
[email protected]
[email protected]
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
I'm not familiar yet with SED, but couldn't that be called with Run Program> and passed parameters from Macro Scheduler?
PS for Joe: The board does not remember you if you are not logged in. If cookies are enabled, you may want to set browser for auto login.
PS for support: How and why was this made into a "sticky" post?
PS for Joe: The board does not remember you if you are not logged in. If cookies are enabled, you may want to set browser for auto login.
PS for support: How and why was this made into a "sticky" post?
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!
No idea why it was made into a sticky post. I have made it a normal post. Not entirely sure if there is really any need for sticky posts at all, so will look at removing the option.
MJT Net Support
[email protected]
[email protected]