Windows 批处理脚本中的文件/文件夹选择器对话框
在输入文件位置直到运行时才知道的情况下,使用GUI进行文件选择输入会降低用户错误的可能性。
有没有办法从Windows批处理脚本调用文件/文件夹选择器对话框?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在输入文件位置直到运行时才知道的情况下,使用GUI进行文件选择输入会降低用户错误的可能性。
有没有办法从Windows批处理脚本调用文件/文件夹选择器对话框?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
文件浏览器
更新 2016.3.20:
由于 PowerShell 是当今所有现代 Windows 安装的本机组件,因此我宣布不再需要 C# 回退。如果您仍然需要它来实现 Vista 或 XP 兼容性,我将其移至新答案。从这次编辑开始,我将脚本重写为 Batch + PowerShell 混合体,并结合了执行多重选择的功能。它更容易阅读并根据需要进行调整。
这会出现一个文件选择器对话框。
选择的结果将
You Choose C:\Users\me\Desktop\tmp.txt
输出到控制台。如果您想强制选择单个文件,只需将$f.Multiselect
属性更改为$false
即可。PowerShell 从 Just Tinkering 博客< /a>.请参阅
OpenFileDialog
类< /a> 您可以设置的其他属性的文档,例如Title
和InitialDirectory
。文件夹浏览器
更新2015.08.10:
因为已经有一个COM方法用于调用文件夹选择器 ,构建一个可以打开文件夹选择器并输出路径的 PowerShell 单行代码非常容易。
在 BrowseForFolder() 方法中,第四个参数指定层次结构的根。有关以下列表,请参阅 ShellSpecialFolderConstants有效值。
这会出现一个文件夹选择器对话框。
选择的结果将
You Choose C:\Users\me\Desktop
输出到控制台。有关其他属性,请参阅 FolderBrowserDialog 类文档您可以设置,例如
RootFolder
。我原来的 .NETSystem.Windows.Forms
PowerShell 和 C# 解决方案可以在 修订版 4 中找到如果需要的话,可以参考这个答案,但是这个 COM 方法更容易阅读和维护。File Browser
Update 2016.3.20:
Since PowerShell is a native component of all modern Windows installations nowadays, I'm declaring the C# fallback as no longer necessary. If you still need it for Vista or XP compatibility, I moved it to a new answer. Starting with this edit, I'm rewriting the script as a Batch + PowerShell hybrid and incorporating the ability to perform multi-select. It's profoundly easier to read and to tweak as needed.
This results in a file chooser dialog.
The result of a selection outputs
You chose C:\Users\me\Desktop\tmp.txt
to the console. If you want to force single file selection, just change the$f.Multiselect
property to$false
.PowerShell taken command from the Just Tinkering Blog. See the
OpenFileDialog
Class documentation for other properties you can set, such asTitle
andInitialDirectory
.Folder Browser
Update 2015.08.10:
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.
In the
BrowseForFolder()
method, the fourth argument specifies the root of the hierarchy. See ShellSpecialFolderConstants for a list of valid values.This results in a folder chooser dialog.
The result of a selection outputs
You chose C:\Users\me\Desktop
to the console.See the FolderBrowserDialog class documentation for other properties you can set, such as
RootFolder
. My original .NETSystem.Windows.Forms
PowerShell and C# solutions can be found in revision 4 of this answer if needed, but this COM method is much easier to read and maintain.这应该适用于 XP 及以上版本,并且不需要 hibrid 文件,它只需使用长命令行运行 mshta:
This should work from XP upwards and does'nt require an hibrid file, it just runs mshta with a long command line:
Windows脚本主机
文件选择
Windows XP有一个神秘的
useraccounts.commondialog
wsh对象和JScript启动文件选择提示。显然,那是被视为安全风险并在Vista中删除。但是,文件夹选择
,但是,wsh shell.application对象 browseforfolder 方法仍然允许创建文件夹选择对话框。这是一个混合批次 + JScript示例。用
.bat
扩展名保存。在
browseforfolder()
方法中。参见Windows Script Host
File Selection
Windows XP had a mysterious
UserAccounts.CommonDialog
WSH object which allowed VBScript and JScript to launch the file selection prompt. Apparently, that was deemed a security risk and removed in Vista.Folder Selection
However, the WSH Shell.Application object BrowseForFolder method will still allow the creation of a folder selection dialog. Here's a hybrid batch + JScript example. Save it with a
.bat
extension.In the
BrowseForFolder()
method, the fourth argument specifies the root of the hierarchy. See ShellSpecialFolderConstants for a list of valid values.可以使用纯批处理进行文件 /文件夹选择,如下所示。当然,感觉和外观并不像GUI那样令人愉悦,但是它的效果很好,我认为它比GUI版本更容易使用。选择方法基于Choice命令,因此它需要在不包括它并稍微修改其参数的Windows版本中下载它。当然,可以轻松地修改代码以使用Set /P而不是选择,但是此更改将消除非常简单且快速的选择方法,该方法仅需要一个键键即可进行导航和选择。
A file / folder selection may be done with pure Batch, as shown below. Of course, the feel and look is not as pleasant as a GUI, but it works very well and in my opinion it is easier to use than the GUI version. The selection method is based on CHOICE command, so it would require to download it in the Windows versions that don't include it and slightly modify its parameters. Of course, the code may be easily modified in order to use SET /P instead of CHOICE, but this change would eliminate the very simple and fast selection method that only requires one keypress to navigate and select.
在批处理中直接运行 PowerShell 命令的其他解决方案
Other solution with direct run PowerShell command in Batch
批次 + powershell + C#polyglot解决方案
这与批次 + powerShell hybrid ,但是有C#shartback re re re Re - 用于XP和Vista兼容性。已在 xnirting /a>。
对于用于XP或Vista的文件夹选择器,请使用 wsh解决方案或 npocmaka的HTA解决方案。
Batch + PowerShell + C# polyglot solution
This is the same solution as the Batch + PowerShell hybrid, but with the C# fallback stuff re-added for XP and Vista compatibility. Multiple file selection has been added at xNightmare67x's request.
For a folder chooser for XP or Vista, use either the WSH solution or npocmaka's HTA solution.
还有两种方法。
1.使用混合.bat/hta(必须保存为
bat
)脚本。它可以使用vbscript或javascript,但示例是javascrtipt。不创建临时文件。选择文件夹不是这样简单,需要外部 javascript 库,但选择文件很容易1.1 - 无需提交 rojo 提出的表单(参见评论):
2.由于您已经使用 powershell/net,您可以创建自编译jscript.net Hybrid。它不需要临时 cs 文件进行编译,而是直接使用内置的 jscrript.net 编译器。也不需要 powershell,代码的可读性要高得多:
Two more ways.
1.Using a hybrid .bat/hta (must be saved as a
bat
) script .It can use vbscript or javascript but the example is with javascrtipt.Does not create temp files.Selecting folder is not so easy and will require an external javascript libraries , but selecting file is easy1.1 - without submit form proposed by rojo (see comments):
2.As you already using powershell/net you can create selfcompiled jscript.net hybrid.It will not require temp cs file for compilation and will directly use the built-in jscrript.net compiler.There's no need of powershell too and the code is far more readable:
我什至会留下一个“回声”来验证此代码中的多项选择是否有效
I will leave an 'echo' even to verify that multiple choice works in this code
我已经写了自己的便携式解决方案:
您可以从此处下载可执行文件:
该实用程序对WXWIDGETS 3.1.x有依赖性,因此您实际上可以为其他操作系统构建它。
I has been wrote my own portable solution:
https://github.com/andry81/contools/tree/HEAD/Utilities/src/_gui/wxFileDialog/
You can download executable from here:
https://github.com/andry81/contools/tree/HEAD/Utilities/bin/contools/wxFileDialog.exe
The utility has dependency on wxWidgets 3.1.x, so you can actually build it for other operating systems.
罗霍,谢谢你的代码。有用。
一个小注意事项:
对于“浏览文件夹”代码,用户可以单击 X,但根本不选择文件夹。对于这种情况,我会添加:
另外,对于 BrowseForFolder() 方法,我建议第四个参数为 0x11。这将显示所有驱动器。
rojo, thanks for the code. It works.
A small note:
For the "Browse for Folder" code, a user may click the X and NOT select a folder at all. For this case, I would add:
Also, for the BrowseForFolder() method, I recommend the 4th argument to be 0x11. This will display all drives.