问题描述
我想知道是否可以从cmd打开打开-dialogbox,所以我可以使用它来选择正确的文件打开。
(我已经知道如何直接从cmd打开文件,但这不是我感兴趣的)
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)
现在我正在做一个项目其中我使用一个程序(例如program.exe)。这个程序需要一个modelfile(例如modelfile.mod)。
我在CMD中执行程序,编写:program.exe modelfile.mod,它的工作。
我有很多modelfiles有不同的名字,但程序文件总是有相同的名称。而不是写在CMD每次我需要执行程序,我想创建一个批处理,我可以选择我喜欢执行的模块,然后它执行程序与选择的modfile作为输入。
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.
因此,我需要知道如何从cmd打开open-dialogbox,以及如何获取所选文件的名称以用于程序的执行。
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.
推荐答案
如果你安装了PowerShell,你可以这样做:
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
或者如果您想要分解powershell cmdlet以更容易维护:
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命令无情地从。 )有关其他属性,请参见文档您可以设置,例如标题
和 InitialDirectory
。
(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
.
这篇关于从cmd中打开“打开”对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!