Jump to content

[UNSOLVED] Writing [SUCCESS] after each line


Recommended Posts

If $oStatusCode = 404 Then
_FileReadToArray("email.txt", $aInput[$i])
For $i = 1 To $aInput[$i]
    $aInput[$i] &= "[SUCCESS]"
Next

_FileWriteFromArray("checked.txt", $aInput[$i], 1)
EndIf

I am trying to write [SUCECSS] on each line in my email.txt file and store it as "Checked.txt"
I have written this code from my research here on this forum, but I just can't get it to work.
I've tried different ways of using array, also tried using "FileWriteLine" functions, nothing works out for me.

 

I hope someone can point me towards a solution and/or write the code I need with an explanation.
Thanks in advance.

Edited by RyukShini
[SOLVED]
Link to comment
Share on other sites

4 minutes ago, JohnOne said:
_FileWriteFromArray("checked.txt", $aInput, 1)

 

Now it writes $aInput on each lines of "checked.txt"

 

However it does not write [SUCCESS] after each lines.
And it does it for all of email.txt not just the ones giving response 404.

Thank you though, it made my progress go a bit further.

Link to comment
Share on other sites

8 minutes ago, JohnOne said:

And

_FileReadToArray("email.txt", $aInput)

And

For $i = 1 To $aInput[0]
    $aInput[$i] &= "[SUCCESS]"
Next

 

Now it writes [SUCCESS] after each line.
however it does it for all of email.txt.
It does not only write [SUCCESS] if the response code is 404.
Thanks a lot, I will try to fix it, but if more responds comes I wont complain!
 

Link to comment
Share on other sites

You're reading the entire contents of "email.txt" into a single array element (with a possibly undefined index), then trying to use that same array element as the upper bound on a for-loop index, which won't work: $aInput[$i] contains the data from "email.txt", not the line count. Perhaps what you meant is something like this:

If $oStatusCode = 404 Then
    _FileReadToArray("email.txt", $aInput)          ; Line count (max index) is in element 0
    For $i = 1 To $aInput[0]
        $aInput[$i] &= "[SUCCESS]"
    Next
    _FileWriteFromArray("checked.txt", $aInput, 1)  ; Begin writing from index = 1
EndIf

Give _FileReadToArray() an array variable with no index to read each line of "email.txt" into successive elements starting at index = 1; you need to specify a starting index of 1 for _FileWriteFromArray(), otherwise it will start with 0 and include the line count in the output to "checked.txt".

I haven't tested the code; you might need to append @CRLF to each element of the array (after "[SUCCESS]"). You might also want to put a space or tab character before "[SUCCESS]" for readability, otherwise it will immediately follow the last character in the input line (no intervening whitespace).

When the going gets tough, the tough start coding.

Link to comment
Share on other sites

21 minutes ago, tremolux66 said:

You're reading the entire contents of "email.txt" into a single array element (with a possibly undefined index), then trying to use that same array element as the upper bound on a for-loop index, which won't work: $aInput[$i] contains the data from "email.txt", not the line count. Perhaps what you meant is something like this:

If $oStatusCode = 404 Then
    _FileReadToArray("email.txt", $aInput)          ; Line count (max index) is in element 0
    For $i = 1 To $aInput[0]
        $aInput[$i] &= "[SUCCESS]"
    Next
    _FileWriteFromArray("checked.txt", $aInput, 1)  ; Begin writing from index = 1
EndIf

Give _FileReadToArray() an array variable with no index to read each line of "email.txt" into successive elements starting at index = 1; you need to specify a starting index of 1 for _FileWriteFromArray(), otherwise it will start with 0 and include the line count in the output to "checked.txt".

I haven't tested the code; you might need to append @CRLF to each element of the array (after "[SUCCESS]"). You might also want to put a space or tab character before "[SUCCESS]" for readability, otherwise it will immediately follow the last character in the input line (no intervening whitespace).

I have tried back and forth, I am not sure I understand.
Would you mind writing an example?
I process code easier than I process words to be honest.

Link to comment
Share on other sites

This example is pretty much the same as the code I posted earlier, and is based on the code fragment that was originally posted.

#cs ----------------
    success.au3
#ce ----------------
#include <File.au3>

Local $aInput[1]            ; Declare the array with 1 element just so AutoIt knows it's an array -
                            ;   _FileReadToArray() will grow the array to be big enough to hold all the
                            ;   lines of text from the file

Local $oStatusCode = 404    ; Set this variable to 404 to force the SUCCESS code to execute

If $oStatusCode = 404 Then
    ; Read the contents of "email.txt" into the array starting at index = 1
    ; -- $aInput[0] will hold the line count, and $aInput[1] ... $aInput[N] will hold lines 1-N
    ; -- For example: with 4 lines of text, $aInput = [4, line_1, line_2, line_3, line_4]
    _FileReadToArray("email.txt", $aInput)

    ; For every line in the file ...
    For $i = 1 to $aInput[0]
        ; ... append <space>[SUCCESS] to the line (no @CRLF needed)
        $aInput[$i] &= " [SUCCESS]"
    Next

    ; Write all the text lines in the array to "checked.txt"
    ; -- We set the 3rd argument to 1 so that it starts with index = 1 instead of 0
    _FileWriteFromArray("checked.txt", $aInput, 1)
EndIf

 

email.txt contents:

This is line 1.
Here is line 2!
This could be line 3...

 

checked.txt contents:

This is line 1. [SUCCESS]
Here is line 2! [SUCCESS]
This could be line 3... [SUCCESS]

 

When the going gets tough, the tough start coding.

Link to comment
Share on other sites

3 hours ago, tremolux66 said:

This example is pretty much the same as the code I posted earlier, and is based on the code fragment that was originally posted.

#cs ----------------
    success.au3
#ce ----------------
#include <File.au3>

Local $aInput[1]            ; Declare the array with 1 element just so AutoIt knows it's an array -
                            ;   _FileReadToArray() will grow the array to be big enough to hold all the
                            ;   lines of text from the file

Local $oStatusCode = 404    ; Set this variable to 404 to force the SUCCESS code to execute

If $oStatusCode = 404 Then
    ; Read the contents of "email.txt" into the array starting at index = 1
    ; -- $aInput[0] will hold the line count, and $aInput[1] ... $aInput[N] will hold lines 1-N
    ; -- For example: with 4 lines of text, $aInput = [4, line_1, line_2, line_3, line_4]
    _FileReadToArray("email.txt", $aInput)

    ; For every line in the file ...
    For $i = 1 to $aInput[0]
        ; ... append <space>[SUCCESS] to the line (no @CRLF needed)
        $aInput[$i] &= " [SUCCESS]"
    Next

    ; Write all the text lines in the array to "checked.txt"
    ; -- We set the 3rd argument to 1 so that it starts with index = 1 instead of 0
    _FileWriteFromArray("checked.txt", $aInput, 1)
EndIf

 

email.txt contents:

This is line 1.
Here is line 2!
This could be line 3...

 

checked.txt contents:

This is line 1. [SUCCESS]
Here is line 2! [SUCCESS]
This could be line 3... [SUCCESS]

 

Thanks, so far that is working fine, sorry for not being able to express myself proper.
It is already working as your example shows.
However it should only write [SUCCESS] on the ones giving status code 404.

So 

This is an example:

 

This is a line - It gave Response code 200
this is a line - same as above
this is a line - ^
this is a line [SUCCESS] - Response code was 404
this is a line - response code 200

Right now it takes all of the file and writes [SUCCESS] each line even thought I gave it if $statuscode = etc.
I am not sure what I am doing wrong.

Link to comment
Share on other sites

#cs ----------------
    success.au3
#ce ----------------
#include <File.au3>

Local $aInput[1]            ; Declare the array with 1 element just so AutoIt knows it's an array -
                            ;   _FileReadToArray() will grow the array to be big enough to hold all the
                            ;   lines of text from the file

Local $oStatusCode = 404    ; Set this variable to 404 to force the SUCCESS code to execute

If $oStatusCode = 404 Then
    ; Read the contents of "email.txt" into the array starting at index = 1
    ; -- $aInput[0] will hold the line count, and $aInput[1] ... $aInput[N] will hold lines 1-N
    ; -- For example: with 4 lines of text, $aInput = [4, line_1, line_2, line_3, line_4]
    _FileReadToArray("email.txt", $aInput)

    ; For every line in the file ...
    For $i = 1 to $aInput[0]
        $oStatusCode = Random(404, 408, 1)
        If Not $oStatusCode = 404 Then
            ; ... append <space>[SUCCESS] to the line (no @CRLF needed)
            $aInput[$i] &= " [SUCCESS]"
        EndIf
    Next

    ; Write all the text lines in the array to "checked.txt"
    ; -- We set the 3rd argument to 1 so that it starts with index = 1 instead of 0
    _FileWriteFromArray("checked.txt", $aInput, 1)
EndIf

 

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

15 hours ago, mikell said:

Is "email.txt" a list of addresses , for which you check status code and then write the result in "checked.txt" ?

It is a list of usernames actually, just never got to change the filename.
but it takes the username/email.txt and inserts it after .com/username
Then if the specific website gives me back a 404 response code(Page not found)
Then it should write [SUCCESS] after that username in the email.txt and/or save the [SUCCESS] in checked.txt

Link to comment
Share on other sites

Then the approach should be like this

Local $aInput
_FileReadToArray("email.txt", $aInput)  ; read the list of names to array

For $i = 1 To $aInput[0]   ; for each name
    ; here some code to send mail and check status code 
    ; ...
    If $oStatusCode = 404 Then $aInput[$i] &= " [SUCCESS]"  ; append to the address
Next
_FileWriteFromArray("checked.txt", $aInput, 1)  ; rewrite the array to file

 

Link to comment
Share on other sites

 

3 hours ago, mikell said:

Then the approach should be like this

Local $aInput
_FileReadToArray("email.txt", $aInput)  ; read the list of names to array

For $i = 1 To $aInput[0]   ; for each name
    ; here some code to send mail and check status code 
    ; ...
    If $oStatusCode = 404 Then $aInput[$i] &= " [SUCCESS]"  ; append to the address
Next
_FileWriteFromArray("checked.txt", $aInput, 1)  ; rewrite the array to file

 

I am not sure what I am doing wrong:

 

#include <String.au3>
#include <Array.au3>
#Include <File.au3>
Local $aInput
_FileReadToArray("email.txt", $aInput)  ; read the list of names to arra

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
For $i = 1 to UBound($aInput) -1
$oHTTP.Open("GET", "https:///" & $aInput[$i], False) ; Post url
$oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") ; Header data >
$oHTTP.SetRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
$oHTTP.SetRequestHeader("User-Agent", "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.52 YaBrowser/15.12.2490.3614 (beta) Yowser/2.5 Safari/537.36")
$oHTTP.SetRequestHeader("X-CSRFToken", "")
$oHTTP.SetRequestHeader("Referer", "https:///")
$oHTTP.SetRequestHeader("X-Requested-With", "XMLHttpRequest")
; Header data <

; Performing the Request
$oHTTP.Send
;Sends the post data with the given details

;Download the body response if any, and get the server status response code.
$oReceived = $oHTTP.ResponseText
$oStatusCode = $oHTTP.Status
Next
For $i = 1 To $aInput[0]   ; for each name
    If $oStatusCode = 404 Then
        $aInput[$i] &= " [SUCCESS]"  ; append to the address

_FileWriteFromArray("checked.txt", $aInput, 1)  ; rewrite the array to file
EndIf
Next
Sleep(3000)

 

Link to comment
Share on other sites

My (personal) feeling about your logic is that you should try something like this

#include <String.au3>
#include <Array.au3>
#Include <File.au3>

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
Local $aInput
_FileReadToArray("email.txt", $aInput)  ; read the list of names to array

For $i = 1 To $aInput[0]   ; for each name
    $oHTTP.Open("GET", "https:///" & $aInput[$i], False) ; Post url
    $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") ; Header data >
    $oHTTP.SetRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
    $oHTTP.SetRequestHeader("User-Agent", "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.52 YaBrowser/15.12.2490.3614 (beta) Yowser/2.5 Safari/537.36")
    $oHTTP.SetRequestHeader("X-CSRFToken", "")
    $oHTTP.SetRequestHeader("Referer", "https:///")
    $oHTTP.SetRequestHeader("X-Requested-With", "XMLHttpRequest")
    $oHTTP.Send
    $oReceived = $oHTTP.ResponseText
    $oStatusCode = $oHTTP.Status
    If $oStatusCode = 404 Then $aInput[$i] &= " [SUCCESS]"  ; append to the address
Next

$oHTTP = 0
_FileWriteFromArray("checked.txt", $aInput, 1)  ; rewrite the array to file

 

Edited by mikell
huge typo
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

×
×
  • Create New...