Jump to content

Simple Yet Effective


bran8
 Share

Recommended Posts

I am currently working on a BITSAdmin Wrapper for AutoIt 3 (beta).

This is one of my very first AutoIt scripts.

Feel free to check it out and provide some feedback if you can help make the code more efficient.

###

BITSAdmin Tool by Microsoft

Download BITS from Microsoft

BITSAdmin is a command-line tool that you can use to create download or upload jobs and monitor their progress.

#include-once
#comments-start
 ----------------------------------------------------------------------------
 Tested with AutoIt 3.1.1.120 BETA
                             ^^^^

 BITSAdmin Wrapper Version 1.0

 Known Issues:
 ^^^^^^^^^^^^
 * Job Names with spaces does not work on these functions:
  : InvokeBITS_List, InvokeBITS_Complete, InvokeBITS_CompleteALL will not be unable to distinguish job names and job status
  : If you chose to use spaces, excplicitly specify the job to act on. 

 Author:         Brandon Lai (BRANNYBOY8@ROGERS.COM, LAIBR@WYETH.COM)

 Author Notes:
    The purpose is to easily integrate BITSAdmin into your AU3 scripts.

 Functions
 ^^^^^^^^^
 MsgArray($ArrayOfData): Display array of data in multiple MsgBoxes, $ArrayOfData[0] = # of lines in arrray

 InvokeBITS($rawcommand): Invoke BITSAdmin using your own command, use "quotes" where appropriate
 InvokeBITS_Create($JobName,[$JobDescription]): Cancel and Create new job, returns Job GUID
 InvokeBITS_List(): Returns array [n][x], where x = <GUID> <JobName> <Status> <FilesDone> <FilesLeft> <BytesDone> <BytesLeft>
 InvokeBITS_AddFile($JobName,$RemoteFile,$LocalFile): Returns TRUE if "added", Transfers a file over HTTP to local PC
 InvokeBITS_Complete($JobName): Returns TRUE if job has TRANSFERRED, Writes job files to disk
 InvokeBITS_CompleteALL(): Returns number of JOBs completed
 InvokeBITS_GetState($JobName): Returns state of job
 InvokeBITS_SetPriority($JobName,(LOW,NORMAL,HIGH)): Sets priority of job
 
 Requirements:
  - Tested with BITSAdmin.exe 1.0 [ 5.1.2600.0 ]
 
 Notes:
  - 

 Revisions:
 23 APR 2006 - Version 1.0
 
  ----------------------------------------------------------------------------
#comments-end

; Default Global Settings (optional)
Dim $LinesPerPage = 24; for MsgArray, how many lines to display per MsgBox?
Dim $BITSBuffer = 256; STDOUT for BITSAdmin.exe buffer allocated before subscript out of range
Dim $MaxJobs = 64; when declaring array of jobs, max jobs to manage before subscript out of range

Func InvokeBITS(Const $BITSCmd)
    If not IsDeclared("BITSBuffer") then Dim $BITSBuffer = 256
    If not FileExists("BITSADMIN.EXE") then 
        MsgBox(0,"","BITSADMIN.EXE not found, please add to path.")
        Exit
    EndIf
    
    $foo = Run(@Comspec & ' /C BITSADMIN.EXE /RAWRETURN /WRAP ' & $BITSCmd,"", @SW_HIDE, 3)
    Dim $BITSOutput[$BITSBuffer]
    Dim $n=1
    While 1
            $BITSOutput[$n] = StdoutRead($foo)
            If @error = -1 Then ExitLoop
            $n = $n + 1
    Wend
        $BITSOutput[0] = ($n - 1)
        Return $BITSOutput
EndFunc

func MsgArray(byref $BITSResult)
If Not IsDeclared("LinesPerPage") Then Dim $LinesPerPage = 24
Dim $RESULTText 
    For $0 = 1 to $BITSResult[0]
        $RESULTText = $RESULTText & $BITSResult[$0]
    if mod($0,$LinesPerPage) = 0 then 
        msgbox(0,"",$RESULTText)
        $RESULTText = ""
        EndIf
    Next
msgbox(0,"",$RESULTText)
EndFunc

; ***************************
; ** BITS Helper Functions **
; ***************************

Func InvokeBITS_Create(Const $JobName,$JobDispName = "")
; Cancel and Create $JobName, returns GUID of new job
    InvokeBITS("/CANCEL " & $JobName)
    $CreatedJob = InvokeBITS("/CREATE " & $JobName)
    If Not $JobDispName = "" Then
    $CreatedJob = InvokeBITS('/SETDESCRIPTION '&$JobName&' "'&$JobDispName&'"')
    EndIf
    Return $CreatedJob[1]
EndFunc

Func InvokeBITS_List()
; Returns array of jobs where element [0][0] = number of jobs
; [n][x], where x = <GUID> <JobName> <Status> <FilesDone> <FilesLeft> <BytesDone> <BytesLeft>

If Not IsDeclared("MaxJobs") then Dim $MaxJobs = 100
    Dim $BITSArray[$BITSBuffer]
    Dim $BITSJobArray[$MaxJobs][8]
    $BITSArray = InvokeBITS("/LIST")
    
    $BITSJobArray[0][0] = $BITSArray[0]; Store # of jobs

    For $0 = 1 to $BITSArray[0]
        $BITSJob = StringSplit($BITSArray[$0]," ")
        $BITSJobArray[$0][1] = $BITSJob[1]; GUID
        $BITSJobArray[$0][2] = $BITSJob[2]; JobName
        $BITSJobArray[$0][3] = $BITSJob[3]; Status
        $BITSJobArray[$0][4] = $BITSJob[4]; File(s) Done
        $BITSJobArray[$0][5] = $BITSJob[6]; File(s) Total
        $BITSJobArray[$0][6] = $BITSJob[7]; Byte(s) Done
        $BITSJobArray[$0][7] = $BITSJob[9]; Byte(s) Total
    Next

    Return $BITSJobArray
EndFunc

func InvokeBITS_AddFile(Const $JobName,Const $RemoteFile,Const $LocalFile)
; Addfile and Resume job
    Dim $BITSArray[$BITSBuffer]
    $BITSArray = InvokeBITS('/ADDFILE "' & $JobName & '" "' & $RemoteFile &'" "' & $LocalFile & '"')
    InvokeBITS('/RESUME "' & $JobName & '"')    
    If StringLeft($BITSArray[1],5) = "Added" then 
        return true
    Else
        return false
    EndIf
EndFunc

func InvokeBITS_GetState(Const $JobName)
; Return state of JOB ie. SUSPENDED, TRANSFERRED, TRANSFERRING
    Dim $BITSArray[$BITSBuffer]
    $BITSArray = InvokeBITS('/GETSTATE "' & $JobName & '"')
    Return $BITSArray[1]
EndFunc

func InvokeBITS_SetPriority(Const $JobName,Const $Priority)
; Set priority to HIGH, NORMAL, LOW
    Dim $BITSArray[$BITSBuffer]
    $BITSArray = InvokeBITS('/SETPRIORITY "' & $JobName & '" "' & $Priority & '"')
    Return $BITSArray[1]
EndFunc

func InvokeBITS_Complete(Const $JobName)
; Try to complete job, return true if successful, Warning: case sensitive!
    Dim $BITSJobArray[$MaxJobs][8]
    $BITSJobArray = InvokeBITS_List()
    For $0 = 1 to $BITSJobArray[0][0]
        If $BITSJobArray[$0][2] = $JobName then
            if $BITSJobArray[$0][3] = "TRANSFERRED" Then
                Dim $BITSResult[$BITSBuffer]
                $BITSResult = InvokeBITS("/COMPLETE " & $JobName)
                Return true
            Else
                Return false
            EndIf
        EndIf
    Next
    return false
EndFunc

func InvokeBITS_CompleteALL()
; Try to complete ALL jobs in queue, returns number of jobs completed
    Dim $BITSJobArray[$MaxJobs][8]
    dim $NumCompleted = 0
    $BITSJobArray = InvokeBITS_List()
    For $0 = 1 to $BITSJobArray[0][0]
                MsgBox(0,"Debug",'/COMPLETE "' & $BITSJobArray[$0][1] & '"')
                MsgBox(0,"Debug",'Array 3 "' & $BITSJobArray[$0][3] & '"')
                if $BITSJobArray[$0][3] = "TRANSFERRED" Then
                Dim $BITSResult[$BITSBuffer]
                $BITSResult = InvokeBITS('/COMPLETE "' & $BITSJobArray[$0][1] & '"')
                $NumCompleted = $NumCompleted + 1
            EndIf
    Next
    return $NumCompleted
EndFunc

func InvokeBITS_ResumeALL()
; Try to resume ALL jobs in queue, returns number of jobs resumed
    Dim $BITSJobArray[$MaxJobs][8]
    dim $NumCompleted = 0
    $BITSJobArray = InvokeBITS_List()
    For $0 = 1 to $BITSJobArray[0][0]
                Dim $BITSResult[$BITSBuffer]
                $BITSResult = InvokeBITS("/RESUME " & $BITSJobArray[$0][1])
                If $BITSResult[1] = "Job resumed." then $NumCompleted = $NumCompleted + 1               
    Next
    return $NumCompleted
EndFunc

I have attached a ZIP file containing the above code and 2 examples.

Brandon.

PS - How do I change the topic of this post? I created in error instead of replying to someone elses post.

BITSAdmin.zip

Edited by bran8
Link to comment
Share on other sites

  • 4 years later...

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