如何在文件选择器中批量添加过滤器? [英] How to add filter to a file chooser in batch?

查看:170
本文介绍了如何在文件选择器中批量添加过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码创建了一个文件选择器,其中包含批处理 Windows批处理脚本中的文件/文件夹选择器对话框

I'm using this code to create a file chooser with batch 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档案

For example I would like to add only .rar to select just winrar files

推荐答案

如何使用powershell?

How about using 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,用于文件夹选择

BTW, for folder selection

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

这篇关于如何在文件选择器中批量添加过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆