Jump to content

Write variable value to file


Recommended Posts

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)

 

 

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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

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