3

I want to know if I can open the "open"-dialogbox from the cmd, so I can use that to choose the right file to open. (I already know how to open the file directly from the cmd, but thats not what I interested in)

At the moment I'm doing a project where i use a program (e.g. program.exe). This program needs a modelfile (e.g. modelfile.mod). I execute the program in the CMD by writing: program.exe modelfile.mod, and it work. I have many modelfiles with different names, but the program file always has the same name. Instead of writing in the CMD every time I need to execute the program I would like to create a batch where I can choose the modfile I like to execute whereafter it executes the program with the chosen modfile as input.

Therefore I need to know how to open the "open"-dialogbox from the cmd, and also how to get the name of the chosen file for use in execution of the program.

tshepang
  • 12,111
  • 21
  • 91
  • 136
skrl
  • 47
  • 2
  • 6
  • cmd has a command line interface, there are no dialog boxes. – Endoro Mar 26 '13 at 18:39
  • I do agree that the CMD has a command line interface, but by using e.g. "start ." or "explorer ." a dialogbox can be show where all files can be shown. But these functions can only open files, where I at the moment want to know what file there have been selected in the dialogbox. – skrl Mar 26 '13 at 20:31
  • "start ." translates to "start " and the default-program for an directory is to start the explorer (as "start www.google.com" would start your default browser). – Stephan Mar 27 '13 at 10:32
  • Welcome to S.O. lst. Since you're new, you should have a look at [this information](http://meta.stackexchange.com/a/5235/187716) to understand how to mark an answer as accepted. – rojo Apr 08 '13 at 16:33

1 Answers1

3

If you've got PowerShell installed, you can do something like this:

@echo off
setlocal
set ps_cmd=powershell "Add-Type -AssemblyName System.windows.forms|Out-Null;$f=New-Object System.Windows.Forms.OpenFileDialog;$f.Filter='Model Files (*.mod)|*.mod|All files (*.*)|*.*';$f.showHelp=$true;$f.ShowDialog()|Out-Null;$f.FileName"

for /f "delims=" %%I in ('%ps_cmd%') do set "filename=%%I"

if defined filename (
    echo You chose %filename%
) else (
    echo You didn't choose squat!
)

goto :EOF

Or if you want to break down the powershell cmdlets for easier maintenance:

@echo off
setlocal
set "ps=Add-Type -AssemblyName System.windows.forms | Out-Null;"
set "ps=%ps% $f=New-Object System.Windows.Forms.OpenFileDialog;"
set "ps=%ps% $f.Filter='Model Files (*.mod)|*.mod|All files (*.*)|*.*';"
set "ps=%ps% $f.showHelp=$true;"
set "ps=%ps% $f.ShowDialog() | Out-Null;"
set "ps=%ps% $f.FileName"

for /f "delims=" %%I in ('powershell "%ps%"') do set "filename=%%I"

if defined filename (
    echo You chose %filename%
) else (
    echo You didn't choose squat!
)

goto :EOF

(PowerShell command mercilessly leeched from the Just Tinkering Blog.) See the OpenFileDialog Class documentation for other properties you can set, such as Title and InitialDirectory.

rojo
  • 24,000
  • 5
  • 55
  • 101