Herb191 Posted April 19, 2009 Share Posted April 19, 2009 Ok, so Im trying to write a peace of script that coverts some text file information on books into a usable ini file. Here is an example of some of the text in the file that I need: TITLE: How to Make Money with Myspace by Dennis Prince ISBN: 0071-54467-4 ISBN 13: 978-0071-54467-2 Publisher: McGraw-Hill Publish Date: February 2008 Binding: Paperback , 256 pages Weight: 0.8 pounds List Price: USD 21.95 Ideally after running the script my ini file would look like this: [Vars] Tile=How to Make Money with Myspace Author =Dennis Prince ISBN=0071-54467-4 ISBN13=978-0071-54467-2 Publisher=McGraw-Hill Publish Date=February 2008 Binding=Paperback ListPrice=21.95 I have tried versus combinations of StringInStr, FileReadLine and _StringBetween to get the info from the file but all with no luck. I thought _StringBetween would be my best bet but I cant seem to get it to work on any text that has spaces or returns in them. Here is what I have been trying with the _StringBetween. Any suggestions or pointers would be greatly appreciated. #include <String.au3> #include <file.au3> Dim $Result Dim $filesample local $Rtn = "" Local $fArray Local $Tile _FileReadToArray(@ScriptDir & "testfile.txt", $fArray) For $I = 1 to Ubound($fArray)-1 $Tile = _StringBetween( $fArray[$I], 'TITLE:' , 'by') $Rtn &= $Tile[0] & @CRLF Next $Bookinfo = @ScriptDir & "\Bookinfo.ini" IniWrite($Bookinfo, "Vars", "Tile", $Tile) Link to comment Share on other sites More sharing options...
MrMitchell Posted April 19, 2009 Share Posted April 19, 2009 You may want to research StringSplit(), split each line by ":" then use the first part as Key then second part as Value. Link to comment Share on other sites More sharing options...
TheSaint Posted April 19, 2009 Share Posted April 19, 2009 Ok, so I'm trying to write a peace of script that coverts some text file information on books into a usable ini file. Here is an example of some of the text in the file that I need: TITLE: How to Make Money with Myspace by Dennis Prince ISBN: 0071-54467-4 ISBN 13: 978-0071-54467-2 Publisher: McGraw-Hill Publish Date: February 2008 Binding: Paperback , 256 pages Weight: 0.8 pounds List Price: USD 21.95 Ideally after running the script my ini file would look like this: [Vars] Tile=How to Make Money with Myspace Author =Dennis Prince ISBN=0071-54467-4 ISBN13=978-0071-54467-2 Publisher=McGraw-Hill Publish Date=February 2008 Binding=Paperback ListPrice=21.95 I have tried versus combinations of StringInStr, FileReadLine and _StringBetween to get the info from the file but all with no luck. I thought _StringBetween would be my best bet but I cant seem to get it to work on any text that has spaces or returns in them. Here is what I have been trying with the _StringBetween. Any suggestions or pointers would be greatly appreciated. #include <String.au3> #include <file.au3> Dim $Result Dim $filesample local $Rtn = "" Local $fArray Local $Tile _FileReadToArray(@ScriptDir & "testfile.txt", $fArray) For $I = 1 to Ubound($fArray)-1 $Tile = _StringBetween( $fArray[$I], 'TITLE:' , 'by') $Rtn &= $Tile[0] & @CRLF Next $Bookinfo = @ScriptDir & "\Bookinfo.ini" IniWrite($Bookinfo, "Vars", "Tile", $Tile)I'd read your text in line by line, and use a combination of commands like FileReadLine, StringLeft, StringInStr, StringSplit, StringMid, etc with keywords like 'TITLE:' and 'by '. I'd also use StringReplace to return just what you want after checking with StringInStr. What you are doing, is pretty simple, so I wouldn't bother with arrays (except for that created by StringSplit if you use it). I'd also use FileOpen, FileClose, While, Wend, etc. When you get a handle on them, then you could start to master the intricacies of _FileReadToArray, which would be less code in the long run. You could also use _FileCountLines with For and Next, or exit the While loop on an empty string. i.e. $line = "TITLE: How to Make Money with Myspace" If StringInstr($line, "TITLE: ") > 0 Then $title = StringReplace($line, "TITLE: ", "") IniWrite($Bookinfo, "Vars", "Title", $title) There is so many ways to do the above, with StringLeft replacing StringInStr for instance. Make sure brain is in gear before opening mouth! Remember, what is not said, can be just as important as what is said. Spoiler What is the Secret Key? Life is like a Donut If I put effort into communication, I expect you to read properly & fully, or just not comment. Ignoring those who try to divert conversation with irrelevancies. If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it. I'm only big and bad, to those who have an over-active imagination. I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage) Link to comment Share on other sites More sharing options...
Herb191 Posted April 19, 2009 Author Share Posted April 19, 2009 I'd read your text in line by line, and use a combination of commands like FileReadLine, StringLeft, StringInStr, StringSplit, StringMid, etc with keywords like 'TITLE:' and 'by '. I'd also use StringReplace to return just what you want after checking with StringInStr. What you are doing, is pretty simple, so I wouldn't bother with arrays (except for that created by StringSplit if you use it). I'd also use FileOpen, FileClose, While, Wend, etc. When you get a handle on them, then you could start to master the intricacies of _FileReadToArray, which would be less code in the long run. You could also use _FileCountLines with For and Next, or exit the While loop on an empty string.i.e.$line = "TITLE: How to Make Money with Myspace"If StringInstr($line, "TITLE: ") > 0 Then $title = StringReplace($line, "TITLE: ", "")IniWrite($Bookinfo, "Vars", "Title", $title)There is so many ways to do the above, with StringLeft replacing StringInStr for instance.Thanks it took awhile but I got it working. This is what I ended up doing:$testfile = FileOpen("testfile.txt", 0)$lineNumber = 0$Bookinfo = @ScriptDir & "\Bookinfo.ini"While 1 $lineNumber = ($lineNumber + 1) $line = FileReadLine($testfile, $lineNumber) If @error = -1 Then ExitLoop Select Case StringInstr($line, "TITLE: ") > 0 $title = StringReplace($line, "TITLE: ", "") IniWrite($Bookinfo, "Vars", "Title", $title) EndSelect Select Case StringInstr($line, "by ") > 0 $Author = StringReplace($line, "by ", "") IniWrite($Bookinfo, "Vars", "Author", $Author) EndSelect Select Case StringInstr($line, "ISBN: ") > 0 $ISBN = StringReplace($line, "ISBN: ", "") IniWrite($Bookinfo, "Vars", "ISBN", $ISBN) EndSelect Select Case StringInstr($line, "ISBN 13: ") > 0 $ISBN13 = StringReplace($line, "ISBN 13: ", "") IniWrite($Bookinfo, "Vars", "ISBN13", $ISBN13) EndSelect Select Case StringInstr($line, "Publisher: ") > 0 $Publisher = StringReplace($line, "Publisher: ", "") IniWrite($Bookinfo, "Vars", "Publisher", $Publisher) EndSelect Select Case StringInstr($line, "Publish Date: ") > 0 $PublishDate = StringReplace($line, "Publish Date: ", "") IniWrite($Bookinfo, "Vars", "Publish Date", $Publisher) EndSelect Select Case StringInstr($line, "Binding: ") > 0 $Binding = StringReplace($line, "Binding: ", "") IniWrite($Bookinfo, "Vars", "Binding ", $Binding) EndSelect Select Case StringInstr($line, "List Price: USD ") > 0 $ListPrice = StringReplace($line, "List Price: USD ", "") IniWrite($Bookinfo, "Vars", "List Price", $ListPrice) EndSelect FileClose("testfile.txt") Wend Link to comment Share on other sites More sharing options...
ResNullius Posted April 20, 2009 Share Posted April 20, 2009 (edited) @Herb191,Since each of your Select statements only has 1 Case, you'rE probably better off with simple If statements.If StringInstr($line, "TITLE: ") > 0 then $title = StringReplace($line, "TITLE: ", "") IniWrite($Bookinfo, "Vars", "Title", $title) EndIfetc.And just because I know TheSaint has more than a mild dislike for the complexities of regular expressions $file = FileRead("testfile.txt") $RegExpSearch = "(?i)(?:(Title|ISBN.*|Pub.*|Binding)(?::\s*))|(?:Weight.*\n(?=(List Price)(?::)).*(?:.*\s)(?=\d.*\d))" $RegExpReplace = "\1\2 = " $file = StringRegExpReplace($file, $RegExpSearch, $RegExpReplace) $RegExpSearchAuthour = "(?i)((?:.*\n)*)(by\s*)" $RegExpReplaceAuthor = "\1Author = " $file = StringRegExpReplace($file, $RegExpSearchAuthour, $RegExpReplaceAuthor) $bookIni = FileOpen(@ScriptDir & "\Bookinfo.ini", 2) FileWriteLine($bookIni, "[Vars]") FileWriteLine($bookIni, $file) FileClose($bookIni)Edit:fixed code tags Edited April 20, 2009 by ResNullius Link to comment Share on other sites More sharing options...
TheSaint Posted April 20, 2009 Share Posted April 20, 2009 And just because I know TheSaint has more than a mild dislike for the complexities of regular expressions Now you've done it! You've given me a headache ... and Herb191 the power to destroy the world! Make sure brain is in gear before opening mouth! Remember, what is not said, can be just as important as what is said. Spoiler What is the Secret Key? Life is like a Donut If I put effort into communication, I expect you to read properly & fully, or just not comment. Ignoring those who try to divert conversation with irrelevancies. If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it. I'm only big and bad, to those who have an over-active imagination. I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage) Link to comment Share on other sites More sharing options...
Herb191 Posted April 21, 2009 Author Share Posted April 21, 2009 @Herb191, Since each of your Select statements only has 1 Case, you'rE probably better off with simple If statements. If StringInstr($line, "TITLE: ") > 0 then $title = StringReplace($line, "TITLE: ", "") IniWrite($Bookinfo, "Vars", "Title", $title) EndIfetc. And just because I know TheSaint has more than a mild dislike for the complexities of regular expressions $file = FileRead("testfile.txt") $RegExpSearch = "(?i)(?:(Title|ISBN.*|Pub.*|Binding)(?::\s*))|(?:Weight.*\n(?=(List Price)(?::)).*(?:.*\s)(?=\d.*\d))" $RegExpReplace = "\1\2 = " $file = StringRegExpReplace($file, $RegExpSearch, $RegExpReplace) $RegExpSearchAuthour = "(?i)((?:.*\n)*)(by\s*)" $RegExpReplaceAuthor = "\1Author = " $file = StringRegExpReplace($file, $RegExpSearchAuthour, $RegExpReplaceAuthor) $bookIni = FileOpen(@ScriptDir & "\Bookinfo.ini", 2) FileWriteLine($bookIni, "[Vars]") FileWriteLine($bookIni, $file) FileClose($bookIni) Edit:fixed code tags Thanks for the suggestionsand when I have time Im going to go though that code line by line to see if I can even under stand it. Link to comment Share on other sites More sharing options...
Authenticity Posted April 21, 2009 Share Posted April 21, 2009 (edited) Just one thing which may help to fail faster..."(?i)(?m)^(?:(?:(Title|ISBN.*|Pub.*|Binding)(?::\s*))|(?:Weight.*\n(?=(List Price)(?:).*(?:.*\s)(?=\d.*\d)))"Edit: ..or remove the bold parts and use '\G' after the (?i) so it fails if it needs to bump-along to the next char if it can't match at the current position. Edited April 21, 2009 by Authenticity 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