Proph Posted February 23, 2007 Share Posted February 23, 2007 Is it possible to use _StringBetween By indicating the start and the end of the file? I am trying to code something and it works fine when I am getting text withing the middle of a file... but at some times I need to get the text from a specific string down to the eof. Is this possible? Without having to read line after line. I like the fact that _StringBetween gets the text in one swoop. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 23, 2007 Moderators Share Posted February 23, 2007 Is it possible to use _StringBetween By indicating the start and the end of the file?I am trying to code something and it works fine when I am getting text withing the middle of a file... but at some times I need to get the text from a specific string down to the eof. Is this possible? Without having to read line after line. I like the fact that _StringBetween gets the text in one swoop.Considering that you started a topic on this question... even though your post doesn't show it, I am going to assume that you've tried. Having said that, provide an example of what you are talking about, including a text file, what it is you want to search, and what you expect the out come to be.Now, if you are just trying to find a word or combination of words, I will suggest StringInStr().If you need to know the RegExp for "Start" of string it's ^ if you need to know it for the end of string it's $.Anything else I provide is a waste of brain power on such a broad question ... Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Proph Posted February 23, 2007 Author Share Posted February 23, 2007 Considering that you started a topic on this question... even though your post doesn't show it, I am going to assume that you've tried. Having said that, provide an example of what you are talking about, including a text file, what it is you want to search, and what you expect the out come to be. Now, if you are just trying to find a word or combination of words, I will suggest StringInStr(). If you need to know the RegExp for "Start" of string it's ^ if you need to know it for the end of string it's $. Anything else I provide is a waste of brain power on such a broad question ...Thanks for the quick reply. Sorry... I thought it was explained thoughroughly when I posted it but now that I re-read it it does look a little vague. Here is a function I am making: ;$secfile = File ;$sec2read Section in file to read Func _SectionRead($secfile, $sec2read) $sizeis = FileGetSize($secfile) $StringIs = FileRead($secfile, $sizeis) $aArray1 = _StringBetween($StringIs, '['&$sec2read&']'&@CRLF, @CRLF&'[') $NoExtraLines = $aArray1[0] While 1 If StringInStr($NoExtraLines, @CRLF&@CRLF) Then $NoExtraLines = StringReplace($NoExtraLines, @CRLF&@CRLF, @CRLF) Else ExitLoop EndIf WEnd $String2Array = StringSplit($NoExtraLines, @LF) Return $String2Array EndFunc It grabs the entire section inside of an ini file and puts it into an array for me to use. I can't use the built in IniReadSection because it isn't a true ini files. It doesn't use any = signs. The code above seems to work fine as long as the section is not the last section in the file because then the pattern doesn't find a '[' for the end string. So if I understand you correctly I can use the StringRegExp portion of _StringBetween and specify $ as the end of the file? I'll try that. Thanks Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 23, 2007 Moderators Share Posted February 23, 2007 (edited) Here, try this:Func _SectionRead($secfile, $sec2read) $sizeis = FileGetSize($secfile) $StringIs = FileRead($secfile, $sizeis) $aArray1 = StringRegExp($StringIs, '(?s)(?i)\[' & $sec2read & ']\s*(.*?)\s*(\[|$)', 3) $NoExtraLines = $aArray1[0] While 1 If StringInStr($NoExtraLines, @CRLF&@CRLF) Then $NoExtraLines = StringReplace($NoExtraLines, @CRLF&@CRLF, @CRLF) Else ExitLoop EndIf WEnd $String2Array = StringSplit(StringStripCR($NoExtraLines), @LF) Return $String2Array EndFuncoÝ÷ ØGb´²Z()àjÛaÉbèî²ÙèÁ«¢+ØÀÌØíMÑÉ¥¹ÉÉÉäôMÑÉ¥¹MÁ±¥Ð ÀÌØí9½áÑÉ1¥¹Ì°1oÝ÷ ØÚ-iö§¡ü!jܨ¹ªÞv§Ø^â7öjÞ¦Vx&wnÞÂ+a¶¬jëh×6$String2Array = StringSplit(StringStripCR($NoExtraLines), @LF)oÝ÷ ØGb·aè¥ì¨¹Ú'ßÛfwHë-çbvØ^Â%yû§rبØ^Á¬v'mä®)àEèÆ!¢é]~,mçh¹¹^ ÅË.y«¢+ÙÕ¹}MÑ¥½¹I ÀÌØíÍ¥±°ÀÌØíÍÉɤ($ÀÌØíÉÉäÄôMÑÉ¥¹IáÀ¡¥±I ÀÌØíÍ¥±¤°Ìäì ý̤ ý¤¤ÀäÈílÌäìµÀìÀÌØíÍÉɵÀìÌäítÀäÈį́ ¸¨ü¤ÀäÈį́ ÀäÈímðÀÌØì¤Ìäì°Ì¤(%%%ÍÉÉä ÀÌØíÉÉäĤôÀQ¡¸IÑÕɸMÑÉÉ½È Ä°À°ÌäìÌäì¤(IÑÕɸMÑÉ¥¹MÁ±¥Ð¡MÑÉ¥¹MÑÉ¥Á H ÀÌØíÉÉäÅlÁt¤°1¤)¹Õ¹ Edited February 23, 2007 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Proph Posted February 24, 2007 Author Share Posted February 24, 2007 Sweet! You are the man. Thanks a lot. Your code is definatly pointing me in the direction I was hoping to go. I appreciate it a lot. Link to comment Share on other sites More sharing options...
Proph Posted February 25, 2007 Author Share Posted February 25, 2007 @Smoke N Sorry to bother you on this again. I am trying to figure out how to make the array not display any lines that are commented out using the ";" character. Any ideas? Thanks. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 26, 2007 Moderators Share Posted February 26, 2007 (edited) @Smoke N Sorry to bother you on this again. I am trying to figure out how to make the array not display any lines that are commented out using the ";" character. Any ideas? Thanks.You say you are trying to figure out... care to share some failed attempts? Edit: I don't feel like figuring out the RE ... Func _SectionRead($secfile, $sec2read) $aArray1 = StringRegExp(FileRead($secfile), '(?s)(?i)\[' & $sec2read & ']\s*(.*?)\s*(\[|$)', 3) If IsArray($aArray1) = 0 Then Return SetError(1, 0, '') Local $sHold = '' For $iCC = 0 To UBound($aArray1) - 1 If StringLeft(StringStripWS($aArray1, 8), 1) <> ';' Then $sHold &= $aArray1[$iCC] & @LF Next If $sHold Then Return StringSplit(StringTrimRight($sHold, 1), @LF) Return SetError(2, 0, '') EndFunc Edited February 26, 2007 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Proph Posted February 26, 2007 Author Share Posted February 26, 2007 (edited) I think I got it working now. Here's the code right now: Func _SectionRead($secfile, $sec2read) $aArray1 = StringRegExp(FileRead($secfile), '(?s)(?i)\[' & $sec2read & ']\s*(.*?)\s*(\[|$)', 3) If IsArray($aArray1) = 0 Then Return SetError(1, 0, '') $aOutput = _ArrayToString($aArray1, @LF) $aOutput = StringSplit($aOutput, @LF) $Amount = $aOutput[0] $i = 1 While 1 If StringLeft(StringStripWS($aOutput[$i], 8), 1) = ';' Or StringLeft(StringStripWS($aOutput[$i], 8), 1) = '' Or StringLeft(StringStripWS($aOutput[$i], 8), 1) = '[' Then _ArrayDelete($aOutput, $i) $Amount = $Amount - 1 $i = $i + 1 If $i > $Amount Then ExitLoop Else $i = $i + 1 If $i > $Amount Then ExitLoop EndIf WEnd _ArrayDelete($aOutput, 0) $aOutput = _ArrayToString($aOutput, @LF) Return StringSplit($aOutput, @LF) EndFunc Thanks for all of your help Smoke n! Edit: Hmm.. I still have one small bug with it so far. If only one value is in a section it does not show it. Edited February 26, 2007 by Proph Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 26, 2007 Moderators Share Posted February 26, 2007 The array index starts at 0 ... not 1 like you have it. Just use mine and add the StringStrip = '' to mine. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Proph Posted February 26, 2007 Author Share Posted February 26, 2007 I tried to use yours... but it doesn't seem to take out the commented lines for me. I know I am missing something simple... but I have been sitting at this laptop for 2 days trying to get this and a _SectionWrite Function to work. Everytime I think it works I find something else wrong. I'm going nuts! LOL! Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 26, 2007 Moderators Share Posted February 26, 2007 I tried to use yours... but it doesn't seem to take out the commented lines for me. I know I am missing something simple... but I have been sitting at this laptop for 2 days trying to get this and a _SectionWrite Function to work. Everytime I think it works I find something else wrong. I'm going nuts! LOL!I messed up on my Array ... I forgot this was only returning the whole string (obviously I didn't test it first)... I did however test thisFunc _SectionRead($secfile, $sec2read) $aArray = StringRegExp(FileRead($secfile), '(?s)(?i)\[' & $sec2read & ']\s*(.*?)\s*(\[|$)', 3) If IsArray($aArray) = 0 Then Return SetError(1, 0, '') Local $sHold = '', $aSplit = StringSplit(StringStripCR($aArray[0]), @LF) For $iCC = 1 To UBound($aSplit) - 1 If StringLeft(StringStripWS($aSplit[$iCC], 8), 1) <> ';' And _ StringStripWS($aSplit[$iCC], 8) <> '' And _ StringLeft(StringStripWS($aSplit[$iCC], 8), 1) <> '[' Then $sHold &= $aSplit[$iCC] & @LF Next If $sHold Then Return StringSplit(StringTrimRight($sHold, 1), @LF) Return SetError(2, 0, '') EndFunc Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Proph Posted February 26, 2007 Author Share Posted February 26, 2007 That was the magic script! Now here is the _SectionWrite script I wrote. I was glad to see that once the _SectionRead was working right that the _SectionWrite I have been working on for 2 days actually worked as well. expandcollapse popupFunc _SectionWrite($secfile, $sec2WriteTo, $sec2write = '', $secsort = 0) $secRead = _SectionRead($secfile, $sec2WriteTo) If @error = 1 Then FileWrite($secfile, @CRLF&@CRLF&'['&$sec2WriteTo&']'&@CRLF&$sec2write&@CRLF) $secRead = _SectionRead($secfile, $sec2WriteTo) EndIf $secExists = 'N' If IsArray($secRead) Then For $i = 1 to $secRead[0] If IsArray($sec2write) Then For $k = 1 To $sec2write[0] $KsecExists = 'N' $secRead2 = _SectionRead($secfile, $sec2WriteTo) For $j = 1 To $secRead2[0] If $secRead2[$j] = $sec2write[$k] Then $KsecExists = 'Y' EndIf Next If $KsecExists = 'N' Then If $secRead2[0] = 1 And $secRead2[1] = '' Then $A2Sis = '' $sizeis = FileGetSize($secfile) $StringIs = FileRead($secfile, $sizeis) $ReplaceIt = StringReplace($StringIs,'['&$sec2WriteTo&']','['&$sec2WriteTo&']'&@CRLF&$sec2write[$k], 1, 1) $secOpenIs = FileOpen($secfile, 2) FileWrite($secOpenIs, $ReplaceIt) FileClose($secOpenIs) ElseIf $secRead2[0] = 1 Then $A2Sis = $secRead2[1] _ArrayDelete($secRead2, 0) ElseIf $secRead2[0] > 1 Then _ArrayDelete($secRead2, 0) $A2Sis = _ArrayToString($secRead2, @CRLF) EndIf If $A2Sis <> '' Then _ArrayAdd($secRead2, $sec2write[$k]) If $secsort <> 0 Then _ArraySort($secRead2) $A2Sto = _ArrayToString($secRead2, @CRLF) $sizeis = FileGetSize($secfile) $StringIs = FileRead($secfile, $sizeis) $ReplaceIt = StringReplace($StringIs,'['&$sec2WriteTo&']'&@CRLF&$A2Sis,'['&$sec2WriteTo&']'&@CRLF&$A2Sto, 1, 1) $secOpenIs = FileOpen($secfile, 2) FileWrite($secOpenIs, $ReplaceIt) FileClose($secOpenIs) EndIf EndIf Next ElseIf $secRead[$i] = $sec2write Then $secExists = 'Y' ExitLoop EndIf Next If Not IsArray($sec2write) Then If $secExists = 'N' Then If $secRead[0] = 1 And $secRead[1] = '' Then $A2Sis = '' $sizeis = FileGetSize($secfile) $StringIs = FileRead($secfile, $sizeis) $ReplaceIt = StringReplace($StringIs,'['&$sec2WriteTo&']','['&$sec2WriteTo&']'&@CRLF&$sec2write, 1, 1) $secOpenIs = FileOpen($secfile, 2) FileWrite($secOpenIs, $ReplaceIt) FileClose($secOpenIs) ElseIf $secRead[0] = 1 Then $A2Sis = $secRead[1] _ArrayDelete($secRead, 0) ElseIf $secRead[0] > 1 Then _ArrayDelete($secRead, 0) $A2Sis = _ArrayToString($secRead, @CRLF) EndIf If $A2Sis <> '' Then _ArrayAdd($secRead, $sec2write) If $secsort <> 0 Or $sec2write = '' Then _ArraySort($secRead) $A2Sto = _ArrayToString($secRead, @CRLF) $sizeis = FileGetSize($secfile) $StringIs = FileRead($secfile, $sizeis) $ReplaceIt = StringReplace($StringIs,$A2Sis,$A2Sto, 1, 1) $secOpenIs = FileOpen($secfile, 2) FileWrite($secOpenIs, $ReplaceIt) FileClose($secOpenIs) EndIf EndIf EndIf Else $sizeis = FileGetSize($secfile) $StringIs = FileRead($secfile, $sizeis) $ReplaceIt = StringReplace($StringIs,'['&$sec2WriteTo&']','['&$sec2WriteTo&']'&@CRLF&$sec2write, 1, 1) $secOpenIs = FileOpen($secfile, 2) FileWrite($secOpenIs, $ReplaceIt) FileClose($secOpenIs) EndIf EndFunc I am gonna run some more tests on it now. But I think this works perfect so far. It's not as slick as your optimized code but it does the trick... at least I hope so. LOL! Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 26, 2007 Moderators Share Posted February 26, 2007 Wow... that function is doing alot... Ever thought of breaking it up into more manageable pieces? Only concern yourself I guess if it starts to fail . Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Proph Posted February 26, 2007 Author Share Posted February 26, 2007 Yeah I know. Once I know it's working the way I want I'll break it down and add more error checking. 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