Azu Posted August 28, 2006 Share Posted August 28, 2006 How do I make autoit read a whole file into a single variable? I know it can be read into an array, but that basically makes a separate variable for each and every single line in the file. I need to just put the whole file into a variable. And also have all of the newlines converted to commas "," in the variable (but not change the file itself, just the data in the variable) I've looked around in the help file for a while but the closet thing I can find is reading it to an array which isn't what I'm looking for.. Please help P.S. I remember seeing a UDF on here somewhere before, but I forget what it's called and who posted it.. I think you pointed it at a string, and told it a beginning and an end, and it would return what is in between the two, or something like that. The file I want to use this on is basically a bunch of a=1 b=2 3=c etc. There is no brackets [] in it. So basically I want it so it's read into a variable and ends up like this a=1,b=2,3=c, So I can use the UDF, set the first part to b= and the last to , and it would return 2. The values in this file aren't always on the same lines, and the file is automatically changed a lot, which is why I need it to all be put into one variable rather then sorted by lines. I would also appreciated it if someone could remind me what the name of that UDF is so I can find it again.. Link to comment Share on other sites More sharing options...
Paulie Posted August 28, 2006 Share Posted August 28, 2006 (edited) How do I make autoit read a whole file into a single variable? I know it can be read into an array, but that basically makes a separate variable for each and every single line in the file. I need to just put the whole file into a variable. And also have all of the newlines converted to commas "," in the variable (but not change the file itself, just the data in the variable) I've looked around in the help file for a while but the closet thing I can find is reading it to an array which isn't what I'm looking for.. Please help P.S. I remember seeing a UDF on here somewhere before, but I forget what it's called and who posted it.. I think you pointed it at a string, and told it a beginning and an end, and it would return what is in between the two, or something like that. The file I want to use this on is basically a bunch of a=1 b=2 3=c etc. There is no brackets [] in it. So basically I want it so it's read into a variable and ends up like this a=1,b=2,3=c, So I can use the UDF, set the first part to b= and the last to , and it would return 2. The values in this file aren't always on the same lines, and the file is automatically changed a lot, which is why I need it to all be put into one variable rather then sorted by lines. I would also appreciated it if someone could remind me what the name of that UDF is so I can find it again.. So this doesn't do what you need $File = FileOpen("FileName") $Int = _FileCountLines($File) For $Bob = 1 to $Int $Tom[$Bob] = FileReadline($File, $Bob) $Contents = $Contents &$Tom[$Bob] Next MsgBox(0, "Contents", $Contents) Excuse incorrect parameters, just a generic concept Edited August 28, 2006 by Paulie Link to comment Share on other sites More sharing options...
Azu Posted August 28, 2006 Author Share Posted August 28, 2006 (edited) Thanks, adding dim $Tom[9999] fixes that, but there is another problem.. --------------------------- AutoIt Error --------------------------- Line 4 (File "C:\Documents and Settings\Administrator\Desktop\New AutoIt v3 Script.au3"): $Int = FileCountLines($File) $Int = ^ ERROR Error: Unknown function name. --------------------------- OK ---------------------------So I'm guessing there isn't function to count file lines.. And the newlines don't get converted.. :S BTW can you please tell me what that UDF is called? I'm having trouble searching for it since I don't remember it's name Edited August 28, 2006 by Azu Link to comment Share on other sites More sharing options...
Paulie Posted August 28, 2006 Share Posted August 28, 2006 Yeah i forgot the underscore in that function sorry As for the UDF, i have no idea what its called... but i edited the other code Link to comment Share on other sites More sharing options...
Azu Posted August 28, 2006 Author Share Posted August 28, 2006 --------------------------- AutoIt Error --------------------------- Line 4 (File "C:\Documents and Settings\Administrator\Desktop\New AutoIt v3 Script.au3"): $Int = _FileCountLines("prefs.ini") $Int = ^ ERROR Error: Unknown function name. --------------------------- OK --------------------------- Link to comment Share on other sites More sharing options...
Paulie Posted August 28, 2006 Share Posted August 28, 2006 --------------------------- AutoIt Error --------------------------- Line 4 (File "C:\Documents and Settings\Administrator\Desktop\New AutoIt v3 Script.au3"): $Int = _FileCountLines("prefs.ini") $Int = ^ ERROR Error: Unknown function name. --------------------------- OK ---------------------------LOL forgot this at top of script #Include <File.au3> Link to comment Share on other sites More sharing options...
Azu Posted August 28, 2006 Author Share Posted August 28, 2006 #include<file.au3> #include<misc.au3> dim $Tom[999] _FileReadToArray("prefs.ini",$Tom) $Blah = _ArrayToString($Tom,",") MsgBox(0,"Contents",$Blah)--------------------------- AutoIt Error --------------------------- Line 5 (File "C:\Documents and Settings\Administrator\Desktop\New AutoIt v3 Script.au3"): $Blah = _ArrayToString($Tom,",") $Blah = ^ ERROR Error: Unknown function name. --------------------------- OK --------------------------- T_T Link to comment Share on other sites More sharing options...
MHz Posted August 28, 2006 Share Posted August 28, 2006 Here is another method using FileRead and StringReplacing the @CRLF's. The rest is just cleanup excess comma's on either end of the result. ; Read whole file and change @CRLF to comma's $variable = StringReplace(FileRead(@ScriptDir & '\test.txt'), @CRLF, ',') ; Cleanup excess comma's on left side While StringLeft($variable, 1) = ',' $variable = StringTrimLeft($variable, 1) WEnd ; Cleanup excess comma's on right side While StringRight($variable, 1) = ',' $variable = StringTrimRight($variable, 1) WEnd ; Show result MsgBox(0, '', $variable) Link to comment Share on other sites More sharing options...
Azu Posted August 28, 2006 Author Share Posted August 28, 2006 Here is another method using FileRead and StringReplacing the @CRLF's. The rest is just cleanup excess comma's on either end of the result. ; Read whole file and change @CRLF to comma's $variable = StringReplace(FileRead(@ScriptDir & '\test.txt'), @CRLF, ',') ; Cleanup excess comma's on left side While StringLeft($variable, 1) = ',' $variable = StringTrimLeft($variable, 1) WEnd ; Cleanup excess comma's on right side While StringRight($variable, 1) = ',' $variable = StringTrimRight($variable, 1) WEnd ; Show result MsgBox(0, '', $variable) Thanks! I just tried that, and it works perfectly! Now I just need to find that UDF... Link to comment Share on other sites More sharing options...
MHz Posted August 28, 2006 Share Posted August 28, 2006 Not sure, but the UDF you mention sounds like StringInBetween() or similar named that SmOke_N uses often. Link to comment Share on other sites More sharing options...
Azu Posted August 28, 2006 Author Share Posted August 28, 2006 Thanks, I found it, but I can't get it to work, where'd I mess up? ; Read whole file and change @CRLF to comma's $variable = StringReplace(FileRead(@ScriptDir & '\test.txt'), @CRLF, ',') ; Cleanup excess comma's on left side While StringLeft($variable, 1) = ',' $variable = StringTrimLeft($variable, 1) WEnd ; Cleanup excess comma's on right side While StringRight($variable, 1) = ',' $variable = StringTrimRight($variable, 1) WEnd ; Show result MsgBox(0, "",$variable) MsgBox(0, "",_SRE_BetweenEX($variable, 'start', ',') ) Func _SRE_BetweenEX($s_String, $s_Start, $s_End, $iCase = 'i') If $iCase <> 'i' Then $iCase = '' $a_Array = StringRegExp($s_String, '(?' & $iCase & _ ':' & $s_Start & ')(.*?)(?' & $iCase & _ ':' & $s_End & ')', 3) If @extended & IsArray($a_Array) Then Return $a_Array Return SetError(1, 0, 0) EndFunc sdfsdf start blah blah blah sfsdfs sdfsdf Link to comment Share on other sites More sharing options...
dabus Posted August 28, 2006 Share Posted August 28, 2006 (edited) I bet you want to read a unicode-file. Try this: $filename = "log.txt" $txt = FileRead($filename, FileGetSize($filename)) MsgBox (0, 'Sample', $txt) and tell us if it shows the correct "text". Edited August 28, 2006 by dabus Link to comment Share on other sites More sharing options...
Azu Posted August 28, 2006 Author Share Posted August 28, 2006 $filename = "test.txtt" $txt = FileRead($filename, FileGetSize($filename)) MsgBox (0, 'Sample', $txt) part in t he code I posted above does. :/ I'm not sure whether it's unicode or not but I wish this stuff would work LOL Link to comment Share on other sites More sharing options...
randallc Posted August 28, 2006 Share Posted August 28, 2006 Hi Your "return" is an array; #include<array.au3> $ar_Result=_SRE_BetweenEX($variable, 'start', ',') if IsArray($ar_Result) then _ArrayDisplay($ar_Result,"$ar_Result") if not IsArray($ar_Result) then MsgBox(0, "",$ar_Result)Randall ExcelCOM... AccessCom.. Word2... FileListToArrayNew...SearchMiner... Regexps...SQL...Explorer...Array2D.. _GUIListView...array problem...APITailRW Link to comment Share on other sites More sharing options...
Azu Posted August 28, 2006 Author Share Posted August 28, 2006 Thanks, it's kinda working now, except that it's adding [0] = in front of everything.. :S #include<array.au3> ; Read whole file and change @CRLF to comma's $variable = StringReplace(FileRead(@ScriptDir & '\test.txt'), @CRLF, ',') ; Cleanup excess comma's on left side While StringLeft($variable, 1) = ',' $variable = StringTrimLeft($variable, 1) WEnd ; Cleanup excess comma's on right side While StringRight($variable, 1) = ',' $variable = StringTrimRight($variable, 1) WEnd ; Show result MsgBox(0, "",$variable) $variable=_SRE_BetweenEX($variable, 'start', ',') _ArrayDisplay($variable,$variable) Func _SRE_BetweenEX($s_String, $s_Start, $s_End, $iCase = 'i') If $iCase <> 'i' Then $iCase = '' $a_Array = StringRegExp($s_String, '(?' & $iCase & _ ':' & $s_Start & ')(.*?)(?' & $iCase & _ ':' & $s_End & ')', 3) If @extended & IsArray($a_Array) Then Return $a_Array Return SetError(1, 0, 0) EndFunc Link to comment Share on other sites More sharing options...
randallc Posted August 28, 2006 Share Posted August 28, 2006 Only inn the "Array Display" box; as it is array item "0"; if you find more than one match , they will be $ar_Result[1], $ar_Result[2], etc. Just write those values to the console, or a file, or wherever you are going to use them. Randall ExcelCOM... AccessCom.. Word2... FileListToArrayNew...SearchMiner... Regexps...SQL...Explorer...Array2D.. _GUIListView...array problem...APITailRW Link to comment Share on other sites More sharing options...
Azu Posted August 28, 2006 Author Share Posted August 28, 2006 Okay.. but I don't really want a message box.. I was just using the message boxes to quickly see if it was working.. I need to get a string with only whatever is between the two strings, so E.G. if the file isdesktopHeight=1024desktopPrimary=0desktopSetup=1desktopWidth=1280It gets changed todesktopHeight=1024,desktopPrimary=0,desktopSetup=1,desktopWidth=1280which works fine and then E.G. $variable=_SRE_BetweenEX($variable, 'desktopHeight', ',')And it will make $variable=1024 which the script will need to be able to divide, multiply, add, subtract, etc; a normal variable in other words.. if there there is other stuff in it it won't work..On a side note, I'm also going to have a comma added to the end of $variable using $variable=$variable&"," before I do the $variable=_SRE_BetweenEX($variable, 'desktopHeight', ',') since there isn't a newline at the end of this file so so the _SRE_BetweenEX wouldn't work on that last line.I'm not sure if this makes a difference in matters but I just thought I'd mention it. Link to comment Share on other sites More sharing options...
randallc Posted August 28, 2006 Share Posted August 28, 2006 (edited) $variable=_SRE_BetweenEX($variable, 'desktopHeight', ',')becomes $desktopHeight=$variable[0] )(or $desktop_variable=$variable[0]) - but you can just as easily use "$variable[0]) /10" as "$variableX/10" Randall Edited August 29, 2006 by randallc ExcelCOM... AccessCom.. Word2... FileListToArrayNew...SearchMiner... Regexps...SQL...Explorer...Array2D.. _GUIListView...array problem...APITailRW Link to comment Share on other sites More sharing options...
Azu Posted August 28, 2006 Author Share Posted August 28, 2006 (edited) Thank you! It's working perfect now! Okay since noone is answering I'll just make a new topic for my next question ^^ Edited August 28, 2006 by Azu 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