简体   繁体   English

使用 Powershell 从 Internet 下载文件并取得进展

[英]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.我一直在研究 powershell 脚本,该脚本使用 a.txt 文件从 tinyurls 下载多个文件。 I have been successful in using Jobs to make this happen simultaneously, thanks to those on this forum.感谢这个论坛上的人,我已经成功地使用Jobs来同时实现这一点。 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.这是我想出的,但我不知道如何将 pipe 信息返回到控制台。 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 看看Write-Progress

PS C:> for ($i = 1; $i -le 100; $i++ ) {write-progress -activity "Search in Progress" -status "$i% Complete:" -percentcomplete $i;} PS C:> for($ i = 1; $ i -le 100; $ i ++){写进度-活动“正在搜索”-状态“ $ i%完成:” -percentcomplete $ i;}

Far more simple way: rely on Bits:更简单的方法:依靠 Bits:

Start-BitsTransfer -Source $tinyUrl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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