我通常使用PowerShell脚本来下载批量CSV图像,但我有一个新的URL,非常奇怪地显示图像。我可以修改此脚本以允许这些图片网址吗?
示例网址:
https://www.example.com/core/media/media.nl?id=12&c=23&h=b944f2f81326d0bb
https://www.example.com/core/media/media.nl?id=15&c=42&h=7ed23c91f3574fc9
和当前的脚本......
[Reflection.Assembly]::LoadFile(
'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Web.dll') | Out-Null
$FileName = "C:\Temp\test.txt";
$Loc = "C:\Temp\Images\"
$ImageName = ""
$wc = New-Object System.Net.WebClient
$content = Get-Content $FileName
foreach ($line in $content) {
$Image = $Loc + $line.Substring($line.LastIndexOf("/") + 1)
$url = $line
Write-Host $url
Write-Host $Image
$wc.DownloadFile($url, $Image)
}
Write-Jost "Finished successfully."
答案 0 :(得分:1)
Windows上的文件名不能包含?
,*
,"
,\
字符,因此请将其过滤掉:
$Image = $Loc + ($line.Substring($line.LastIndexOf("/") + 1) -replace '[?*"\\]', '_')
要从动态网址获取真实的重定向文件名,请处理Content-Disposition
标题:
$tmp = [IO.Path]::GetTempFileName()
$wc.DownloadFile($url, $tmp)
$Image = "$($wc.ResponseHeaders['Content-Disposition'])" -replace '^.*?filename=', ''
if (!$Image) {
$Image = $line.Substring($line.LastIndexOf("/") + 1) -replace '[?*"\\]', '_'
}
Move $tmp (Join-Path $Loc $Image)