Jump to content
tecnomancer

Write variable value to file

Recommended Posts

tecnomancer

Hi

New to autoit and I'm trying to automate some stuff for my thesis

Need a little help.

I have msg box with webpage response time

When I'm trying to write this values to file I have just empty file

This is part of longer code but this other parts are working fine

 

; Display Information for all Links
#include <IE.au3>
#include <File.au3>
#include <Array.au3>
#include <String.au3>
#include <FileConstants.au3>

$oIE = _IECreate("https://www.google.ie/search?q=google.ie+hurricane+irma")
$sText = _IEBodyReadText ($oIE)
$data1 = _StringBetween($sText, 'About ', ')')
$ss1 = StringSplit($sText, 'Search Results',1)
$ss2 = StringSplit($ss1[2],@CRLF,1)
$data2 = StringReplace(StringReplace($ss2[1],' ...',@CRLF),'...Cached','')&@CRLF&$ss2[3]
MsgBox(0, "", $data1[0]&')',$data2)

; Open the file for writing (append to the end of a file) and store the handle to a variable.
    Local $hFileOpen = FileOpen("C:test.txt", $FO_APPEND)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
        Return False
    EndIf

; Write data to the file using the handle returned by FileOpen.
    FileWrite($hFileOpen, "Line 2")
    FileWrite($hFileOpen, "This is still line 2 as a new line wasn't appended to the last FileWrite call." & @CRLF)
    FileWrite($hFileOpen, "someone " & $data1 & "" & @CRLF)
    FileWrite($hFileOpen, "Line 4")

    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)

 

 

Share this post


Link to post
Share on other sites
Subz

You may require #RequireAdmin at the top of your script to write to C:\ for example another way to write Google Search results:

#RequireAdmin
#include <IE.au3>

$oIE = _IECreate("https://www.google.ie/search?q=google.ie+hurricane+irma")
$oIEId = _IEGetObjById($oIE, "rso")
$oDivId = _IETagNameGetCollection($oIEId, "div")

Local $hFileOpen = FileOpen("C:\Test.txt", 1)
    If $hFileOpen = -1 Then Exit MsgBox(16, "Error", "An error occurred whilst writing the temporary file.")

For $oDiv in $oDivId
    If $oDiv.ClassName = "g" Then
        FileWrite($hFileOpen, StringReplace(StringReplace(StringReplace(_IEPropertyGet($oDiv, "innertext"), @CRLF & @CRLF & @CRLF, @CRLF), @CRLF & @CRLF, @CRLF), "cached" & @CRLF, "") & @CRLF)
    EndIf
Next

FileClose($hFileOpen)

 

Share this post


Link to post
Share on other sites
tecnomancer

@Subz

Thank you but not exactly I'm looking for

I do not understand why variable $data1 and $data2 have value when displayed on msg box,

but do not have any value when I'm trying to save them to file

 

Share this post


Link to post
Share on other sites
Subz

a. You used $data1[0] in the MsgBox and then $data1 in FileWrite

b. You used $data2 in your MsgBox, but $data2 returns a blank string, it also wasn't included in FileWrite

  • Like 1

Share this post


Link to post
Share on other sites
tecnomancer
44 minutes ago, Subz said:

a. You used $data1[0] in the MsgBox and then $data1 in FileWrite

b. You used $data2 in your MsgBox, but $data2 returns a blank string, it also wasn't included in FileWrite

Thank you :)

I have to learn a lot.

Seems I will be busy till May

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

×