Sign in to follow this  
Followers 0
RyukShini

[UNSOLVED] Writing [SUCCESS] after each line

29 posts in this topic

#1 ·  Posted (edited)

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]

Share this post


Link to post
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.

Share this post


Link to post
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!
 

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Are you sending a EMail to all adresses in EMail.txt in a loop? I think it's better to show your whole script,because title and script snippet are saying you want to write success after each line.

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

JO, your code assumes that the answer to the question in post #10 is "yes" ... no ?  :)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
5 hours ago, JohnOne said:

Well, we are being forced to guess. :blink:

Thank you for all the support!

Share this post


Link to post
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

 

Share this post


Link to post
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)

 

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

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

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

  • Similar Content

    • ronmage
      By ronmage
      So I have a loop that keeps reading data from an array and searching it for the same value. If the value is no there it does work then adds the value to the array to prevent it from doing the same work.
      If _ArraySearch($ID,$filearray[$i]) = -1 Then Work.... _ArrayAdd($ID,$filearray[$i]) EndIf This is in a for loop hence $i
      So what is happening is the code works great for several hours. After a period of time _ArraySearch($ID,$filearray[$i]) will result in -1 even if $ID = $filearray. So it ready as if there is no data in the array. Anyone have this problem? 
       
      Also I am just running in using F5 not compiling it and running it if that makes a difference.
       
    • Ascer
      By Ascer
      1. Description.
      Udf working with MSDN System.Collections.ArrayList. Allow you to make fast operations on huge arrays, speed is even x10 better than basic _ArrayAdd.  Not prefered for small arrays < 600 items. 2. Requirements
      .NET Framework 1.1+ System Windows 3. Possibilities.
      ;=============================================================================================================== ; UDF Name: List.au3 ; ; Date: 2018-02-17, 10:52 ; Description: Simple udf to create System Collections as ArrayList and make multiple actions on them. ; ; Function(s): _ListCreate -> Creates a new list ; _ListCapacity -> Gets a list size in bytes ; _ListCount -> Gets items count in list ; _ListIsFixedSize -> Get bool if list if fixed size ; _ListIsReadOnly -> Get bool if list is read only ; _ListIsSynchronized -> Get bool if list is synchronized ; _ListGetItem -> Get item on index ; _ListSetItem -> Set item on index ; ; _ListAdd -> Add item at end of list ; _ListClear -> Remove all list items ; _ListClone -> Duplicate list in new var ; _ListContains -> Get bool if item is in list ; _ListGetHashCode -> Get hash code for list ; _ListGetRange -> Get list with items between indexs ; _ListIndexOf -> Get index of item ; _ListInsert -> Insert a new item on index ; _ListInsertRange -> Insert list into list on index ; _ListLastIndexOf -> Get index last of item ; _ListRemove -> Remove first found item ; _ListRemoveAt -> Remove item in index ; _ListRemoveRange -> Remove items between indexs ; _ListReverse -> Reverse all items in list ; _ListSetRange -> Set new value for items in range ; _ListSort -> Sort items in list (speed of reading) ; _ListToString -> Get list object name ; _ListTrimToSize -> Remove unused space in list ; ; Author(s): Ascer ;=============================================================================================================== 4. Downloads
      List.au3 5. Examples
      SpeedTest _ArrayAdd vs ListAdd SpeedTest ArraySearch vs ListIndexOf Basic usage - crating guild with members  
    • FMS
      By FMS
      Hello,
      I'm trying to read a binary file to an array but couln't get it to work.
      Also I coul not find any help in the forum around this subject whish was helpfull.
      Is there any way it could be done?
      I tried a lot of ways but maybe somebody know's the right way?
      #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <File.au3> #include <Array.au3> #include <AutoItConstants.au3> Local $in=FileOpen("TEST_labels.idx1-ubyte",16) ; 16+0=Read binary Local $data = FileRead($in) Local $FileArray = BinaryToString($data,4) ;~ $FileArray = StringSplit($BinarydData, @CRLF, 1+2) ;~ Local $FileArray = StringRegExp($BinarydData, "[^\r\n]+", 3) FileClose($in) _ArrayDisplay($FileArray,"$FileArray","",32) MsgBox($MB_SYSTEMMODAL, "", "$FileArray = " & $FileArray )  
      TEST_labels.idx1-ubyte
    • ur
      By ur
      I am reading a CSV file which is tab seperated as below.
      Local $aArray = FileReadToArray($file) And now, I am splitting this main array record wise so that Array contains internally another arrow to represent each row.
      For $i = 0 to (UBound($aArray) - 1) ;MsgBox(0,"",$aArray[$i]) $aArray[$i] = StringSplit(StringStripCR($aArray[$i]), Chr(9),2);Chr(9) for tab ;_ArrayDisplay($aArray[$i]) Next Afther that, _ArrayDIsplay is able to see the individual internal arrays.
      _ArrayDisplay($aArray[1]) But If I try to access the individual element of it as below.It is not showing any result.
      MsgBox(0,"",$aArray[1][1]) Any suggestion, below is the sample csv file.
      New Text Document.csv
    • Jibsbrown
      By Jibsbrown
      Need some help understanding why the ConsoleWrite works inside 2nd For loop but not out side. Between Audit Wiki, Help file , Forum searching (lots of code reading), and YouTube ( shout out to TutsTeach), I have not been able to find the reason why. 
      $sIniPath = "installLog.ini" ; - Get section name $iniSctionNames = IniReadSectionNames($sIniPath) ; - Get Keys and Vaules For $a = 1 to UBound($iniSctionNames) - 1 $keys = IniReadSection($sIniPath , $iniSctionNames[$a]) For $b = 1 to UBound($keys) - 1 $oldSysInfo = IniRead($sIniPath , $iniSctionNames[1], $keys[$b][0], "") $PntIPInfo = IniRead($sIniPath , $iniSctionNames[2], $keys[$b][0], "") $NewPCInfor = IniRead($sIniPath , $iniSctionNames[3], $keys[$b][0], "") ;ConsoleWrite($oldSysInfo & @LF) Next ;ConsoleWrite($oldSysInfo & @LF) Next ConsoleWrite($oldSysInfo) My intention is to use the variables later for Listboxes. Any explanation, forum post links or whatever would help. Sorry also very very new to Autoit.
      Also here's the ini file.
      [OldSysInfo] 4=192.168.0.4|DESKTOP-RDIU2SN|R90M05Q8 5=192.168.0.5|SD0123456789101|R9WGP9P 6=192.168.0.6|SD0123456789102|R9WGP9PT 3=192.168.0.3|DESKTOP-3RS4LKL|R9WGP9P 23=192.168.0.23|SD0123456789102|MXL1234P5I [PrinterIp] 50=192.168.0.50 48=192.168.0.48 47=192.168.0.47 [NewSysInfo] newPC = SD0123456789adfs|192.168.0.185|2UA1234FTR Thank you for your time.