Jump to content

Recommended Posts

Hello all,

 

I am trying to get a script together for downloading a file, installing it, then delete the file... So far I have been able to put peace by peace together and it is working. They only opportunity that I am running into is when a user puts in the wrong credentials to get to the setup the script will continue running down the script. I wanted to display a Message if the inetget command timesout or fails to just kill the script and display a message error or something..  FYI I am very new with Autoit. The Help File is great but there something that I am still learning. Any help will be greatly appreciated..

Here what I have so far

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <ProgressConstants.au3>
#include <Constants.au3>

#RequireAdmin

_SelfDelete()

While 1
   
    $OPIDCHK = InputBox("Security Checkpoint", "Enter OPID:","", "")
    If @error = "1" Then exit
    
    If StringLen($OPIDCHK) <> 6 Then
        MsgBox(48, "Error!", "OPID NOT VALID.")
        Continueloop 
        
        EndIf
    ExitLoop
    
WEnd
    
While 2
   
    $PASSCHK = InputBox("Security Checkpoint", "Enter PASSWORD:","", "*")
    If @error = "1" Then exit
   
    If StringLen($PASSCHK) < 7 Then
         MsgBox (48, "Error!", "PASSWORD NOT VALID.")
         ContinueLoop
         
         EndIf
    ExitLoop
      
WEnd    

Opt("TrayMenuMode", 1)

$Form1              = GUICreate("Downloader", 400, 170, -1, -1)
$pb_File            = GUICtrlCreateProgress(30, 30, 300, 20, $PBS_SMOOTH)
$lbl_FilePercent    = GUICtrlCreateLabel("0 %", 335, 32, 35, 16, $SS_RIGHT)
$but_Download       = GUICtrlCreateButton("Download", 160, 120, 80, 30)

GUISetState(@SW_Show)

While 1
    
    $nMsg = GUIGetMsg()
    
    If $nMsg = $GUI_EVENT_CLOSE Then EXIT
        
    If $nMsg = $but_Download Then _Download()
    
WEnd

Func _Download()
    
    ; Disable the download button
    GUICtrlSetState($but_Download, $GUI_DISABLE)
    
    ; Reset total filesize to download
    $TotalToDownload = 0
    
    Dim $DownloadArray[8][3]
    
    ; Setup the URL's to download
    $DownloadArray[1][0] = "https://" & $OPIDCHK & ":" & $PASSCHK & "@websitegoeshere/setup64.exe"
    $PATH = @TempDir & "\setup64.exe"
    

    For $i = 1 To UBound($DownloadArray)-1
        
        ; Get the File size
        $FileSize = INetGetSize($DownloadArray[$i][0])
        
        ; Current File Size
        $DownloadArray[$i][1] = $FileSize
        
        ; Cumulative Total
        $DownloadArray[$i][2] = $TotalToDownload
        
        ; Add the current file size to the total
        $TotalToDownload += $FileSize
    Next
    
    ; Do the Downloads
    For $i = 1 To UBound($DownloadArray)-1
        
        ; Dow the download in the background
        $Download = INetGet($DownloadArray[$i][0],$PATH,1,1)

        
        ; Loop to update progress
        Do
            ; Get number of bytes read for current file
            $BytesDownloaded    = INetGetInfo($Download, 0)
            
            ; Add this to the cumulative total
            $DownloadedSoFar    = $DownloadArray[$i][2] + $BytesDownloaded
            
            ; Calculate the current file percentage
            $FileProgress       = Floor(($BytesDownloaded / $DownloadArray[$i][1]) * 100)
            
            ; Calculate the overall percentage
            $OverallProgress    = Floor(($DownloadedSoFar / $TotalToDownload) * 100)
            
            ; Update the Current FIle progress bar
            GUICtrlSetData($pb_File, $FileProgress)
            
            ; Only update the current file percent label if it has changed to avoid flickering
            If GUICtrlRead($lbl_FilePercent) <> $FileProgress & " %" Then GUICtrlSetData($lbl_FilePercent, $FileProgress & " %")
            
            ; Only update the title bar (overall) percent label if it has changed to avoid flickering
            If WinGetTitle($Form1, "") <> $OverallProgress & " % - Downloader" Then WinSetTitle($Form1, "", $OverallProgress & " % - Downloader")
            
            ; Continue loop until download is complete
            Until InetGetInfo($Download, 2)            
         
           ; Set current file progress bar to 100% when complete
           GUICtrlSetData($pb_File, 100)
        
           ; Set current file percent label to 100% when complete
           GUICtrlSetData($lbl_FilePercent, "100 %")
           
           
   Next
         MsgBox (0, "Download", "Download completed. Click ok to continue installation")
         RunWait($PATH)
         FileDelete (@TempDir & "\setup64.exe")
         Exit    
    
 EndFunc

Func _SelfDelete($iDelay = 0)
    Local $sCmdFile
    FileDelete(@TempDir & "scratch.bat")
    $sCmdFile = 'ping -n ' & $iDelay & ' 127.0.0.1 > nul' & @CRLF _
             & ':loop' & @CRLF _
             & 'del "' & @ScriptFullPath & '"' & @CRLF _
             & 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
             & 'del ' & @TempDir & 'scratch.bat'
    FileWrite(@TempDir & "scratch.bat", $sCmdFile)
    Run(@TempDir & "scratch.bat", @TempDir, @SW_HIDE)
 EndFunc;==>_SelfDelete
Link to post
Share on other sites

; Select a timeout value (miliseconds)
$TimeOut = 300
; Set a timer to zero
$Timer = TimerInit()
; Set a variable to hold bytes downloaded
$CheckBytes = 0
Do
    ; Get number of bytes read for current file
    $BytesDownloaded = InetGetInfo($Download, 0)

    ; Add this to the cumulative total
    $DownloadedSoFar = $DownloadArray[$i][2] + $BytesDownloaded
    
    ; Test if there if more info than in $CheckBytes after a Timeout period
    If TimerDiff($Timer) >= $TimeOut Then
        If $BytesDownloaded <= $CheckBytes Then
            ; No data has been added, some problem
            MsgBox(0,"Error", "File #: " & $i & " failed to download", 3)
            ExitLoop
        EndIf
        ; Set $CheckBytes to current bytes
        $CheckBytes = $BytesDownloaded
        ;Reset timer
        $Timer = TimerInit()
    EndIf   

    ; Calculate the current file percentage
    $FileProgress = Floor(($BytesDownloaded / $DownloadArray[$i][1]) * 100)

    ; Calculate the overall percentage
    $OverallProgress = Floor(($DownloadedSoFar / $TotalToDownload) * 100)

    ; Update the Current FIle progress bar
    GUICtrlSetData($pb_File, $FileProgress)

    ; Only update the current file percent label if it has changed to avoid flickering
    If GUICtrlRead($lbl_FilePercent) <> $FileProgress & " %" Then GUICtrlSetData($lbl_FilePercent, $FileProgress & " %")

    ; Only update the title bar (overall) percent label if it has changed to avoid flickering
    If WinGetTitle($Form1, "") <> $OverallProgress & " % - Downloader" Then WinSetTitle($Form1, "", $OverallProgress & " % - Downloader")

    ; Continue loop until download is complete
Until InetGetInfo($Download, 2) 

This just off top of head I have not tested it, but you should be able to get the gist if there is bug.

 

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

Monkey's are, like, natures humans.

Link to post
Share on other sites

JohnOne,

Thank you for your help, I tried it but if I put the wrong credentials it will finish the script right away, so it looks like there no time for a timeout since so quick. I lower the timeout but that did not help. 

Here is the Script with your modifications:

Please advise  

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <ProgressConstants.au3>
#include <Constants.au3>

#RequireAdmin

_SelfDelete()

While 1
   
    $OPIDCHK = InputBox("Security Checkpoint", "Enter OPID:","", "")
    If @error = "1" Then exit
    
    If StringLen($OPIDCHK) <> 6 Then
        MsgBox(48, "Error!", "OPID NOT VALID.")
        Continueloop 
        
        EndIf
    ExitLoop
    
WEnd
    
While 2
   
    $PASSCHK = InputBox("Security Checkpoint", "Enter PASSWORD:","", "*")
    If @error = "1" Then exit
   
    If StringLen($PASSCHK) < 7 Then
         MsgBox (48, "Error!", "PASSWORD NOT VALID.")
         ContinueLoop
         
         EndIf
    ExitLoop
      
WEnd    

Opt("TrayMenuMode", 1)

$Form1              = GUICreate("Downloader", 400, 170, -1, -1)
$pb_File            = GUICtrlCreateProgress(30, 30, 300, 20, $PBS_SMOOTH)
$lbl_FilePercent    = GUICtrlCreateLabel("0 %", 335, 32, 35, 16, $SS_RIGHT)
$but_Download       = GUICtrlCreateButton("Download", 160, 120, 80, 30)

GUISetState(@SW_Show)

While 1
    
    $nMsg = GUIGetMsg()
    
    If $nMsg = $GUI_EVENT_CLOSE Then EXIT
        
    If $nMsg = $but_Download Then _Download()
    
WEnd

Func _Download()
    
    ; Disable the download button
    GUICtrlSetState($but_Download, $GUI_DISABLE)
    
    ; Reset total filesize to download
    $TotalToDownload = 0
    
    Dim $DownloadArray[8][3]
    
    ; Setup the URL's to download
    $DownloadArray[1][0] = "https://" & $OPIDCHK & ":" & $PASSCHK & "@websitegoeshere/setup64.exe"
    $PATH = @TempDir & "\setup64.exe"
    

    For $i = 1 To UBound($DownloadArray)-1
        
        ; Get the File size
        $FileSize = INetGetSize($DownloadArray[$i][0])
        
        ; Current File Size
        $DownloadArray[$i][1] = $FileSize
        
        ; Cumulative Total
        $DownloadArray[$i][2] = $TotalToDownload
        
        ; Add the current file size to the total
        $TotalToDownload += $FileSize
    Next
    
    ; Do the Downloads
    For $i = 1 To UBound($DownloadArray)-1
        
        ; Dow the download in the background
        $Download = INetGet($DownloadArray[$i][0],$PATH,1,1)
        
; Select a timeout value (miliseconds)
$TimeOut = 300
; Set a timer to zero
$Timer = TimerInit()
; Set a variable to hold bytes downloaded
$CheckBytes = 0      
Do
    ; Get number of bytes read for current file
    $BytesDownloaded = InetGetInfo($Download, 0)

    ; Add this to the cumulative total
    $DownloadedSoFar = $DownloadArray[$i][2] + $BytesDownloaded
    
    ; Test if there if more info than in $CheckBytes after a Timeout period
    If TimerDiff($Timer) >= $TimeOut Then
       If $BytesDownloaded <= $CheckBytes Then
            ; No data has been added, some problem
            MsgBox(0,"Error", "File #: " & $i & " failed to download", 3)
            ExitLoop
        EndIf
        ; Set $CheckBytes to current bytes
        $CheckBytes = $BytesDownloaded
        ;Reset timer
        $Timer = TimerInit()
    EndIf   

    ; Calculate the current file percentage
    $FileProgress = Floor(($BytesDownloaded / $DownloadArray[$i][1]) * 100)

    ; Calculate the overall percentage
    $OverallProgress = Floor(($DownloadedSoFar / $TotalToDownload) * 100)

    ; Update the Current FIle progress bar
    GUICtrlSetData($pb_File, $FileProgress)

    ; Only update the current file percent label if it has changed to avoid flickering
    If GUICtrlRead($lbl_FilePercent) <> $FileProgress & " %" Then GUICtrlSetData($lbl_FilePercent, $FileProgress & " %")

    ; Only update the title bar (overall) percent label if it has changed to avoid flickering
    If WinGetTitle($Form1, "") <> $OverallProgress & " % - Downloader" Then WinSetTitle($Form1, "", $OverallProgress & " % - Downloader")

    ; Continue loop until download is complete
Until InetGetInfo($Download, 2) 
   
    ;Set current file progress bar to 100% when complete
     GUICtrlSetData($pb_File, 100)
   
    ;Set current file percent label to 100% when complete
     GUICtrlSetData($lbl_FilePercent, "100 %")
      
      
Next
         MsgBox (0, "Download", "Download completed. Click ok to continue installation")
         RunWait($PATH)
         FileDelete (@TempDir & "\setup64.exe")
         Exit    
    
 EndFunc

Func _SelfDelete($iDelay = 0)
    Local $sCmdFile
    FileDelete(@TempDir & "scratch.bat")
    $sCmdFile = 'ping -n ' & $iDelay & ' 127.0.0.1 > nul' & @CRLF _
             & ':loop' & @CRLF _
             & 'del "' & @ScriptFullPath & '"' & @CRLF _
             & 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
             & 'del ' & @TempDir & 'scratch.bat'
    FileWrite(@TempDir & "scratch.bat", $sCmdFile)
    Run(@TempDir & "scratch.bat", @TempDir, @SW_HIDE)
 EndFunc;==>_SelfDelete

Lots of thanks in advance for you help.

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...