2016-05-27 31 views
0

我越来越疯狂,要做一些相对容易的事情... 我在使用bat文件中的代码File/folder chooser dialog from a Windows batch script来打开文件对话框并让用户选择多个文件。 接下来我需要做的是将单个字符串中的选择结果连接起来,作为参数传递给另一个应用程序。我无法在连接字符串...以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