我正在使用此代码使用批处理File / folder chooser dialog from a Windows batch script创建文件选择器
@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
所以我的问题是:如何将筛选器批量添加到文件选择器?
例如,我只想添加
.rar
以仅选择winrar文件
最佳答案
如何使用Powershell?
@echo off
set "full="
set "file="
set "mypath=%~dp0"
call:fileSelection "%file%", "%mypath%", "Choose a file", file, mypath
set "full=%mypath%\%file%"
echo(
echo( File is: %file%
echo( Path is: %mypath%
echo(
echo( Full filename: %full%
exit/B
:fileSelection
SetLocal & set "file=%~1" & set "folder=%~2" & rem if selection is canceled restore previous data
set "dialog=powershell -sta "Add-Type -AssemblyName System.windows.forms^|Out-Null;$f=New-Object System.Windows.Forms.OpenFileDialog;$f.filename='%~1';$f.InitialDirectory='%~2';$f.title='%~3';$f.showHelp=$false;$f.Filter='RAR files (*.rar)^|*.rar^|ZIP files (*.zip)^|*.zip^|All files (*.*)^|*.*';$f.ShowDialog()^|Out-Null;$f.FileName""
for /f "delims=" %%I in ('%dialog%') do set "res=%%I"
echo "%res%" | find "\" >NUL && call:stripPath "%res%", file, folder & rem selection, otherwise cancel. Avoid this if you want full path and filename
EndLocal & set "%4=%file%" & set "%5=%folder%"
exit/B 0
:: --------------------- Split path and filename ---------------------
:stripPath
SetLocal & set "file=%~nx1" & set "folder=%~dp1"
EndLocal & set "%2=%file%" & set "%3=%folder:~0,-1%"
exit/B
BTW,用于文件夹选择
call:folderSelection "%mypath%", mypath, "Choose a folder"
:folderSelection
SetLocal & set "folder=%~1"
set "dialog=powershell -sta "Add-Type -AssemblyName System.windows.forms^|Out-Null;$f=New-Object System.Windows.Forms.FolderBrowserDialog;$f.SelectedPath='%~1';$f.Description='%~3';$f.ShowNewFolderButton=$true;$f.ShowDialog();$f.SelectedPath""
for /F "delims=" %%I in ('%dialog%') do set "res=%%I"
EndLocal & (if "%res%" EQU "" (set "%2=%folder%") else (set "%2=%res%"))
exit/B 0
关于javascript - 如何将过滤器批量添加到文件选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38369664/