Windows 调用WebRequest-输出文件<;文件名>;文件名中带有通配符
如何在Invoke WebRequest的-OutFile参数中引用通配符Windows 调用WebRequest-输出文件<;文件名>;文件名中带有通配符,windows,powershell,Windows,Powershell,如何在Invoke WebRequest的-OutFile参数中引用通配符 $filename = "Image[1].jpg" $uri = [System.Uri]"http:/url/to/image.jpg" Invoke-WebRequest -Uri $uri -OutFile $filename 这段代码给了我一个例外: Cannot perform operation because the wildcard path Image[1].jpg did not resolve
$filename = "Image[1].jpg"
$uri = [System.Uri]"http:/url/to/image.jpg"
Invoke-WebRequest -Uri $uri -OutFile $filename
这段代码给了我一个例外:
Cannot perform operation because the wildcard path Image[1].jpg did not resolve to a file.
说
这里需要注意两件事。首先,我们用单引号将路径名括起来。这不是强制性的,但它让我们的命令看起来更容易。(稍后您将看到)。第二,我们需要在方括号前面加上两个反勾号(``);这告诉PowerShell,我们希望将左方括号视为常规字符,而不是通配符。正如我们所说,它看起来很疯狂,但它确实有效:
给我:
Cannot perform operation because the wildcard path Image``[1``].jpg did not resolve to a file.
将文件名更改为
$filename = "Image``[1``].jpg"
不会生成异常,但已保存文件的名称为
Image`[1`].jpg
我想更改文件名,但要求下载时保持相同的名称
你有什么想法可以让它工作吗
Thx不是优雅的解决方案:
$filename = 'c:\temp\Image(1).jpg'
$uri = [System.Uri]"http://www.google.fr"
Invoke-WebRequest -Uri $uri -OutFile $filename
Rename-Item $filename $filename.Replace('(', '[').Replace(')', ']')
我很困惑为什么你需要外文文件上的通配符?这是一个没有评论的报道问题。声称有解决办法,但没有提到任何@ArcSet我认为提问者不需要在输出文件上使用通配符…@ArcSet我不需要在输出文件上使用通配符,但Powershell会解释任何[,],*,?在路径中作为通配符。[和]是windows文件名的法定字符,我正在尝试下载一个包含[和]的文件,并保留其文件名。@TessellingHeckler感谢您提供的信息,我正试图摆脱方括号的束缚
$filename = 'c:\temp\Image(1).jpg'
$uri = [System.Uri]"http://www.google.fr"
Invoke-WebRequest -Uri $uri -OutFile $filename
Rename-Item $filename $filename.Replace('(', '[').Replace(')', ']')