使用路径中的特殊字符将批处理文件转换为Powershell [英] Converting batch file to powershell with special characters in path
问题描述
我很难编写一个简单的批处理文件作为powershell脚本.
考虑此文件夹结构.请注意其中带有凉爽的[1]
的目录...
> exiftool.exe
是一个命令实用程序,例如(例如)从嵌入式MP3标签提取图片.
如果您需要更多信息,我上载了它的帮助.
oldscript.cmd
exiftool -picture -b input.mp3 > output.jpg
这行是在powershell中编写的.我在作者的论坛帖子中找到了该语法,/p>
-
-picture
代表要提取的标签,-b
代表二进制模式 -
input.mp3
是我的测试mp3,它的路径中可以包含特殊字符,例如[和] -
> 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方法)的工作方式似乎有所不同.无论使用哪种编码,生成的图像都不可见.上面的帮助文件表示exiftool使用的是UTF8编码.
我当然尝试了其他可用的编码,但是它们都无法生成可见的图像.我被困在这一点上.因此,我的最初问题仍然部分是如何将此批处理文件转换为Powershell".
那为什么在使用旧的批处理命令时它可以正常工作?
例如:创建一个文件夹"D:folder"并将其带有封面图像的MP3文件.
从上方下载 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
您可以尝试执行此操作,尽管我尚未对其进行测试,因为我没有带有嵌入式图像的mp3:
$file = & "D:\folder\exiftool.exe" -picture -binary "D:\folder\input.mp3"
[io.file]::WriteAllBytes('D:\folder\input[1].jpg',$file)
在Powershell控制台中使用此行可返回可读图像:
cmd.exe /c "D:\folder\exiftool.exe -picture -binary `"D:\folder\input.mp3`" > image.jpg"
您可以在路径和文件名中使用特殊字符,如下所示:
$exe = "c:\ps\exiftool.exe"
$mp3 = "c:\ps\a[1]\input.mp3"
$jpg = " c:\ps\a[1]\image[1].jpg"
cmd.exe /c "$exe -picture -binary $mp3 > $jpg"
路径内有空格:
$exe = "c:\ps\exiftool.exe"
$mp3 = "`"c:\ps\a [1]\input.mp3`""
$jpg = "`"c:\ps\a [1]\image [1].jpg`""
cmd.exe /c "$exe -picture -binary $mp3 > $jpg"
I'm having a hard time to write a simple batch file as powershell script.
Consider this folder structure. Note the directory with the cool [1]
in it...
exiftool.exe
Is a command utility to (for example) extract pictures from embedded MP3 tags.
I uploaded its help if you need more info.
oldscript.cmd
exiftool -picture -b input.mp3 > output.jpg
This line is the one to write in powershell. I found the syntax in a forum post from the author
-picture
stands for the tag to extract and-b
stands for binary modeinput.mp3
is my test mp3 which can contain special characters in its path like [ and ]> output.jpg
defines the name and saves the resulting image in the same folder
newscript.ps1
My best current non-working code is:
$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
I found Set-Content
which is able to handle special characters in pathes through "-literalpath". But I'm still not able to convert the batch to a Powershell script because
Set-Content (and Out-File method too) seems work different compared to old batch piping (">"). The resulting image is not viewable regardless which encoding I use. The help file from above says that exiftool is using UTF8 encoding.
Of course I tried other available encodings, but all of them failed to produce a viewable image. I'm stuck at this point. So my initial question still stands partly "How do I convert this batch file to powershell".
So why is it working when using the old batch command?
For example: create a folder "D:folder" and place this MP3 file with a cover image in it.
Download exiftool.exe from above and place it there too.
The old batch command will work and give you a viewable image
D:\folder\exiftool -picture -binary D:\folder\input.mp3 > D:\folder\output.jpg
The new Powershell V2 script with the same syntax will fail. Why?
& D:\folder\exiftool.exe -picture -binary D:\folder\input.mp3 > D:\folder\output.jpg
You can try this, though I've not tested it 'cause I've not an mp3 with embedded images:
$file = & "D:\folder\exiftool.exe" -picture -binary "D:\folder\input.mp3"
[io.file]::WriteAllBytes('D:\folder\input[1].jpg',$file)
Edit:
using this line from a powershell console return a readable image:
cmd.exe /c "D:\folder\exiftool.exe -picture -binary `"D:\folder\input.mp3`" > image.jpg"
You can use special characters in path and in file name as:
$exe = "c:\ps\exiftool.exe"
$mp3 = "c:\ps\a[1]\input.mp3"
$jpg = " c:\ps\a[1]\image[1].jpg"
cmd.exe /c "$exe -picture -binary $mp3 > $jpg"
with spaces inside path:
$exe = "c:\ps\exiftool.exe"
$mp3 = "`"c:\ps\a [1]\input.mp3`""
$jpg = "`"c:\ps\a [1]\image [1].jpg`""
cmd.exe /c "$exe -picture -binary $mp3 > $jpg"
这篇关于使用路径中的特殊字符将批处理文件转换为Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!