【问题标题】:How to name downloaded file?如何命名下载的文件?
【发布时间】:2015-06-08 12:19:16
【问题描述】:

我的老板让我创建一个函数/脚本来从网站下载文件并将它们放在一个文件夹中:

Add-Type -AssemblyName Microsoft.Visualbasic

$url = "http://www.rarlab.com/rar/wrar521.exe"
$output = "C:\Users\****\Downloads\test\1"

$object = New-Object Net.WebClient
$object.DownloadFile($url, $output)


#######################################
$start_time = Get-Date
Write-Output "Time Taken((Get-Date).Subtract($start_time).Seconds) second(s)"

这是我得到的当前代码。基本上,我需要它做的是自动命名文件或从服务器解析名称,而不必输入$output = "C:\Users\****\Downloads\test\test.txt" 或其他东西。

【问题讨论】:

  • 如果有人能在这件事上帮助我,我将不胜感激^.^

标签: download powershell-3.0


【解决方案1】:

使用Split-Path 将文件名与URL 分开,使用Join-Path 将其加入文件夹路径:

$url      = 'http://www.rarlab.com/rar/wrar521.exe'
$filename = Split-Path -Leaf $url
$output   = Join-Path 'C:\Users\****\Downloads\test\1' $filename

(New-Object Net.WebClient).DownloadFile($url, $output)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    相关资源
    最近更新 更多