0

I am getting crazy to do something that shall be relatively easy... I am using the code in File / folder chooser dialog from a Windows batch script in a bat file to open the file dialog and let the user select multiple files. What I need to do next is to concatenate the result of the selection in a single string to pass as parameters to another application. I am failing to concatenate the strings...

What I have is:

<# : 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 }

And the result I get is:

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>

Can somebody help?

Community
  • 1
  • 1
rodedo
  • 791
  • 4
  • 10
  • 23

1 Answers1

0

your problem is the pipesymbol (|), which is executed by the parser. set slctn=!slctn!|%%~I gives you an errormessage, because it gets parsed as set slctn=!slctn! and pipes it's output (which is empty) to another command, which in this case is the content of %%I.

You can set your variable like this: set "slctn=!slctn!|%%~I" (you can verify with set slctn).

But you will fall into the same problem with your line echo %slctn%. You have to use qoutes around the string to echo it properly: echo "%slctn%".
If showing quotes is not an option, it gets a bit more complicated:
for %%i in ("%slctn%") do echo %%~i

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • The other option is to use delayed expansion when displaying the value. But the OP should be aware that variables have a max length <8191 bytes. – dbenham May 27 '16 at 15:35
  • Thanks, almost perfect... now I do not get the error any longer, but the variable reading is not working as expected, I get `C:\Users\Public\Documents\MCI\Sito Web\Insieme>MergePDFInsieme.cmd You chose C:\Users\Public\Documents\MCI\Sito Web\Ristorante\2016-02-15\menugiornaliero.pdf You chose C:\Users\Public\Documents\MCI\Sito Web\Ristorante\2016-02-15\specialitasettimana.pdf "!slctn!|C:\Users\Public\Documents\MCI\Sito Web\Ristorante\2016-02-15\specialitasettimana.pdf"` – rodedo May 28 '16 at 07:39
  • by adding the `EnableDelayedExpansion` the answer worked completely. – rodedo May 28 '16 at 09:24