Windows 批处理脚本中的文件/文件夹选择器对话框

File / folder chooser dialog from a Windows batch script(Windows 批处理脚本中的文件/文件夹选择器对话框)
本文介绍了Windows 批处理脚本中的文件/文件夹选择器对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,要求用户为批处理脚本提供文件名是一件很麻烦的事情,不需要拼写错误、用空格括起来的路径等等.不幸的是,用户并不以准确性着称.在直到运行时才知道输入文件位置的情况下,使用 GUI 进行文件选择输入可以减少用户出错的可能性.

有没有办法从 Windows 批处理脚本中调用 File...Open 样式的 gui 文件选择器或文件夹选择器?

如果脚本用户安装了 PowerShell 或 .NET,则可以.请参阅下面的答案.

我也很想看看其他人可以提供哪些其他解决方案.

解决方案

文件浏览器

2016.3.20 更新:

由于 PowerShell 是当今几乎所有现代 Windows 安装的本机组件,因此我声明不再需要 C# 后备.如果您仍然需要它来兼容 Vista 或 XP,我.)请参阅 文档了解您可以设置的其他属性,例如 RootFolder.我原来的 .NET System.Windows.Forms PowerShell 和 C# 解决方案可以在 revision 4 中找到如果需要的话,这个答案,但这个 COM 方法更容易阅读和维护.

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.

Is there a way to invoke a File... Open style gui file chooser or folder chooser from a Windows batch script?

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.

解决方案

FileBrowser

Update 2016.3.20:

Since PowerShell is a native component of pretty much 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.

<# : chooser.bat
:: launches a File... Open sort of file chooser and outputs choice(s) to the console
:: https://stackoverflow.com/a/15885133/1683264

@echo off
setlocal

for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0} | out-string)"') do (
    echo You chose %%~I
)
goto :EOF

: end Batch portion / begin PowerShell hybrid chimera #>

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

This results in a file chooser dialog.

The result of a selection outputs You chose C:UsersmeDesktop mp.txt to the console. If you want to force single file selection, just change the $f.Multiselect property to $false.

(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.


FolderBrowser

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.

:: fchooser.bat
:: launches a folder chooser and outputs choice to the console
:: https://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

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:UsersmeDesktop to the console.

See the FolderBrowserDialog class documentation for other properties you can set, such as RootFolder. My original .NET System.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.

这篇关于Windows 批处理脚本中的文件/文件夹选择器对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)