windows PowerShell - 从 URL 中不包含文件名的 URL 下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12789516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:57:59  来源:igfitidea点击:

PowerShell - Download file(s) from URL that does not include file name in the URL

windowspowershelldownload

提问by Charles Feemster

I've searched around and tried a few things and have not gotten it to work. The link would be for example: http://www.website com/file/[fileID](e.g. http://www.website com/file/1wA5fT) and a box would appear whether to save the file(s) or not.

我四处搜索并尝试了一些东西,但没有让它工作。链接将是例如:(http://www.website com/file/[fileID]例如http://www.website com/file/1wA5fT)并且会出现一个框是否保存文件。

I have tried this, from what I can remember and it did not work.

我已经尝试过这个,据我所知,它没有用。

$source = "http://www.website.com/file/[fileID]"
$dest = "C:\Users\Charles\Desktop\Downloads\"

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source, $dest)

Edit:

编辑:

I am able to correctly download the file if I put a filename for the destination. However I need to extract the filename from e.g.

如果我为目标输入文件名,我就可以正确下载文件。但是我需要从例如提取文件名

<a href="http://www.website.com/file/[fileID]">Filename.txt</a></li></div></ul>

<a href="http://www.website.com/file/[fileID]">Filename.txt</a></li></div></ul>

After I get this singled out how would I single out the filename into $Filename?

在我挑出这个之后,我将如何将文件名挑出到 $Filename 中?

$source = "http://www.website.com/file/[fileID]"
$dest = "C:\Users\Charles\Desktop\Downloads$Filename"

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source, $dest)

This code would work then.

这段代码然后就可以工作了。

回答by Bob Reynolds

$source = "http://www.website.com/file/someFile.txt"
$Filename = [System.IO.Path]::GetFileName($source)
$dest = "C:\Users\Charles\Desktop\Downloads$Filename"

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source, $dest)

回答by toftis

I got the same error as you described when I called:

我在打电话时遇到了与您描述的相同的错误:

$source = "http://www.website.com/file/[fileID]"
$dest = "C:\Users\Charles\Desktop\Downloads\"

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source, $dest)

But when I changed $dest to contain the full path(including the name of the file it worked)

但是当我将 $dest 更改为包含完整路径时(包括它工作的文件名)

$dest = "C:\Users\Charles\Desktop\Downloads\[aFileName]"