Wena Posted June 11, 2012 Share Posted June 11, 2012 (edited) Hi I am trying to read a portion of a line in a text file and the replace the whle line. I have tried _ReplaceStringInFile but that seems to on replace the the text serched for and not the whole line. I have attached a sample of my code. I am not expecting anyone to rewrite the code or fix it, just a nudge in the correct direction. The text file given in the code is not the original so please excuse my $variables = "nonsense line" $oldline = "ke is here", is "ke is here for a long time" in the txt file. Thanks AlistairSearch_for_line.au3 Edited June 11, 2012 by Wena Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 11, 2012 Moderators Share Posted June 11, 2012 Wena, Much simpler to read the file into an array, change the lines and then rewrite it: #include <File.au3> Global $aLines $sOldLine = "Ke is here" $sNewLine = "Wena is here now" $sTestFile = ("testline.txt") ; Read the file into an array _FileReadToArray($sTestFile, $aLines) ; Now loop through the array For $i = 1 To $aLines[0] ; If the text is found If StringInStr($aLines[$i], $sOldLine) Then ; Change the whole line $aLines[$i] = $sNewLine EndIf Next ; And now rewrite the file - note we need to exclude the [0] element as that is the line count _FileWriteFromArray($sTestFile, $aLines, 1) Please ask if you have any questions. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
PhoenixXL Posted June 11, 2012 Share Posted June 11, 2012 (edited) Try Coordinating FileReadLine with _ReplaceStringInFileTip: Read the full line make the replacements wherever required and then write the whole line (not only the Replace Chars)RegardsPhoenix XL Edited June 11, 2012 by PhoenixXL My code: PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners. MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression. Link to comment Share on other sites More sharing options...
Wena Posted June 11, 2012 Author Share Posted June 11, 2012 Hi Melba23 Thanks for the reply. I have read through the help file on _FileReadFromArray and FileWriteToArray and now understand it. What I do not understand is in the FOR loop there is no step just "$aLines[0] as a stop value. @Phoenix XL: I cannot read the whole line as the last half of the line is unique. I need to change a line in the txt file that has the same beginning but unique at the end with a line that will always be the same. Is there no "wild cards" like in SQL "%ke is%" Alistair Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 11, 2012 Moderators Share Posted June 11, 2012 Wena, in the FOR loop there is no step just "$aLines[0] as a stop valueAnd what does the Help file tell you about the array returned by _FileReadToArray? $aArray[0] will contain the number of records read into the array Which is why we have to use the $iBase parameter in _FileWriteFromArray to make sure we do not write it back to the file. All clear? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Wena Posted June 11, 2012 Author Share Posted June 11, 2012 All Clear. Read it again and there it was in plain daylight under remarks. Thanks for your help I really appreciate it.(and now understand it better as well) Alistair Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 11, 2012 Moderators Share Posted June 11, 2012 Wena, My pleasure. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Wena Posted June 11, 2012 Author Share Posted June 11, 2012 Could someone close this topic for me or tell me how to mark it resolved. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 11, 2012 Moderators Share Posted June 11, 2012 Wena,Edit the first post and select the "Use Full Editor" option - then you can edit the title as well. Please just add "[sOLVED] to the title - do not replace it entirely. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Malkey Posted June 11, 2012 Share Posted June 11, 2012 And as the sun sets slowly in the west. Opt("WinTitleMatchMode", -2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase ; -------- Create a Test File - "testline.txt" ------------- Local $sOrigContents = _ "line 1" & @CRLF & _ "line 2" & @CRLF & _ "So, Ke is here then." & @CRLF & _ "line 4" Local $sTestFile = "testline.txt" Local $hFileOrig = FileOpen($sTestFile, 2) FileWrite($hFileOrig, $sOrigContents) FileClose($hFileOrig) ; -------- Test File Created -------------- Local $sOldLine = "Ke is here" Local $sNewLine = "Wena is here now" Local $sFileContents = FileRead($sTestFile) ;ConsoleWrite($sFileContents & @LF) ; -- Replace entire old line containing string, $sOldLine, with new line, $sNewLine, and re-write to file -- Local $hFile = FileOpen($sTestFile, 2) FileWrite($hFile, StringRegExpReplace($sFileContents, "(?m)(^.*" & $sOldLine & ".*$)", $sNewLine)) FileClose($hFile) ; -------- Display the file and delete the file. ---------- ShellExecute($sTestFile) ;ConsoleWrite(StringRegExpReplace($sTestFile,"(^.*\\)","") & @LF) WinWaitActive(StringRegExpReplace($sTestFile,"(^.*\\)","")) FileDelete($sTestFile) ; <------- Test File Deleted. ------------- 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