erebus Posted October 4, 2005 Share Posted October 4, 2005 (edited) Hello all,I want to write a function that will do the following:a. Read the file blah.txt and search for a line containing the word "changeme".b. Replaces the whole line containing the string "changeme" with a total new line, i.e. "something new".c. Deletes the double entries if desired (for example if the string "changeme" appears in more than one line in the file, it will be able to delete all the last lines containing the string so as to have only one always).I tried to use FileWrite but I have run out of creativity at the moment and I'm stuck...Any ideas?P.S. I don't want to use IniWrite as the lines should not contain the equal sign (=).P.S. 2. Just to let people know, I have already read this post. However I don't want to change just the strings but mostly to use a specific string as an identifier to change/replace it's whole line.Thanks in advance. Edited October 4, 2005 by erebus Link to comment Share on other sites More sharing options...
flyingboz Posted October 4, 2005 Share Posted October 4, 2005 Hello all,I want to write a function that will do the following:a. Read the file blah.txt and search for a line containing the word "changeme".b. Replaces the whole line containing the string "changeme" with a total new line, i.e. "something new".c. Deletes the double entries if desired (for example if the string "changeme" appears in more than one line in the file, it will be able to delete all the last lines containing the string so as to have only one always).you can figure out how filereadtoarray works, or build your own.pseudocode (one way)for each line in array use stringinstr() to determine if changeme is present if so, $line = $changed_linenextfor each line in array filewrite to outputfilenext Reading the help file before you post... Not only will it make you look smarter, it will make you smarter. Link to comment Share on other sites More sharing options...
jefhal Posted October 4, 2005 Share Posted October 4, 2005 you can figure out how filereadtoarray works, or build your own.pseudocode (one way)for each line in array use stringinstr() to determine if changeme is present if so, $line = $changed_linenextfor each line in array filewrite to outputfilenextAlso add an inner loop to go through the array in reverse to delete all array occurances of "old string" except one. Use ubound(array) to find the current array length. Use _arraydelete to delete the current duplicate. And be sure to walk through the array backwards to avoid trying to delete an array value that no longer exists. That is, if your array was array[6] and you deleted [3] and [5], the array is no longer 7 long, but 5... ...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format Link to comment Share on other sites More sharing options...
herewasplato Posted October 4, 2005 Share Posted October 4, 2005 (edited) Hello all,I want to write a function that will do the following:a. Read the file blah.txt and search for a line containing the word "changeme".b. Replaces the whole line containing the string "changeme" with a total new line, i.e. "something new".c. Deletes the double entries if desired (for example if the string "changeme" appears in more than one line in the file, it will be able to delete all the last lines containing the string so as to have only one always).I tried to use FileWrite but I have run out of creativity at the moment and I'm stuck...Any ideas?P.S. I don't want to use IniWrite as the lines should not contain the equal sign (=).P.S. 2. Just to let people know, I have already read this post. However I don't want to change just the strings but mostly to use a specific string as an identifier to change/replace it's whole line.Thanks in advance.Start with c:\temp\test.txt containing:This is line 1This is line 2This is line 3 - changemeThis is line 4This is line 5 - changemeThis is line 6 - changemeThis is line 7$changed_line = "The script replaced this line of text." $allow_duplicates = "no" ; $file = FileOpen("c:\temp\test.txt", 0) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; $new_file = FileOpen("c:\temp\newtest.txt", 2) ; Check if file opened for writing OK If $new_file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; ; Read in lines of text until the EOF is reached $cnt = 0 While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop If (StringInStr($line, "changeme")) Then $cnt = $cnt + 1 If $allow_duplicates = "yes" Then FileWriteLine($new_file, $changed_line) If $allow_duplicates = "no" And $cnt = 1 Then FileWriteLine($new_file, $changed_line) $allow_duplicates = "" EndIf Else FileWriteLine($new_file, $line) EndIf WEnd ; FileClose($file) FileClose($new_file)$allow_duplicates = "no" and you should end up with c:\temp\newtest.txt containing:This is line 1This is line 2The script replaced this line of text.This is line 4This is line 7$allow_duplicates = "yes" and you should end up with c:\temp\newtest.txt containing:This is line 1This is line 2The script replaced this line of text.This is line 4The script replaced this line of text.The script replaced this line of text.This is line 7...just another way to do it.Clear as Mud?Edit: changed codebox to just code Edited October 12, 2005 by herewasplato [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
erebus Posted October 4, 2005 Author Share Posted October 4, 2005 Hello plato, You are definitely my man! Thank you so much, your code works like a charm! Thanks all the other guys too for showing me an excellent way of thinking. The Greeks salute all of you. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now