【发布时间】:2019-12-16 01:24:05
【问题描述】:
我很难编写一个简单的批处理文件作为 powershell 脚本。
考虑这个文件夹结构。注意里面有很酷的[1] 的目录...
exiftool.exe
是一个命令实用程序,用于(例如)从嵌入的 MP3 标签中提取图片。
如果您需要更多信息,我uploaded its help。
oldscript.cmdexiftool -picture -b input.mp3 > output.jpg
这一行是在 powershell 中编写的行。我在作者的forum post 中找到了语法
-
-picture代表要提取的标签,-b代表二进制模式 -
input.mp3是我的测试 mp3,它的路径中可以包含特殊字符,例如 [ 和 ]i> -
> output.jpg定义名称并将生成的图像保存在同一文件夹中
newscript.ps1
我目前最好的非工作代码是:
$ownpath = Split-Path $MyInvocation.MyCommand.Path
$exe = $ownpath + '\exiftool.exe'
$input = $ownpath + '\input.mp3'
$outimg = $ownpath + '\output.jpg'
& $exe -picture -binary $input| Set-Content -literalPath $outimg -encoding UTF8
我发现Set-Content 能够通过“-literalpath”处理路径中的特殊字符。但我仍然无法将批处理转换为 Powershell 脚本,因为
与旧的批处理管道(“>”)相比,Set-Content(以及 Out-File 方法)似乎工作不同。无论我使用哪种编码,都无法查看生成的图像。 help file from above 表示 exiftool 使用的是 UTF8 编码。
当然,我尝试了其他available encodings,但它们都未能生成可视图像。我被困在这一点上。所以我最初的问题仍然部分存在“如何将此批处理文件转换为 powershell”。
那么为什么使用旧的批处理命令会起作用?
例如:创建一个文件夹“D:folder”并将此MP3 file with a cover image 放入其中。
从上面下载exiftool.exe并将其也放在那里。
旧的批处理命令将起作用并为您提供可见的图像
D:\folder\exiftool -picture -binary D:\folder\input.mp3 > D:\folder\output.jpg
具有相同语法的新 Powershell V2 脚本将失败。为什么?
& D:\folder\exiftool.exe -picture -binary D:\folder\input.mp3 > D:\folder\output.jpg
【问题讨论】:
标签: batch-file powershell-2.0 command-line-arguments