0
for /f "delims=" %%i in ('powershell -noprofile "iex (${%~f0} | out-string)"') do (
    echo %%i
) >> list.txt

I want to save the total results from the loop to a variable instead of list.txt, how do I do that?

The powershell dialog box is supplied with:

Add-Type -AssemblyName System.Windows.Forms
$f = new-object Windows.Forms.OpenFileDialog
$f.InitialDirectory = pwd
$f.Filter = "BAM files (*.bam)|*.bam"
$f.ShowHelp = $false
$f.Multiselect = $true
[void]$f.ShowDialog()
if ($f.Multiselect) { $f.FileNames } else { $f.FileName }

Add-Type -AssemblyName System.Windows.Forms
$f = new-object Windows.Forms.OpenFileDialog
$f.InitialDirectory = pwd
$f.Filter = "BED Files (*.bed)|*.bed"
$f.ShowHelp = $false
$f.Multiselect = $false
[void]$f.ShowDialog()
if ($f.Multiselect) { $f.FileNames } else { $f.FileName }

I think the selected files in powershell will by supplied to each iteration in the loop, therefore I want to try output all of them in a variable (I can do it by output to a tmp file but just wondering if I can avoid that).

Really appreciate for anyone who helped!!

Eric
  • 53
  • 5
  • The `SET` command is used to assign values to environmental variables. Open up a command prompt and type: `set /?` to see the usage for the command. – Squashman Mar 06 '20 at 02:10
  • Well that really depends upon what your powershell code is doing, if as I suspect, it allows for multiple files to be input via a file dialog, then its a little bit different from allowing only a single file to be selected. You need to specify exactly which you're supplying at the dialog box. – Compo Mar 06 '20 at 02:12
  • You already have the exact code in your previous [question](https://stackoverflow.com/questions/60521898/how-to-do-folder-chooser-dialog-from-a-windows-batch-script-twice-for-different) – Squashman Mar 06 '20 at 02:12
  • @Compo yes - its a file dialog box, it's original a hybrid script, the powershell code will input multiple selected files directory in each iteration of the loop, and I have two dialog because I want to select two times for different files. I realise that the directory will all be input to the same loop no matter how many dialog boxes you opened, so if I can just output the total result of the loop then I can separate them by the file name. I know I can do this by writing into a file and it worked but I am just wondering that if I can output them in a variable to avoid creating a .tmp file. – Eric Mar 06 '20 at 02:36
  • @Squashman Yes - I have deleted it, because I found out that if I just output the total result of the for loop, I can separate the files afterwards by findstr, so I repost a simpler question. – Eric Mar 06 '20 at 02:40
  • As I said in my previous comments, you can use a `set` command to assign variables. And you were doing it in your last question using this line: `set "bed=%%b"` – Squashman Mar 06 '20 at 02:57
  • @Squashman I've tried, however the last iteration would overwrite the variable, so if I chose three files, only the last file will be saved to %%b. Therefore I want to output all past iterations saved, hence >> list.txt outside the loop, but I am seeking for a way to save it into a variable instead of list.txt...maybe it is not possible... – Eric Mar 06 '20 at 05:44
  • I believe this is what you are looking for. [How to select multiple files using the BATCH selector?](https://stackoverflow.com/questions/52240766/how-to-select-multiple-files-using-the-batch-selector) – Squashman Mar 06 '20 at 06:01

0 Answers0