Sign in to follow this  
Followers 0
ClosetDweller

Help with a loop

18 posts in this topic

#1 ·  Posted (edited)

Ive been banging my head against a wall here. Im new to looping but I gotta believe that I am missing something obvious here.

At the moment there are 13 files in a remote directory that I write to an array. That number could change.

So rather than re write every time it does, I would like a looping condition that simply processes the array until empty.

Any help would be appreciated.

;array name is $afile
For $i = 0 To UBound($aFile) - 1
If $aFile[$i] = 1 Then
Do
Local $hDownload = InetGet($path & $aFile[$i], $localpath & "" & $aFile[$i], 1, 1)
InetGetInfo($hDownload, 2)
InetClose($hDownload)
Until

EndIf
Next
Edited by ClosetDweller

Share this post


Link to post
Share on other sites



Your script does the download in the background. Can get a bit complext if you need to check if all downloads have finished successfully.

Can they be done in the foreground so the script waits for them to finish?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Water:

Thanks for the quick reponse. I have gotten the background portion worked out actually. problem is its only when I specify the element $afile[2] etc it compares file sizes in the DO part ( I stripped out that part to focus on the looping condition) what I cant seem to get ahead of is the processing of the array.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Here is the (untested) code for forground and background downloading:

Local $path, $localpath, $aFile, $hDownload, $aStatus, $bAllFinished = True
;---------------------------
; Download in the foreground
;---------------------------
For $i = 0 To UBound($aFile) - 1
    $hDownload = InetGet($path & $aFile[$i], $localpath & "" & $aFile[$i], 1, 0) ; download in the foreground
    If @error Then Exit MsgBox(16, "Error", "Error " & @error & " occurred when downloading file " & $path & $aFile[$i])
Next

;---------------------------
; Download in the background
;---------------------------
Local $hDownload[UBound($aFile)]
; Start the download
For $i = 0 To UBound($aFile) - 1
    $hDownload[$i] = InetGet($path & $aFile[$i], $localpath & "" & $aFile[$i], 1, 1) ; download in the background
    If @error Then Exit MsgBox(16, "Error", "Error " & @error & " occurred when downloading file " & $path & $aFile[$i])
Next
; Check the status of the downloads
While 1
    $bAllFinished = True
    For $i = 0 To UBound($aFile) - 1
        $aStatus = InetGetInfo($hDownload[$i], -1)
        If @error Then Exit MsgBox(16, "Error", "Error " & @error & " occurred when checking the download status for file " & $path & $aFile[$i])
        If $aStatus[2] = False Then ; Download still runing?
            $bAllFinished = False
        ElseIf $aStatus[3] = False Then ; download finished without error?
            Exit MsgBox(16, "Error", "Error " & $aStatus[4] & " occurred when downloading file " & $path & $aFile[$i])
        EndIf
    Next
    If $bAllFinished Then ExitLoop
WEnd
; Close the connections
For $i = 0 To UBound($aFile) - 1
    InetClose($hDownload[$i])
Next
MsgBox(64, "Info", "Alldownloads have been successfully finished!")

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Wow...I was way off.

I will test that out straight away and let you know shortly ...thanks

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

99% of the way there now. Youve been very helpful.

One last thing.

This writes a log file that captures the size of the download ($nBytes) but my DO statement appears to hang

Taking the DO out completes as hoped by fails to capture the TOTAL SIZE of the file.

Pointers as to why this halts? or Stops showing the size?

While 1
$bAllFinished = True
For $i = 1 To UBound($aFile) - 1
If FileExists($localpath & $aFile[$i]) Then
FileWriteLine($localpath & $storefile, $aFile[$i] & " Already Existed")
FileWriteLine($localpath & $storefile, "********************")
Else
FileWriteLine($localpath & $storefile, $aFile[$i] & " DID NOT Exist and Download began@" & @HOUR & ":" & @MIN)
Local $hDownload = InetGet($path & $aFile[$i], $localpath & "" & $aFile[$i], 1, 0)
Do
Until InetGetInfo($hDownload, 2)
Local $nBytes = InetGetInfo($hDownload, 1)
FileWriteLine($localpath & $storefile, $aFile[$i] & "Completed Downloading **" & $nBytes & " KB** @" & @HOUR & ":" & @MIN)
FileWriteLine($localpath & $storefile, "********************")
InetClose($hDownload)
EndIf
Next
If $bAllFinished Then ExitLoop
WEnd
Edited by ClosetDweller

Share this post


Link to post
Share on other sites

This should write the downloaded bytes when all downloads have finished (again untested):

Local $path, $localpath, $aFile, $hDownload, $aStatus, $bAllFinished = True, $iBytes = 0
;---------------------------
; Download in the foreground
;---------------------------
For $i = 0 To UBound($aFile) - 1
    $hDownload = InetGet($path & $aFile[$i], $localpath & "" & $aFile[$i], 1, 0) ; download in the foreground
    If @error Then Exit MsgBox(16, "Error", "Error " & @error & " occurred when downloading file " & $path & $aFile[$i])
Next

;---------------------------
; Download in the background
;---------------------------
Local $hDownload[UBound($aFile)]
; Start the download
For $i = 0 To UBound($aFile) - 1
    $hDownload[$i] = InetGet($path & $aFile[$i], $localpath & "" & $aFile[$i], 1, 1) ; download in the background
    If @error Then Exit MsgBox(16, "Error", "Error " & @error & " occurred when downloading file " & $path & $aFile[$i])
Next
; Check the status of the downloads
While 1
    $bAllFinished = True
    $iBytes = 0
    For $i = 0 To UBound($aFile) - 1
        $aStatus = InetGetInfo($hDownload[$i], -1)
        If @error Then Exit MsgBox(16, "Error", "Error " & @error & " occurred when checking the download status for file " & $path & $aFile[$i])
        If $aStatus[2] = False Then ; Download still running?
            $bAllFinished = False
        ElseIf $aStatus[3] = False Then ; download finished without error?
            Exit MsgBox(16, "Error", "Error " & $aStatus[4] & " occurred when downloading file " & $path & $aFile[$i])
     Else
         $iBytes = $iBytes + $aStatus[1]
        EndIf
    Next
    If $bAllFinished Then ExitLoop
WEnd
; Close the connections
For $i = 0 To UBound($aFile) - 1
    InetClose($hDownload[$i])
Next
MsgBox(64, "Info", "All downloads have been successfully finished!" & @CRLF & $iBytes & " downloaded.")


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Your file works like a champ. Many thanks. :thumbsup:

However :ermm:

Your file doesnt do any comparison. And I am having a hard time incorporating my checks

If FileExists($localpath & $aFile[$i]) Then
FileWriteLine($localpath & $storefile, $aFile[$i] & " Already Existed")
FileWriteLine($localpath & $storefile, "********************")
Else
   FileWriteLine($localpath & $storefile, $aFile[$i] & " DID NOT Exist and Download began@" & @HOUR & ":" & @MIN)
   Local $hDownload = InetGet($path & $aFile[$i], $localpath & "" & $aFile[$i], 1, 0)

If the file exists locally, it skips it in the array and merely notes as much in the log file.

Im sure I can nest another if statement in there but I appear to have reached my saturation point.

If you can find it in your heart to help me on this last leg, I would be eternally grateful, If youve had enough hand holding a noob I thank you for all you have contributed. Youve been a great help.

Edited by ClosetDweller

Share this post


Link to post
Share on other sites

At the moment you mix the two modes of operation: foreground and background.

How the code should look like depends on the number and size of the files you want to download.

Can you please tell me the size of all files summed up?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

For the moment****

Total of--526730443 KB --downloaded. is the final tally

But that is subject to change obviously with revisions or additions/subtractions based on the array

***thoughts...

Perhaps an array of the current directory, and an array of the remote directory, then a string to compare one element againts the contents of the other, with the results written to a 3rd array that is used to be the array to be downloaded?

Seems very .....Thick, but maybe doable?

Edited by ClosetDweller

Share this post


Link to post
Share on other sites

Before we start to code we need to know what we want: Download all files from a remote directory which aren't in a local directory. Right?

Some questions then come to my mind:

* What if the file in the remote directory changes (size, date)? Download the file again?

* Does the remote site offer FTP service? We could then get the remote directory, file size and date.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Yes thats exactly what I am trying to do.

I have all that info..

1. it is an FTP site (mine) that I am comparing.

local $rdirset = _FTP_DirSetCurrent($Conn, $rfile)

local $afile = _FTP_ListToArray($COnn, 2)

I am comparing ONLY the NAME, if the file changes, so does the name, so size and date arent an issue.

Edited by ClosetDweller

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

OK. This version only downloads files that can't be found in the local directory.

To test you can manually fill $aFile and see if the (untested) code works:

Global $path, $localpath, $aFile[13], $hDownload, $aStatus, $bAllFinished = True, $iBytes = 0
Global $hDownload[UBound($aFile)]

; Start the download if the file doesn't exist in the local directory
For $i = 0 To UBound($aFile) - 1
    If Not FileExists($localpath & "" & $aFile[$i]) Then
        $hDownload[$i] = InetGet($path & $aFile[$i], $localpath & "" & $aFile[$i], 1, 1) ; download in the background
        If @error Then Exit MsgBox(16, "Error", "Error " & @error & " occurred when downloading file " & $path & $aFile[$i])
    EndIf
Next
; Check the status of the downloads
While 1
    $bAllFinished = True
    $iBytes = 0
    For $i = 0 To UBound($aFile) - 1
        If $hDownload[$i] <> "" Then
            $aStatus = InetGetInfo($hDownload[$i], -1)
            If @error Then Exit MsgBox(16, "Error", "Error " & @error & " occurred when checking the download status for file " & $path & $aFile[$i])
            If $aStatus[2] = False Then ; Download still running?
                $bAllFinished = False
            ElseIf $aStatus[3] = False Then ; download finished without error?
                Exit MsgBox(16, "Error", "Error " & $aStatus[4] & " occurred when downloading file " & $path & $aFile[$i])
            Else
                $iBytes = $iBytes + $aStatus[1]
            EndIf
        EndIf
    Next
    If $bAllFinished Then ExitLoop
    Sleep(1000)
WEnd
; Close all opened connections
For $i = 0 To UBound($aFile) - 1
    If $hDownload[$i] <> "" Then InetClose($hDownload[$i])
Next
MsgBox(64, "Info", "All downloads have been finished successfully!" & @CRLF & $iBytes & " downloaded.")
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I will test it out and see what I come up with....your a god send

Share this post


Link to post
Share on other sites

no Errors...but recognizes all files there I assume? Doesnt actually DL anything.

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

I used the following test data and it works just fine (downloads 16189 Bytes):

Global $path, $localpath, $aFile[13], $hDownload, $aStatus, $bAllFinished = True, $iBytes = 0
Global $hDownload[UBound($aFile)]

; Test data starts here
$path = "http://ssl.gstatic.com/gb/images/"
$localpath = "C:temp"
$aFile[0] = "b_8d5afc09.png"
$aFile[1] = "b8_3615d64d.png"
$aFile[2] = "silhouette_96.png"
; Test data ends here

; Start the download if the file doesn't exist in the local directory
For $i = 0 To UBound($aFile) - 1
    If Not FileExists($localpath & "" & $aFile[$i]) Then
        $hDownload[$i] = InetGet($path & $aFile[$i], $localpath & "" & $aFile[$i], 1, 1) ; download in the background
        If @error Then Exit MsgBox(16, "Error", "Error " & @error & " occurred when downloading file " & $path & $aFile[$i])
    EndIf
Next
; Check the status of the downloads
While 1
    $bAllFinished = True
    $iBytes = 0
    For $i = 0 To UBound($aFile) - 1
        If $hDownload[$i] <> "" Then
            $aStatus = InetGetInfo($hDownload[$i], -1)
            If @error Then Exit MsgBox(16, "Error", "Error " & @error & " occurred when checking the download status for file " & $path & $aFile[$i])
            If $aStatus[2] = False Then ; Download still running?
                $bAllFinished = False
            ElseIf $aStatus[3] = False Then ; download finished without error?
                Exit MsgBox(16, "Error", "Error " & $aStatus[4] & " occurred when downloading file " & $path & $aFile[$i])
            Else
                $iBytes = $iBytes + $aStatus[1]
            EndIf
        EndIf
    Next
    If $bAllFinished Then ExitLoop
    Sleep(1000)
WEnd
; Close all opened connections
For $i = 0 To UBound($aFile) - 1
    If $hDownload[$i] <> "" Then InetClose($hDownload[$i])
Next
MsgBox(64, "Info", "All downloads have been finished successfully!" & @CRLF & $iBytes & " Bytes downloaded.")

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

WIth a few customizations to fit my purpose it works beautifully.

Big Ups to you sir.

As much as it falls short of what I hope to convey, Thank you

Share this post


Link to post
Share on other sites

Is my understanding correct that your problem is solved now?

Or can I help you in another respect?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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