如何在 Invoke-WebRequest 的 -OutFile 参数中引用通配符?
$filename = "Image[1].jpg"
$uri = [System.Uri]"http:/url/to/image.jpg"
Invoke-WebRequest -Uri $uri -OutFile $filename
这段代码给了我一个异常(exception):
Cannot perform operation because the wildcard path Image[1].jpg did not resolve to a file.
Technet 说
Two things to note here. First, we enclosed the path name in single quote marks. That’s not mandatory, but it makes our command a bit easier on the eyes. (As you’ll see in a minute). Second, we needed to preface the square bracket with two back tick characters (``); this tells PowerShell that we want to treat the left square bracket as a regular character and not a wildcard character. Like we said, it looks crazy, but it works:
$filename = 'Image``[1``].jpg'
给我:
Cannot perform operation because the wildcard path Image``[1``].jpg did not resolve to a file.
将文件名更改为
$filename = "Image``[1``].jpg"
不会产生异常,但是保存文件的名字是
Image`[1`].jpg
我很想更改文件名,但要求它们在下载时保持相同的名称。
关于如何使这项工作有任何想法吗?
谢谢
最佳答案
不是优雅的解决方案:
$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(')', ']')
关于windows - 在文件名中使用通配符的 Invoke-WebRequest -OutFile <filename>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46590534/