How to add filter to a file chooser in batch?

前端 未结 3 1254
隐瞒了意图╮
隐瞒了意图╮ 2021-01-16 12:34

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:

        
3条回答
  •  隐瞒了意图╮
    2021-01-16 12:59

    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, 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
    

提交回复
热议问题