windows - 在文件名中使用通配符的 Invoke-WebRequest -OutFile <filename>

标签 windows powershell

如何在 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/

相关文章:

Powershell 主目录未在文件服务器文件系统上创建

powershell - 如何在Powershell中删除空白列并按所需顺序对列进行重新排序

powershell - 使用 powershell 发送 gmail

powershell - Azure-SSMS-PowerShell

windows - 使用 CMD 从 Windows 凭据管理器检索密码

windows - 通过链接文件运行可执行文件时保留cmd工作目录

windows - 在 Windows 上以管理员身份运行时无法识别“choco”命令

windows - 为什么在 git bash 中创建的文件在 Windows 资源管理器中不可见?

c++ - 如何在 C++ 中获取 JoyStick Z 旋转消息

powershell - 从 PowerShell 脚本将工作流事件添加到工具箱时遇到问题