2013-02-17 81 views
0

我很難寫一個簡單的批處理文件作爲PowerShell腳本。將批處理文件轉換爲具有路徑中特殊字符的PowerShell

考慮這個文件夾結構。請注意目錄與它的涼爽[1] ... enter image description here

exiftool.exe
是一個命令實用程序(例如)從嵌入MP3標籤中提取的照片。
I uploaded its help如果您需要更多信息。

oldscript.cmd
exiftool -picture -b input.mp3 > output.jpg
這條線是在的powershell寫的一個。我發現一個forum post語法從筆者

  • -picture代表標籤提取和-b代表二進制模式
  • input.mp3是我測試的MP3可以包含在這樣的路徑中的特殊字符[和]
  • > output.jpg定義了名稱,並在同一文件夾

newscript.ps1 012保存所得到的圖像 我目前最好的非工作代碼爲:

$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」來處理特殊字符pathes。但是我仍然無法將批處理轉換爲Powershell腳本,因爲 Set-Content(和Out-File方法)似乎與舊批處理管道(「>」)相比工作不同。無論我使用哪種編碼,結果圖像都不可見。 help file from above表示exiftool使用UTF8編碼。

當然,我嘗試了其他available encodings,但他們都沒有產生可視圖像。我被困在這一點上。所以我最初的問題仍然部分地「如何將此批處理文件轉換爲PowerShell」。

那麼,爲什麼使用舊的批處理命令時它工作?

例如:創建一個文件夾「D:文件夾」,並將其放置在其中。
從上面下載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 

回答

1

你可以試試這個,雖然我沒有測試它,因爲我還沒有與嵌入圖像的MP3:使用從PowerShell控制檯回報這條線

$file = & "D:\folder\exiftool.exe" -picture -binary "D:\folder\input.mp3" 

[io.file]::WriteAllBytes('D:\folder\input[1].jpg',$file) 

編輯可讀的圖像:

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" 
+0

經過測試。給「WriteAllBytes」鍵入「System.Byte []」提供錯誤「無法轉換參數1」,值爲「System.Object []」:「無法轉換值」「鍵入」System .Byte「。錯誤:」輸入字符串格式不正確。「」'如果您需要測試,請使用[示例MP3](http://ge.tt/4PUs8hY/v/1)。 – nixda 2013-02-17 13:27:01

+0

@nixda謝謝你的mp3例子。我找到了一個替代方案,在我的答案中添加了代碼。 – 2013-02-17 15:54:51

+0

關閉一個。在「a [1]」»「a [1]」中插入一個空格,它將不起作用。我想這些變量需要用雙引號括起來。但是如何? – nixda 2013-02-17 17:04:59

0

試試這個:

& $exe -picture -b $input | Out-File -LiteralPath $output 

沒有必要使用開始處理使事情變得複雜。因爲您計算exe的路徑並將結果放入字符串中,所以只需使用調用運算符&來調用由它後面的字符串命名的命令。

+1

你測試過了嗎?因爲Out-File [不理解-LiteralPath](http://ss64.com/ps/out-file.html)。即使刪除特殊字符'[1]',輸出文件也會被創建但不可見。如果從批處理調用該工具,但不從PS調用該工具。 – nixda 2013-02-17 02:48:59

+0

它適用於PowerShell 3.0。 http://www.microsoft.com/en-us/download/details.aspx?id=34595 – 2013-02-17 03:09:44

+0

請參閱我的編輯。我更進一步,在V2中也有字面路徑的Set-Content。但生成的文件與舊窗口批處理和管道有所不同,與「>」 – nixda 2013-02-17 05:17:50

0

這裏是一個變通。看來你不能完全避免良好的舊cmd.exe。
感謝應該去@ CB

$ownpath = Split-Path $MyInvocation.MyCommand.Path 
$exe = $ownpath + '\exiftool.exe' 
$input = $ownpath + '\input.mp3' 
$output = $ownpath + '\output.jpg' 

cmd.exe /c " `"$exe`" -picture -binary `"$input`" > `"$output`" " 

enter image description here

注:

  • 這樣,所有pathes可以包含特殊字符,如[和]或空格
  • ,在額外的空間" `"$exe很重要。沒有它,它就無法工作。

正常Powershell的方式與set-contentOut-File( 「>」 是一個別名)和[io.file]::WriteAllBytes所有不與exiftool.exe實用的工作。對我來說這是一個奇蹟。

相關問題