Jump to content

File to string


Recommended Posts

How can i read the contents of a file into a string? I want to be able to extract text from the file using a REGEXP.

I have tried to load the file into an array first then convert it to a string using a line like this

$string1=_ArrayToString($configarray, 0 )

But it doesn't seem to load anything into the string, i am also reading the string out to a log file and it comes up blank as well.

Link to comment
Share on other sites

Thank you for the *extremely* quick response that makes it allot easier.

One more question for you the 3rd varible passed to StringRegExp is supposed to define how the result is output from what i have read, but i didnt ever see it explicitly defined which number 0-3 i think gives you what kind of output. I seem to have the number of results returned to me rather than the actual blocks of text.

Link to comment
Share on other sites

  • Moderators

Thank you for the *extremely* quick response that makes it allot easier.

One more question for you the 3rd varible passed to StringRegExp is supposed to define how the result is output from what i have read, but i didnt ever see it explicitly defined which number 0-3 i think gives you what kind of output. I seem to have the number of results returned to me rather than the actual blocks of text.

Ok, this is going to sound rude, but that's just how I type, take it how you want.

1. I have absolutely no idea wth you just said.

2. I think you should provide a test "txt" file.

3. With that test txt file, provide what you would like to find.

4. Any other coherent reply that we can help you with.

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

Ok, this is going to sound rude, but that's just how I type, take it how you want.

1. I have absolutely no idea wth you just said.

2. I think you should provide a test "txt" file.

3. With that test txt file, provide what you would like to find.

4. Any other coherent reply that we can help you with.

It's not rude, i do sometimes have the bad habit of just allowing my thoughts to flow in a very unstructured manner when im typeing something.

#include<file.au3>
#include<array.au3>

$configfile="c:\config.js"
$addonout="c:\addonout\"
$logfile="wpiaddonmaker.log"
$lines=_FileCountLines($configfile)
$configarray=_ArrayCreate(100, 100)
$simplereg="prog\[pn]=\['.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+\npn\+\+;"
$string = FileRead($configfile)



;_FileWriteLog($logfile, $string)

_FileWriteLog($logfile, StringRegExp($string, $simplereg, 1))

$configarry=StringRegExp($string, $simplereg, 1)

Thats what i have so far. Basically the program is designed to help the ppl over at Ryanvm.net in the creation of WPIaddons. And the program is supposed to take a currently working config.js file and convert all the install programs into a set of addon files.

Link to comment
Share on other sites

  • Moderators

Yeah, we are still back to ground 0... as far as me helping anyway.

1. I need the config.js file

2. I need the desired output file, so I can understand what and how it's supposed to work.

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

Yeah, we are still back to ground 0... as far as me helping anyway.

1. I need the config.js file

2. I need the desired output file, so I can understand what and how it's supposed to work.

At this point the desired output would be either to another file or even better to an array of strings so that i could run another regexp against each of the outputs separatly.

Also the config.js file is attached. Just had to change the extention so it would allow me to upload it.

Edited by toyotabedzrock
Link to comment
Share on other sites

  • Moderators

At this point the desired output would be either to another file or even better to an array of strings so that i could run another regexp against each of the outputs separatly.

Also the config.js file is attached. Just had to change the extention so it would allow me to upload it.

Ok toyota... I'll try to be more patient then I usually am.

Lets look at this logically.

1. Now we have a file to work with ;)

2. We still have no idea what the H**l you want. I mean the actual out put. \n. doesn't tell us anything other than you want basically (.*?) every single character of the file.

3. You keep saying to use StringRegExp(), but we have no idea how you want to use it.

Do this.

1. Make a "short" fake .js file.

2. Make a "short" fake result file of what you would like that result file to pull out of the fake .js file.

If it's cloak and dagger you want, your doing a good job :whistle:. I'm willing to help here because I like SRE (reg exp.) but, I'm growing weary on the lack of information necessary to help you.

Edit:

Also, you are aware that SRE returns an array when you use 1 and 3 right?

Edited 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

No i wansnt aware of that. Im still very new to autoit and i havnt actualy touched C++ for at least 4 years now so im very rusty and i am grateful for the patience.

Im trying to extract blocks of code from the config.js file.

by a block i mean this

prog[pn]=['7-Zip 4.42'];
desc[pn]=['7-Zip is a file archiver with high compression ratio.'];
uid[pn]=['7-Zip'];
dflt[pn]=['yes'];
cat[pn]=['Utilities'];
forc[pn]=['no'];
configs[pn]=['yes'];
gcond[pn]=['FileExists(\'%programfiles%\\\')'];
cmd1[pn]=['%wpipath%\\Install\\7-Zip.exe'];
pn++;

Can you perhaps show me how to extract the into an array. The reg expretion from the code you have seen already should be correct for it. Also when i made the regualar expresion .*? seemed to not work as expected which is why i have the multiple \n in the middle of it.

Link to comment
Share on other sites

  • Moderators

Hmm, I don't know if that is backwards or not, but give this a whirl:

$FileName = @DesktopDir & '\fakeinput.txt'
MsgBox(64, 'Output', _IDontKnowWhatToCallThis($FileName))

Func _IDontKnowWhatToCallThis($hFile)
    If FileExists($hFile) Then $hFile = FileRead($hFile)
    Local $aSplit = StringSplit(StringStripCR($hFile), @LF)
    Local $sHold, $iAdd
    Local $sInsertHead = ";begining of an array element "
    Local $sInsertFoot = "; end of array element "
    
    For $iCC = 1 To UBound($aSplit) - 1
        If StringLeft($aSplit[$iCC], 9) = "prog[pn]=" Then
            $iAdd += 1
            $sHold &= $sInsertHead & $iAdd & @CRLF & $aSplit[$iCC] & @CRLF
            ContinueLoop
        EndIf
        If StringLeft($aSplit[$iCC], 6) = "pn++;" Then
            $sHold &= $aSplit[$iCC] & @CRLF & $sInsertFoot & $iAdd & @CRLF
            ContinueLoop
        EndIf
        $sHold &= $aSplit[$iCC] & @CRLF
    Next
    If $sHold Then Return StringTrimRight($sHold, 2)
    Return SetError(1, 0, 0)
EndFunc
I didn't use SRE and don't know where you expected to either.

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

Hmm, I don't know if that is backwards or not, but give this a whirl:

$FileName = @DesktopDir & '\fakeinput.txt'
MsgBox(64, 'Output', _IDontKnowWhatToCallThis($FileName))

Func _IDontKnowWhatToCallThis($hFile)
    If FileExists($hFile) Then $hFile = FileRead($hFile)
    Local $aSplit = StringSplit(StringStripCR($hFile), @LF)
    Local $sHold, $iAdd
    Local $sInsertHead = ";begining of an array element "
    Local $sInsertFoot = "; end of array element "
    
    For $iCC = 1 To UBound($aSplit) - 1
        If StringLeft($aSplit[$iCC], 9) = "prog[pn]=" Then
            $iAdd += 1
            $sHold &= $sInsertHead & $iAdd & @CRLF & $aSplit[$iCC] & @CRLF
            ContinueLoop
        EndIf
        If StringLeft($aSplit[$iCC], 6) = "pn++;" Then
            $sHold &= $aSplit[$iCC] & @CRLF & $sInsertFoot & $iAdd & @CRLF
            ContinueLoop
        EndIf
        $sHold &= $aSplit[$iCC] & @CRLF
    Next
    If $sHold Then Return StringTrimRight($sHold, 2)
    Return SetError(1, 0, 0)
EndFunc
I didn't use SRE and don't know where you expected to either.

the ";begining of array element" and ";end of array element" was supposed to be a visual aid to show what i wanted in each part of the array.

Also i will be using SRE more on the individual sections of the array. So i can pull out selected values from them.

On a side note there are 2 great REGEXP tools i recently found. The guys webpage acts like its running on a 486 but the tools Regulator and Regulazy are very useful.

http://regulator.sourceforge.net/

Edited by toyotabedzrock
Link to comment
Share on other sites

  • Moderators

the ";begining of array element" and ";end of array element" was supposed to be a visual aid to show what i wanted in each part of the array.

Also i will be using SRE more on the individual sections of the array. So i can pull out selected values from them.

On a side note there are 2 great REGEXP tools i recently found. The guys webpage acts like its running on a 486 but the tools Regulator and Regulazy are very useful.

http://regulator.sourceforge.net/

Didn't even bother to to try?
#include <array.au3>
$FileName = @DesktopDir & '\fakeinput.txt'
$aArray = _IDontKnowWhatToCallThis($FileName)
_ArrayDisplay($aArray, 'here')
Func _IDontKnowWhatToCallThis($hFile)
    If FileExists($hFile) Then $hFile = FileRead($hFile)
    Local $aSplit = StringSplit(StringStripCR($hFile), @LF)
    Local $sHold, $iCC, $iAdd = 1, $aArray[1]
    
    For $iCC = 1 To UBound($aSplit) - 1
        If StringLeft($aSplit[$iCC], 9) = "prog[pn]=" Then
            Do
                $sHold &= $aSplit[$iCC] & @CRLF
                $iCC += 1
            Until StringLeft($aSplit[$iCC], 6) = "pn++;"
            $sHold &= $aSplit[$iCC]
            $iAdd += 1
            ReDim $aArray[$iAdd]
            $aArray[$iAdd - 1] = $sHold
            $sHold = ''
        EndIf
    Next
    Return $aArray
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

  • Moderators

I also wrote this _StrBetween() function today, but you'll see it excludes "all" the info as I have it presented, maybe you'd like to play with that

#include <array.au3>
$FileName = @DesktopDir & '\fakeinput.txt'
$aArray = _StrBetween(FileRead($FileName), 'prog\[pn\]\=', 'pn\+\+\;', -1, 1)
_ArrayDisplay($aArray, 'here')

Func _StrBetween($sString, $sStart, $sEnd, $vCase = -1, $iSRE = -1)
    If $iSRE = -1 Or $iSRE = Default Then
        If $vCase = -1 Or $vCase = Default Then $vCase = 0
        If $vCase <> -1 And $vCase <> Default Then $vCase = 1
        Local $sHold = '', $sSnSStart = '', $sSnSEnd = ''
        While StringLen($sString) > 0
            $sSnSStart = StringInStr($sString, $sStart, $vCase)
            If Not $sSnSStart Then ExitLoop
            $sString = StringTrimLeft($sString, ($sSnSStart + StringLen($sStart)) - 1)
            $sSnSEnd = StringInStr($sString, $sEnd, $vCase)
            If Not $sSnSEnd Then ExitLoop
            $sHold &= StringLeft($sString, $sSnSEnd - 1) & Chr(1)
            $sString = StringTrimLeft($sString, $sSnSEnd)
        WEnd
        If Not $sHold Then Return SetError(1, 0, 0)
        $sHold = StringSplit(StringTrimRight($sHold, 1), Chr(1))
        Local $aArray[UBound($sHold) - 1]
        For $iCC = 1 To UBound($sHold) - 1
            $aArray[$iCC - 1] = $sHold[$iCC]
        Next
        Return $aArray
    Else
        If $vCase = Default Or $vCase = -1 Then $vCase = '(?i)'
        If $vCase <> Default And $vCase <> -1 Then $vCase = ''
        Local $aArray = StringRegExp($sString, '(?s)' & $vCase & $sStart & '(.*?)' & $sEnd, 3)
        If IsArray($aArray) Then Return $aArray
        Return SetError(1, 0, 0)
    EndIf
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

Didn't even bother to to try?
#include <array.au3>
$FileName = @DesktopDir & '\fakeinput.txt'
$aArray = _IDontKnowWhatToCallThis($FileName)
_ArrayDisplay($aArray, 'here')
Func _IDontKnowWhatToCallThis($hFile)
    If FileExists($hFile) Then $hFile = FileRead($hFile)
    Local $aSplit = StringSplit(StringStripCR($hFile), @LF)
    Local $sHold, $iCC, $iAdd = 1, $aArray[1]
    
    For $iCC = 1 To UBound($aSplit) - 1
        If StringLeft($aSplit[$iCC], 9) = "prog[pn]=" Then
            Do
                $sHold &= $aSplit[$iCC] & @CRLF
                $iCC += 1
            Until StringLeft($aSplit[$iCC], 6) = "pn++;"
            $sHold &= $aSplit[$iCC]
            $iAdd += 1
            ReDim $aArray[$iAdd]
            $aArray[$iAdd - 1] = $sHold
            $sHold = ''
        EndIf
    Next
    Return $aArray
EndFunc

I have no idea what the StringLeft function does Or the Ubound. Which is why i thought using a regexp function on a string would allow me to output a single dimentional array of strings.

Thats another thing that escapes me all the varibles in auto it seem to have no type?

Edited by toyotabedzrock
Link to comment
Share on other sites

  • Moderators

No i am having a hard time following it, like i said been over 4 years since i attempted anything in c++ and totaly new to autoit.

I have no idea what the StringLeft function does Or the Ubound. Which is why i thought using a regexp function on a string would allow me to output a single dimentional array of strings.

Thats another thing that escapes me all the varibles in auto it seem to have no type?

You could take some time and read up on each one of the functions that are used there so you can get a basic undertanding.

Can it be done with StringRegExp? I'm sure, but not as easy as it was for me to write the function you are referring to. The output is exactly what you are requesting, so I don't see the big deal.

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

You could take some time and read up on each one of the functions that are used there so you can get a basic undertanding.

Can it be done with StringRegExp? I'm sure, but not as easy as it was for me to write the function you are referring to. The output is exactly what you are requesting, so I don't see the big deal.

Its not a big deal. I just thought the amount of code would be less, and i wanted to learn more about the regexp as i went along :whistle:

Edited by toyotabedzrock
Link to comment
Share on other sites

  • Moderators

Its not just thought the code would be less and i wanted to learn more about the regexp as i went along :whistle:

http://perldoc.perl.org/perlre.html#Regular-Expressions

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

  • Moderators

What does the "here" mean in this line?

_ArrayDisplay($aArray, 'here')
Ok you tested the old patience level here. That's a question that can easily be answered by you.

http://www.autoitscript.com/autoit3/docs/

Your version of AutoIt did come with a .chm file right?

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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...