从Windows批处理脚本文件/文件夹选择对话框 [英] File / folder chooser dialog from a Windows batch script
问题描述
通常情况下,要求用户提供一个文件名的批处理脚本是一个很麻烦的事,不需要拼写错误,报价围绕路径与空间,等等。不幸的是,用户不会被公知的准确性。在输入文件位置是未知的,直到运行时的情况下,使用GUI的文件选择输入减少用户错误的可能性。
Typically, asking the user to supply a file name to a batch script is a messy affair, requiring no misspellings, quotes around paths with spaces, and so forth. Unfortunately, users aren't well-known for accuracy. In situations where input file location is not known until runtime, using a GUI for file selection input reduces the likelihood of user error.
有没有办法来调用文件...打开
从Windows批处理脚本风格的GUI文件选择器或文件夹选择器?
Is there a way to invoke a File... Open
style gui file chooser or folder chooser from a Windows batch script?
如果脚本用户拥有的PowerShell或.NET安装,它是可能的。请参阅下面的答案。
If the script user has PowerShell or .NET installed, it is possible. See the answer below.
我也有兴趣,看看有什么其他的解决方案,任何人都可以提供。
I'm also interested to see what other solutions anyone else can offer.
推荐答案
如果您已经安装了PowerShell中或.NET,你可以做这样的事情:
File Browser
If you've got PowerShell or .NET installed, you can do something like this:
:: chooser.bat
:: launches a File... Open sort of file chooser and outputs choice to the console
:: http://stackoverflow.com/a/15885133/1683264
@echo off
setlocal enabledelayedexpansion
:: Does powershell.exe exist within %PATH%?
for %%I in (`powershell.exe`) do if "%%~$PATH:I" neq "" (
set chooser=powershell "Add-Type -AssemblyName System.windows.forms|Out-Null;$f=New-Object System.Windows.Forms.OpenFileDialog;$f.InitialDirectory='%cd%';$f.Filter='Text Files (*.txt)|*.txt|All Files (*.*)|*.*';$f.showHelp=$true;$f.ShowDialog()|Out-Null;$f.FileName"
) else (
rem :: If not, compose and link C# application to open file browser dialog
set chooser=%temp%\chooser.exe
>"%temp%\c.cs" echo using System;using System.Windows.Forms;
>>"%temp%\c.cs" echo class dummy{
>>"%temp%\c.cs" echo public static void Main^(^){
>>"%temp%\c.cs" echo OpenFileDialog f=new OpenFileDialog^(^);
>>"%temp%\c.cs" echo f.InitialDirectory=Environment.CurrentDirectory;
>>"%temp%\c.cs" echo f.Filter="Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
>>"%temp%\c.cs" echo f.ShowHelp=true;
>>"%temp%\c.cs" echo f.ShowDialog^(^);
>>"%temp%\c.cs" echo Console.Write^(f.FileName^);}}
for /f "delims=" %%I in ('dir /b /s "%windir%\microsoft.net\*csc.exe"') do (
if not exist "!chooser!" "%%I" /nologo /out:"!chooser!" "%temp%\c.cs" 2>NUL
)
del "%temp%\c.cs"
if not exist "!chooser!" (
echo Error: Please install .NET 2.0 or newer, or install PowerShell.
goto :EOF
)
)
:: capture choice to a variable
for /f "delims=" %%I in ('%chooser%') do set "filename=%%I"
echo You chose %filename%
:: Clean up the mess
del "%temp%\chooser.exe" 2>NUL
goto :EOF
这会导致一个文件选择对话框。
This results in a file chooser dialog.
的选择输出您选择C中的结果:\用户\我\桌面\ tmp.txt
控制台
(PowerShell命令毫不留情地从中浸出只需修改一下博客。)请参阅<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx">OpenFileDialog类文档其他属性可以设置,如标题
和 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
.
更新2015年8月10日:
既然已经是调用的文件夹选择器 COM方法,它是pretty的容易建立一个PowerShell单行程序可以打开的文件夹选择器和输出路径。
Since there is already a COM method for invoking a folder chooser, it's pretty easy to build a PowerShell one-liner that can open the folder chooser and output the path.
:: fchooser.bat
:: launches a folder chooser and outputs choice to the console
:: http://stackoverflow.com/a/15885133/1683264
@echo off
setlocal
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose a folder.',0,0).self.path""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"
setlocal enabledelayedexpansion
echo You chose !folder!
endlocal
在 BrowseForFolder()
方法,第四个参数指定的层次结构的根。请参阅<一href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb774096%28v=vs.85%29.aspx">ShellSpecialFolderConstants有效值的列表。
In the BrowseForFolder()
method, the fourth argument specifies the root of the hierarchy. See ShellSpecialFolderConstants for a list of valid values.
旧的回答:
打开文件夹选择器有一点不同,需要一个单线程的公寓和一点点不同的语法。
Opening a folder chooser is a little different, requiring a single-threaded apartment and a little different syntax.
:: fchooser-old.bat
:: launches a folder chooser and outputs choice to the console
:: http://stackoverflow.com/a/15885133/1683264
@echo off
setlocal enabledelayedexpansion
:: Does powershell.exe exist within %PATH%?
for %%I in (`powershell.exe`) do if "%%~$PATH:I" neq "" (
set chooser=powershell -sta "Add-Type -AssemblyName System.windows.forms|Out-Null;$f=New-Object System.Windows.Forms.FolderBrowserDialog;$f.SelectedPath='%cd%';$f.Description='Please choose a folder.';$f.ShowNewFolderButton=$true;$f.ShowDialog();$f.SelectedPath"
) else (
rem :: If not, compose and link C# application to open folder browser dialog
set chooser=%temp%\fchooser.exe
if exist !chooser! del !chooser!
>"%temp%\c.cs" echo using System;using System.Windows.Forms;
>>"%temp%\c.cs" echo class dummy{[STAThread]
>>"%temp%\c.cs" echo public static void Main^(^){
>>"%temp%\c.cs" echo FolderBrowserDialog f=new FolderBrowserDialog^(^);
>>"%temp%\c.cs" echo f.SelectedPath=System.Environment.CurrentDirectory;
>>"%temp%\c.cs" echo f.Description="Please choose a folder.";
>>"%temp%\c.cs" echo f.ShowNewFolderButton=true;
>>"%temp%\c.cs" echo if^(f.ShowDialog^(^)==DialogResult.OK^){Console.Write^(f.SelectedPath^);}}}
for /f "delims=" %%I in ('dir /b /s "%windir%\microsoft.net\*csc.exe"') do (
if not exist "!chooser!" "%%I" /nologo /out:"!chooser!" "%temp%\c.cs" 2>NUL
)
del "%temp%\c.cs"
if not exist "!chooser!" (
echo Error: Please install .NET 2.0 or newer, or install PowerShell.
goto :EOF
)
)
:: capture choice to a variable
for /f "delims=" %%I in ('%chooser%') do set "folder=%%I"
echo You chose %folder%
:: Clean up the mess
del "%temp%\fchooser.exe" 2>NUL
goto :EOF
这会导致一个文件夹选择对话框。
This results in a folder chooser dialog.
的选择输出您选择C中的结果:\用户\我\桌面
控制台
见<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.aspx">FolderBrowserDialog类文档其他属性可以设置,如 RootFolder
。
See the FolderBrowserDialog class documentation for other properties you can set, such as RootFolder
.
这篇关于从Windows批处理脚本文件/文件夹选择对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!