简体   繁体   中英

Downloading files from the Internet using Powershell with progress

I have been working on a powershell script that uses a.txt file to download multiple files from tinyurls. I have been successful in using Jobs to make this happen simultaneously, thanks to those on this forum. The project requires some pretty large files to be downloaded, and using the current method has no progress indicator. I figured some users might think the program died. Looking for a way give a status of where it is in the download. Here is what I came up with, but I'm lost in how to pipe this information back out to the console. Any suggestions?

#Checks to see if NT-Download folder is on the Desktop, if not found, creates it
$DOCDIR = [Environment]::GetFolderPath("Desktop")
$TARGETDIR = "$DOCDIR\NT-Download"
if(!(Test-Path -Path $TARGETDIR )){
    New-Item -ItemType directory -Path $TARGETDIR
}

$filepaths = Resolve-Path "files.txt"

Get-Content "$filepaths" | Foreach {
Start-Job {
    function Save-TinyUrlFile
    {
        PARAM (
            $TinyUrl,
            $DestinationFolder
        )

        $response = Invoke-WebRequest -Uri $TinyUrl
        $filename = [System.IO.Path]::GetFileName($response.BaseResponse.ResponseUri.OriginalString)
        $filepath = [System.IO.Path]::Combine($DestinationFolder, $filename)
        $totalLength = [System.Math]::Floor($response.get_ContentLength()/1024) 
        $responseStream = $response.GetResponseStream()
        $buffer = new-object byte[] 10KB
        $count = $responseStream.Read($buffer,0,$buffer.length) 
        $downloadedBytes = $count
        try
        {
            $filestream = [System.IO.File]::Create($filepath)
            $response.RawContentStream.WriteTo($filestream)
            $filestream.Close()
            while ($count -gt 0) 
            { 
                [System.Console]::CursorLeft = 0 
                [System.Console]::Write("Downloaded {0}K of {1}K", [System.Math]::Floor($downloadedBytes/1024), $totalLength) 
                $targetStream.Write($buffer, 0, $count) 
                $count = $responseStream.Read($buffer,0,$buffer.length) 
                $downloadedBytes = $downloadedBytes + $count 
            } 
                         "`nFinished Download" 
            $targetStream.Flush()
            $targetStream.Close() 
            $targetStream.Dispose() 
            $responseStream.Dispose() 
        }
        finally
        {
            if ($filestream)
            {
                $filestream.Dispose();
            }
        }
    }

    Save-TinyUrlFile -TinyUrl $args[0] -DestinationFolder $args[1]
} -ArgumentList $_, "$TARGETDIR"

}

Have a look at Write-Progress

PS C:> for ($i = 1; $i -le 100; $i++ ) {write-progress -activity "Search in Progress" -status "$i% Complete:" -percentcomplete $i;}

Far more simple way: rely on Bits:

Start-BitsTransfer -Source $tinyUrl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM