且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在文件选择器中批量添加过滤器?

更新时间:2023-09-22 10:38:22

如何使用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