How to add filter to a file chooser in batch?

老子叫甜甜 提交于 2019-12-04 02:35:33

问题


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

So my question is : How to add filter to a file chooser in batch ?

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


回答1:


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



回答2:


I found another solution in vbscript posted here :

The absolute ultimate Browse for file dialog (w95 to w8) with start folder and filters

Function GetFileDlgEx(sIniDir,sFilter,sTitle) 
 Set oDlg = CreateObject("WScript.Shell").Exec("mshta.exe ""about:<object id=d classid=clsid:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object><script>moveTo(0,-9999);eval(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0).Read("&Len(sIniDir)+Len(sFilter)+Len(sTitle)+41&"));function window.onload(){var p=/[^\0]*/;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(p.exec(d.object.openfiledlg(iniDir,null,filter,title)));close();}</script><hta:application showintaskbar=no />""") 
 oDlg.StdIn.Write "var iniDir='" & sIniDir & "';var filter='" & sFilter & "';var title='" & sTitle & "';" 
 GetFileDlgEx = oDlg.StdOut.ReadAll 
End Function
'sample test
sIniDir = "C:\MyFile.rar" 
sFilter = "RAR files (*.rar)|*.rar|" 
sTitle = "WinRar Password Cracker by Hackoo (Only Digits numbers)" 
MyFile = GetFileDlgEx(Replace(sIniDir,"\","\\"),sFilter,sTitle) 
wscript.echo MyFile

Edit : On 15/07/2016 @02:04

And to genrate it with a batch file like this :

@echo off
Title The absolute ultimate Browse for file dialog (w95 to w8) with start folder and filters
(
    echo Function GetFileDlgEx(sIniDir,sFilter,sTitle^) 
    echo Set oDlg = CreateObject("WScript.Shell"^).Exec("mshta.exe ""about:<object id=d classid=clsid:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object><script>moveTo(0,-9999);eval(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0).Read("^&Len(sIniDir^)^+Len(sFilter^)^+Len(sTitle^)+41^&"));function window.onload(){var p=/[^\0]*/;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(p.exec(d.object.openfiledlg(iniDir,null,filter,title)));close();}</script><hta:application showintaskbar=no />"""^) 
    echo oDlg.StdIn.Write "var iniDir='" ^& sIniDir ^& "';var filter='" ^& sFilter ^& "';var title='" ^& sTitle ^& "';" 
    echo GetFileDlgEx = oDlg.StdOut.ReadAll 
    echo End Function
    echo sIniDir = "C:\MyFile.rar" 
    echo sFilter = "RAR files (*.rar)|*.rar|" 
    echo sTitle = "WinRar Password Cracker by Hackoo (Only Digits numbers)" 
    echo MyFile = GetFileDlgEx(Replace(sIniDir,"\","\\"^),sFilter,sTitle^) 
    echo wscript.echo MyFile
)>"%tmp%\%~n0.vbs"
for /f "tokens=* delims=" %%p in ('Cscript /NoLogo "%tmp%\%~n0.vbs"') do set "file=%%p"
echo %file%
pause

The hole code :

@echo off
mode con cols=70 lines=20 & color 0B
Title WinRar Password Cracker by Hackoo (Only Digits numbers)
:Rar
(
    echo Function GetFileDlgEx(sIniDir,sFilter,sTitle^) 
    echo Set oDlg = CreateObject("WScript.Shell"^).Exec("mshta.exe ""about:<object id=d classid=clsid:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object><script>moveTo(0,-9999);eval(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0).Read("^&Len(sIniDir^)^+Len(sFilter^)^+Len(sTitle^)+41^&"));function window.onload(){var p=/[^\0]*/;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(p.exec(d.object.openfiledlg(iniDir,null,filter,title)));close();}</script><hta:application showintaskbar=no />"""^) 
    echo oDlg.StdIn.Write "var iniDir='" ^& sIniDir ^& "';var filter='" ^& sFilter ^& "';var title='" ^& sTitle ^& "';" 
    echo GetFileDlgEx = oDlg.StdOut.ReadAll 
    echo End Function
    echo sIniDir = "C:\MyFile.rar" 
    echo sFilter = "RAR files (*.rar)|*.rar|" 
    echo sTitle = "WinRar Password Cracker by Hackoo (Only Digits numbers)" 
    echo MyFile = GetFileDlgEx(Replace(sIniDir,"\","\\"^),sFilter,sTitle^) 
    echo wscript.echo MyFile
)>"%tmp%\%~n0.vbs"
for /f "tokens=* delims=" %%p in ('Cscript /NoLogo "%tmp%\%~n0.vbs"') do set "file=%%p"
cls
echo           ----------------------------------------------
echo                          GET DETAIL
echo           ----------------------------------------------
echo.
set "Rarext=.rar"
echo selected  file is : "%file%"
setlocal enabledelayedexpansion
for %%i in ("%file%") do (
set ext=%%~xi
IF /I Not "!Rarext!"=="!ext!" Color 0C && echo This file is not a winrar file the extension must be ".rar" & pause & cls & goto Rar
)
echo.
echo Hit any key to continue ....
pause>nul
:Main
cls
rem ******************  MAIN CODE SECTION
set STARTTIME=!TIME!
mode con cols=60 lines=15
set strProgramFiles=%ProgramFiles%
if exist "%ProgramFiles(x86)%" set strProgramFiles=%ProgramFiles(x86)%
Set Unrar="%strProgramFiles%\WinRAR\UnRar.exe"
SET PSWD=0
SET DEST=%TEMP%\Hackoo\%RANDOM%
MD %DEST%

:START
cls
color 0A
SET /A PSWD=%PSWD%+1
echo.
echo     0 1 0 %random% 1 1 0 0 1 0 0 1 %random% 1 0 1 0 0 1%random%
echo     1%random% 0 0 1 0 1 1 1 1 0 0 1 0 0 1 1 1 1 0 0 0 %random%
echo     1 1 1 1 1 0 1 1 0 0 %random% 1 1 0 1 0 1 0 0 0 1 1 %random%
echo     0 %random% 0 1 1 1 1 1 0 1 0 1 0 1 %random% 0 0 0 0 0 %random%  
echo.
echo  **********************************************************
echo    Please wait a while... Trying to crack the password... 
echo              current tested password = %PSWD%
echo  **********************************************************
echo.  
echo     0 1 0 %random% 1 1 0 0 1 0 0 1 %random% 1 0 1 0 0 1 %random%
echo     1%random% 0 0 1 0 1 1 1 1 0 0 1 0 0 1 1 1 1 0 0 0 %random%
echo     1 1 1 1 1 0 1 1 0 0 %random% 1 1 0 1 0 1 0 0 0 1 1 %random%
echo     0 %random% 0 1 1 1 1 1 0 1 0 1 0 1 %random% 0 0 0 0 0 %random%
%UNRAR% E -inul -y -P%PSWD% "%file%" "%DEST%">nul 2>&1
IF "%ERRORLEVEL%" EQU "0" GOTO :FINISH
GOTO START
:FINISH
mode con cols=60 lines=15
RD %DEST% /Q /S
cls
echo        ----------------------------------------------
echo                           CRACKED
echo        ----------------------------------------------
echo.
echo PASSWORD FOUND
echo FILE  = "%file%"
echo CRACKED PASSWORD = %PSWD%
set ENDTIME=!TIME!
call :GetDuration !STARTTIME! !ENDTIME!
exit /b
rem ******************  END MAIN CODE SECTION

:GetDuration
set function_starttime=%1
set function_endtime=%2

rem Change formatting for the start and end times
for /F "tokens=1-4 delims=:.," %%a in ("%function_starttime%") do (
   set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

for /F "tokens=1-4 delims=:.," %%a in ("%function_endtime%") do (
   set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

rem Calculate the elapsed time by subtracting values
set /A elapsed=end-start

rem Format the results for output
set /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
if %hh% lss 10 set hh=0%hh%
if %mm% lss 10 set mm=0%mm%
if %ss% lss 10 set ss=0%ss%
if %cc% lss 10 set cc=0%cc%

set DURATION=%hh%:%mm%:%ss%.%cc%

echo Start Time   : %function_starttime%
echo Finish Time  : %function_endtime%
echo          ---------------
echo Duration : %DURATION%
echo.
pause>nul



回答3:


Just, i want to share my script based on the solution of elzooilogico

Just give a try for only 2 or 3 or 4 digits numbers to crack 4 digits can be done in 5 minutes or more.

if you want to crack the password more than 4 digits numbers it will take more time of course. so, to test it, just create a new archive with winrar protected with a password for example :

password=123

@echo off
mode con cols=70 lines=15 & color 0B
:Rar
Title WinRar Password Cracker by Hackoo (Only Digits numbers)
rem set dialog="about:<input type=file id=FILE><script>FILE.click();new ActiveXObject
rem set dialog=%dialog%('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);
rem set dialog=%dialog%close();resizeTo(0,0);</script>"
rem for /f "tokens=* delims=" %%p in ('mshta.exe %dialog%') do set "file=%%p"
set "full="
set "file=" 
set "mypath=%~dp0"
call:fileSelection "%file%", "%mypath%", "Choose a file", file, mypath
set "full=%mypath%\%file%"
rem echo(
rem echo( File is: %file%
rem echo( Path is: %mypath%
rem echo(
rem echo( Full filename: %full%
rem pause
cls
echo           ----------------------------------------------
echo                          GET DETAIL
echo           ----------------------------------------------
echo.
set "Rarext=.rar"
echo selected  file is : "%full%"
setlocal enabledelayedexpansion
for %%i in ("%file%") do ( 
set ext=%%~xi
IF /I Not "!Rarext!"=="!ext!" Color 0C && echo This file is not a winrar file the extension must be ".rar" & pause & cls & goto Rar
)
echo.
echo Hit any key to continue ....
pause>nul
:Main
cls
rem ******************  MAIN CODE SECTION
set STARTTIME=!TIME!
mode con cols=60 lines=15
Set Unrar="%ProgramFiles%\WinRAR\UnRar.exe"
SET PSWD=0
SET DEST=%TEMP%\Hackoo\%RANDOM%
MD %DEST%

:START
cls
color 0A
rem echo %UNRAR% E -inul -y -P%PSWD% "%full%" "%DEST%"
SET /A PSWD=%PSWD%+1
echo.
echo     0 1 0 %random% 1 1 0 0 1 0 0 1 %random% 1 0 1 0 0 1%random%
echo     1%random% 0 0 1 0 1 1 1 1 0 0 1 0 0 1 1 1 1 0 0 0 %random%
echo     1 1 1 1 1 0 1 1 0 0 %random% 1 1 0 1 0 1 0 0 0 1 1 %random%
echo     0 %random% 0 1 1 1 1 1 0 1 0 1 0 1 %random% 0 0 0 0 0 %random%  
echo.
echo  **********************************************************
echo    Please wait a while... Trying to crack the password... 
echo              current tested password = %PSWD%
echo  **********************************************************
echo.  
echo     0 1 0 %random% 1 1 0 0 1 0 0 1 %random% 1 0 1 0 0 1 %random%
echo     1%random% 0 0 1 0 1 1 1 1 0 0 1 0 0 1 1 1 1 0 0 0 %random%
echo     1 1 1 1 1 0 1 1 0 0 %random% 1 1 0 1 0 1 0 0 0 1 1 %random%
echo     0 %random% 0 1 1 1 1 1 0 1 0 1 0 1 %random% 0 0 0 0 0 %random%
%UNRAR% E -inul -y -P%PSWD% "%full%" "%DEST%">nul 2>&1
IF "%ERRORLEVEL%" EQU "0" GOTO :FINISH
GOTO START
:FINISH
mode con cols=60 lines=15
RD %DEST% /Q /S
cls
echo        ----------------------------------------------
echo                           CRACKED
echo        ----------------------------------------------
echo.
echo PASSWORD FOUND
echo FILE  = "%full%"
echo CRACKED PASSWORD = %PSWD%
set ENDTIME=!TIME!
call :GetDuration !STARTTIME! !ENDTIME!
exit /b
rem ******************  END MAIN CODE SECTION

:GetDuration
set function_starttime=%1
set function_endtime=%2

rem Change formatting for the start and end times
for /F "tokens=1-4 delims=:.," %%a in ("%function_starttime%") do (
   set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

for /F "tokens=1-4 delims=:.," %%a in ("%function_endtime%") do (
   set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

rem Calculate the elapsed time by subtracting values
set /A elapsed=end-start

rem Format the results for output
set /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
if %hh% lss 10 set hh=0%hh%
if %mm% lss 10 set mm=0%mm%
if %ss% lss 10 set ss=0%ss%
if %cc% lss 10 set cc=0%cc%

set DURATION=%hh%:%mm%:%ss%.%cc%

echo Start Time   : %function_starttime%
echo Finish Time  : %function_endtime%
echo          ---------------
echo Duration : %DURATION%
echo.
pause>nul
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';$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%"


来源:https://stackoverflow.com/questions/38369664/how-to-add-filter-to-a-file-chooser-in-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!