2016-05-27 9 views
0

私は比較的簡単になるように夢中になっています... File/folder chooser dialog from a Windows batch scriptのコードを使用してファイルダイアログを開いて、複数のファイルを選択させます。 次に、選択した結果を1つの文字列に連結して、別のアプリケーションにパラメータとして渡す必要があります。1文字列のファイルダイアログの出力を連結する

<# : chooser.bat 
:: launches a File... Open sort of file chooser and outputs choice(s) to the console 
:: https://stackoverflow.com/a/15885133/1683264 

@echo off 
setlocal 

set slctn= 

for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0} | out-string)"') do (
    echo You chose %%~I 
    set slctn=!slctn!|%%~I 
) 

REM here use %slctn% in another command 
echo %slctn% 
goto :EOF 

: end Batch portion/begin PowerShell hybrid chimera #> 

Add-Type -AssemblyName System.Windows.Forms 
$f = new-object Windows.Forms.OpenFileDialog 
$f.InitialDirectory = "C:\Users\Public\Documents\MCI\Sito Web" 
$f.Title = "Seleft PDF files to merge" 
$f.Filter = "PDF Files (*.pdf)|*.pdf|All Files (*.*)|*.*" 
$f.ShowHelp = $true 
$f.Multiselect = $true 
[void]$f.ShowDialog() 
if ($f.Multiselect) { $f.FileNames } else { $f.FileName } 

そして私が得る結果は次のとおりです:

C:\Users\Public\Documents\MCI\Sito Web\Insieme>MergePDFInsieme 
You chose C:\Users\Public\Documents\MCI\Sito Web\Ristorante\2016-02-08\menugiornaliero.pdf 
'C:\Users\Public\Documents\MCI\Sito' is not recognized as an internal or external command, operable program or batch file. 

C:\Users\Public\Documents\MCI\Sito Web\Insieme> 

は、誰かが助けることができます私は

私は何を持っていることである...文字列を連結することができないのですか?

答えて

0

あなたの問題は、パーサーによって実行されるpipesymbol(|)です。 set slctn=!slctn!|%%~Iは、set slctn=!slctn!として解析され、それが(この場合は%%Iの内容である)別のコマンドに出力されます(空である)ので、エラーメッセージを返します。

set "slctn=!slctn!|%%~I"のように変数を設定できます(set slctnで確認できます)。

しかし、同じ問題が発生するのは、echo %slctn%です。あなたはそれを正しくエコーするために文字列の周りにqoutesを使用しなければなりません:echo "%slctn%"
引用符を示すことはできない場合は、それは少し複雑になります:
for %%i in ("%slctn%") do echo %%~i

+0

他のオプションは、値を表示する際に遅延拡張を使用することです。しかし、OPは、変数の最大長が<8191バイトであることを認識する必要があります。 – dbenham

+0

ありがとう、ほぼ完璧です...私はもうエラーは表示されませんが、変数の読み込みが期待通りに機能していないので、 'C:\ Users \ Public \ Documents \ MCI \ Sito Web \ Insieme> MergePDFInsieme.cmd C:\ Users \ Public \ Documents \ MCI \ Sito Web \ Ristorante \ 2016-02を選択した場合は、C:\ Users \ Public \ Documents \ MCI \ Sito Web \ Ristorante \ 2016-02-15 \ menugiornaliero.pdf を選択しました。 -15 \ specialitasettimana.pdf "!Slctn!| C:\ Users \ Public \ Documents \ MCI \ Sito Web \ Ristorante \ 2016-02-15 \ specialitasettimana.pdf" – rodedo

+0

答えを完全に有効にした 'EnableDelayedExpansion'を追加する。 – rodedo