Sign in to follow this  
Followers 0
Azu

Readfiletovar

19 posts in this topic

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

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.. :nuke:

Share this post


Link to post
Share on other sites



#2 ·  Posted (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

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.. :nuke:

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 by Paulie

Share this post


Link to post
Share on other sites

#3 ·  Posted (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 :P

Edited by Azu

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

---------------------------

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

---------------------------

Share this post


Link to post
Share on other sites

---------------------------

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>

Share this post


Link to post
Share on other sites

#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

Share this post


Link to post
Share on other sites

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)

:P

Share this post


Link to post
Share on other sites

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)

:P

Thanks! I just tried that, and it works perfectly! Now I just need to find that UDF... :nuke:

Share this post


Link to post
Share on other sites

Not sure, but the UDF you mention sounds like StringInBetween() or similar named that SmOke_N uses often.

Share this post


Link to post
Share on other sites

Thanks, I found it, but I can't get it to work, where'd I mess up? :P

; 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

Share this post


Link to post
Share on other sites

#12 ·  Posted (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 by dabus

Share this post


Link to post
Share on other sites

$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 :P

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 is

desktopHeight=1024

desktopPrimary=0

desktopSetup=1

desktopWidth=1280

It gets changed to

desktopHeight=1024,desktopPrimary=0,desktopSetup=1,desktopWidth=1280

which 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.

Share this post


Link to post
Share on other sites

#18 ·  Posted (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 by randallc

Share this post


Link to post
Share on other sites

#19 ·  Posted (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 by Azu

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0