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

roj*_*ojo 36 .net windows powershell cmd batch-file

通常,要求用户向批处理脚本提供文件名是一件混乱的事情,不需要拼写错误,带有空格的路径周围的引号等等.不幸的是,用户并不知道准确性.在直到运行时才知道输入文件位置的情况下,使用GUI进行文件选择输入可降低用户错误的可能性.

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

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

我也有兴趣看看其他人能提供的其他解决方案.

roj*_*ojo 48

文件浏览器

更新2016.3.20:

由于PowerShell是当今几乎所有现代Windows安装的本机组件,因此我宣布不再需要C#回退.如果您仍然需要它兼容Vista或XP,我将其移至新的答案.从这个编辑开始,我将脚本重写为Batch + PowerShell混合,并结合了执行多选的功能.根据需要阅读和调整非常容易.

<# : 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 }
Run Code Online (Sandbox Code Playgroud)

这导致文件选择器对话框.

文件选择器

选择结果输出You chose C:\Users\me\Desktop\tmp.txt到控制台.如果要强制单个文件选择,只需将$f.Multiselect属性更改为$false.

(PowerShell命令从Just Tinkering博客中毫不含糊地获取.)请参阅OpenFileDialog类文档,了解您可以设置的其他属性,例如TitleInitialDirectory.


Folder Browser

更新2015.08.10:

由于已经有一个用于调用文件夹选择器的COM方法,因此构建一个可以打开文件夹选择器并输出路径的PowerShell单行程非常容易.

:: 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
Run Code Online (Sandbox Code Playgroud)

在该BrowseForFolder()方法中,第四个参数指定层次结构的根.有关有效值的列表,请参见ShellSpecialFolderConstants.

这会导致文件夹选择器对话框.

在此输入图像描述

选择结果输出You chose C:\Users\me\Desktop到控制台.

有关您可以设置的其他属性,请参阅FolderBrowserDialog类文档,例如RootFolder.如果需要,我System.Windows.Forms可以在本答案的修订版4中找到我原来的.NET PowerShell和C#解决方案,但是这种COM方法更容易阅读和维护.


Ant*_*Via 10

这应该从XP向上工作,并且不需要hibrid文件,它只使用长命令行运行mshta:

@echo off
set dialog="about:<input type=file id=FILE><script>FILE.click();new ActiveXObject
set dialog=%dialog%('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);
set dialog=%dialog%close();resizeTo(0,0);</script>"

for /f "tokens=* delims=" %%p in ('mshta.exe %dialog%') do set "file=%%p"
echo selected  file is : "%file%"
pause
Run Code Online (Sandbox Code Playgroud)


roj*_*ojo 8

Windows脚本宿主


文件选择

Windows XP有一个神秘的UserAccounts.CommonDialogWSH对象,允许VBScript和JScript启动文件选择提示.显然,这被视为安全风险并在Vista中被删除.


文件夹选择

但是,WSH Shell.Application对象BrowseForFolder方法仍将允许创建文件夹选择对话框.这是一个混合批处理+ JScript示例.用.bat扩展名保存.

@if (@a==@b) @end /*

:: fchooser2.bat
:: batch portion

@echo off
setlocal

for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0"') do (
    echo You chose %%I
)

goto :EOF

:: JScript portion */

var shl = new ActiveXObject("Shell.Application");
var folder = shl.BrowseForFolder(0, "Please choose a folder.", 0, 0x00);
WSH.Echo(folder ? folder.self.path : '');
Run Code Online (Sandbox Code Playgroud)

文件夹选择对话框

在该BrowseForFolder()方法中,第四个参数指定层次结构的根.有关有效值的列表,请参见ShellSpecialFolderConstants.


Aac*_*ini 6

可以使用纯批处理来选择文件/文件夹,如下所示。当然,感觉和外观不像GUI那样令人愉悦,但是它的效果非常好,我认为它比GUI版本更易于使用。选择方法基于CHOICE命令,因此需要在不包含该选择的Windows版本中下载它,并稍加修改其参数。当然,可以使用SET / P代替CHOICE轻松地修改代码,但是此更改将消除仅需一次按键即可导航和选择的非常简单和快速的选择方法。

@echo off
setlocal

rem Select a file or folder browsing a directory tree
rem Antonio Perez Ayala

rem Usage examples of SelectFileOrFolder subroutine:

call :SelectFileOrFolder file=
echo/
echo Selected file from *.* = "%file%"
pause

call :SelectFileOrFolder file=*.bat
echo/
echo Selected Batch file = "%file%"
pause

call :SelectFileOrFolder folder=/F
echo/
echo Selected folder = "%folder%"
pause

goto :EOF


:SelectFileOrFolder resultVar [ "list of wildcards" | /F ]

setlocal EnableDelayedExpansion

rem Process parameters
set "files=*.*"
if "%~2" neq "" (
   if /I "%~2" equ "/F" (set "files=") else set "files=%~2"
)

rem Set the number of lines per page, max 34
set "pageSize=30"
set "char=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

rem Load current directory contents
set "name[1]=<DIR>  .."
:ProcessThisDir
set "numNames=1"
for /D %%a in (*) do (
   set /A numNames+=1
   set "name[!numNames!]=<DIR>  %%a"
)
for %%a in (%files%) do (
   set /A numNames+=1
   set "name[!numNames!]=       %%a"
)
set /A numPages=(numNames-1)/pageSize+1

rem Show directory contents, one page at a time
set start=1
:ShowPage
set /A page=(start-1)/pageSize+1, end=start+pageSize-1
if %end% gtr %numNames% set end=%numNames%
cls
echo Page %page%/%numPages% of %CD%
echo/
if %start% equ 1 (set base=0) else set "base=1"
set /A lastOpt=pageSize+base, j=base
for /L %%i in (%start%,1,%end%) do (
   for %%j in (!j!) do echo     !char:~%%j,1! -  !name[%%i]!
   set /A j+=1
)
echo/

rem Assemble the get option message
if %start% equ 1 (set "mssg=: ") else (set "mssg= (0=Previous page")
if %end% lss %numNames% (
   if "%mssg%" equ ": " (set "mssg= (") else set "mssg=%mssg%, "
   set "mssg=!mssg!Z=Next page"
)
if "%mssg%" neq ": " set "mssg=%mssg%): "

:GetOption
choice /C "%char%" /N /M "Select desired item%mssg%"
if %errorlevel% equ 1 (
   rem "0": Previous page or Parent directory
   if %start% gtr 1 (
      set /A start-=pageSize
      goto ShowPage
   ) else (
      cd ..
      goto ProcessThisDir
   )
)
if %errorlevel% equ 36 (
   rem "Z": Next page, if any
   if %end% lss %numNames% (
      set /A start+=pageSize
      goto ShowPage
   ) else (
      goto GetOption
   )
)
if %errorlevel% gtr %lastOpt% goto GetOption
set /A option=start+%errorlevel%-1-base
if %option% gtr %numNames% goto GetOption
if defined files (
   if "!name[%option%]:~0,5!" neq "<DIR>" goto endSelect
) else (
   choice /C OS /M "Open or Select '!name[%option%]:~7!' folder"
   if errorlevel 2 goto endSelect
)
cd "!name[%option%]:~7!"
goto ProcessThisDir

:endSelect
rem Return selected file/folder
for %%a in ("!name[%option%]:~7!") do set "result=%%~Fa"
endlocal & set "%~1=%result%
exit /B
Run Code Online (Sandbox Code Playgroud)


Esp*_*o57 6

在批处理中直接运行 PowerShell 命令的其他解决方案

rem preparation command
set pwshcmd=powershell -noprofile -command "&{[System.Reflection.Assembly]::LoadWithPartialName('System.windows.forms') | Out-Null;$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog; $OpenFileDialog.ShowDialog()|out-null; $OpenFileDialog.FileName}"

rem exec commands powershell and get result in FileName variable
for /f "delims=" %%I in ('%pwshcmd%') do set "FileName=%%I"

echo %FileName%
Run Code Online (Sandbox Code Playgroud)


roj*_*ojo 5

Batch + PowerShell + C# 多语言解决方案

这与Batch + PowerShell 混合解决方案相同,但为 XP 和 Vista 兼容性重新添加了 C# 后备内容。应 xNightmare67x 的要求添加了多个文件选择。

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

@echo off
setlocal enabledelayedexpansion

rem // Does powershell.exe exist within %PATH%?

for %%I in ("powershell.exe") do if "%%~$PATH:I" neq "" (
    set chooser=powershell -noprofile "iex (${%~f0} | out-string)"
) 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;
        echo using System.Windows.Forms;
        echo class dummy {
        echo    public static void Main^(^) {
        echo        OpenFileDialog f = new OpenFileDialog^(^);
        echo        f.InitialDirectory = Environment.CurrentDirectory;
        echo        f.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
        echo        f.ShowHelp = true;
        echo        f.Multiselect = true;
        echo        f.ShowDialog^(^);
        echo        foreach ^(String filename in f.FileNames^) {
        echo            Console.WriteLine^(filename^);
        echo        }
        echo    }
        echo }
    )
    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
    )
)

rem // Do something with the chosen file(s)
for /f "delims=" %%I in ('%chooser%') do (
    echo You chose %%~I
)

rem // comment this out to keep chooser.exe in %temp% for faster subsequent runs
del "%temp%\chooser.exe" >NUL 2>NUL

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 }
Run Code Online (Sandbox Code Playgroud)

对于 XP 或 Vista 的文件夹选择器,请使用WSH 解决方案npocmaka 的 HTA 解决方案