Ahmed97 Posted August 26, 2011 Posted August 26, 2011 (edited) Hi, i want make a script that edit a file and write some lines in it, but i want make script read the lines in the file first before writing and if the script found specific line start with this word "Hello" then delete the word and write new line, is it possible? if yes then, is there any idea?? Thanks. Edit: Problem solved >>>>>> i'll try thanks 4 help I mentioned _FileWriteToLine because FileWriteLine will only write to end of file. Here is a very basic way to do it (seeing as you at least had a go) Its not the recommended way and will be quicker reading the whole file at once, but will get you started and you can take it from there. #include <File.au3> ; needed for the file UDFs $sFilePath = @ScriptDir & "\test.ini" $iLinesInFile = _FileCountLines($sFilePath) ;Count how many lines are in the file MsgBox(0, "_FileCountLines", $iLinesInFile) For $i = 1 To $iLinesInFile ; loop through all the lines in your file $sReadLine = FileReadLine($sFilePath, $i); read them as you go along If StringInStr($sReadLine, "123 hello") Then ; If it finds your string in the file it just read MsgBox(0, "FileReadLine", "This line will be replaced : " & $sReadLine) _FileWriteToLine($sFilePath, $i, "This writes over the line found", 1) ; Write over that line with new string EndIf Next MsgBox(0, "Ta", "Da") Thanks alot this helped me Edited August 26, 2011 by TheMaster
JohnOne Posted August 26, 2011 Posted August 26, 2011 Yes, it is possible. Have you tried something? here's a quick order of business you will need to take care of if you have not. Read the lines of your file, this is pretty self explaining and you can use the example in helpfile to guide you. As you are reading the lines, you will need to check for your string within it (I'm not saying how to do that as I'm certain you will say that was just an example "Hello" and its something else), so for now just look at StringInStr() function to test the whole line. If your string is found then you depending on where you want to write this new line, you can use the FileWriteLine() which will write to the end of the file, or _FileWriteToLine() which can insert it at any position. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
Ahmed97 Posted August 26, 2011 Author Posted August 26, 2011 Ok john thanks for idea, i'll try and will tell you.
Ahmed97 Posted August 26, 2011 Author Posted August 26, 2011 ummmm really nothing happened or i can't do it ^^ i tryed but i failed i need help ^^ look to the e.g. ----------- there's text @ScriptDir and in this txt there's lines - hi - bla - bla bla - hello - 548gj45gs4t3 - 459uts9df - 123 hello - hello 123 i want the script search for "123 hello" line or any line and if the script found it then replace it with another line and if not then, WriteLine at the end of the txt. Any helps?? Thanks.
monoscout999 Posted August 26, 2011 Posted August 26, 2011 so.... your efforts... dont be shy and post it
Ahmed97 Posted August 26, 2011 Author Posted August 26, 2011 (edited) ok. but dont ridicule. Here is it ^^ $file = FileOpen(@ScriptDir & "\test.txt", 1) $Check = StringInStr($file, "123 Hello", 0, 1) _FileWriteToLine($file, $Check, "123 Hello") If $Check = 1 Then MsgBox(0, "", "found text",) EndIf FileClose($file) Edit: Any helps??? Edited August 26, 2011 by TheMaster
JohnOne Posted August 26, 2011 Posted August 26, 2011 You are missing one very important part of that script, and that is actually reading the file. It can be done in a number of ways FileRead FileReadLine _FileReadToArray (Recommended) Remember, when you look at a function in the helpfile, click the button to open the script and try out the example. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
monoscout999 Posted August 26, 2011 Posted August 26, 2011 I never use _FileReadToLine but as far i can see there´s a couple of errors... The design is wrong because you need to applied the StringInStr to fileLine, and not to the file handler I dont have AutoIt right now, but the steps are the follow, - File Open - In a Loop start to Read from the first line until the last one(becasue your way is reading only one line, you need a LOOP, Look at the filereadline() function example in the help file) - Do a sentence - IF stringinstr blablabla returns true or wherever it returns.. do filewriteline(read the help file to know how to handle this function) - If not... do nothing - Close the loop - Close the file handle something like this
JohnOne Posted August 26, 2011 Posted August 26, 2011 I mentioned _FileWriteToLine because FileWriteLine will only write to end of file. Here is a very basic way to do it (seeing as you at least had a go) Its not the recommended way and will be quicker reading the whole file at once, but will get you started and you can take it from there. #include <File.au3> ; needed for the file UDFs $sFilePath = @ScriptDir & "\test.ini" $iLinesInFile = _FileCountLines($sFilePath) ;Count how many lines are in the file MsgBox(0, "_FileCountLines", $iLinesInFile) For $i = 1 To $iLinesInFile ; loop through all the lines in your file $sReadLine = FileReadLine($sFilePath, $i); read them as you go along If StringInStr($sReadLine, "123 hello") Then ; If it finds your string in the file it just read MsgBox(0, "FileReadLine", "This line will be replaced : " & $sReadLine) _FileWriteToLine($sFilePath, $i, "This writes over the line found", 1) ; Write over that line with new string EndIf Next MsgBox(0, "Ta", "Da") AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
Ahmed97 Posted August 26, 2011 Author Posted August 26, 2011 I never use _FileReadToLine but as far i can see there´s a couple of errors... The design is wrong because you need to applied the StringInStr to fileLine, and not to the file handler I dont have AutoIt right now, but the steps are the follow, - File Open - In a Loop start to Read from the first line until the last one(becasue your way is reading only one line, you need a LOOP, Look at the filereadline() function example in the help file) - Do a sentence - IF stringinstr blablabla returns true or wherever it returns.. do filewriteline(read the help file to know how to handle this function) - If not... do nothing - Close the loop - Close the file handle something like this i'll try thanks 4 help I mentioned _FileWriteToLine because FileWriteLine will only write to end of file. Here is a very basic way to do it (seeing as you at least had a go) Its not the recommended way and will be quicker reading the whole file at once, but will get you started and you can take it from there. #include <File.au3> ; needed for the file UDFs $sFilePath = @ScriptDir & "\test.ini" $iLinesInFile = _FileCountLines($sFilePath) ;Count how many lines are in the file MsgBox(0, "_FileCountLines", $iLinesInFile) For $i = 1 To $iLinesInFile ; loop through all the lines in your file $sReadLine = FileReadLine($sFilePath, $i); read them as you go along If StringInStr($sReadLine, "123 hello") Then ; If it finds your string in the file it just read MsgBox(0, "FileReadLine", "This line will be replaced : " & $sReadLine) _FileWriteToLine($sFilePath, $i, "This writes over the line found", 1) ; Write over that line with new string EndIf Next MsgBox(0, "Ta", "Da") Thanks alot this helped me
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