javascript - 如何将过滤器批量添加到文件选择器?

标签 javascript powershell batch-file cmd hta

我正在使用此代码使用批处理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/

相关文章:

javascript - 围绕 GridX 树使用 dojo 方面时,对 "this"的引用丢失

powershell - Enter-PSSession 导致堆栈溢出异常

powershell - Powershell和BizTalk

windows - xcopy 可以传输子目录中的文件,但不能传输目录本身吗?

windows - 如何使用 BAT 文件终止所有以我的用户帐户启动的进程?

javascript - 如何将字符串添加到变量

javascript - HighCharts 将总计堆叠在顶部以进行百分比堆叠

javascript - React Native 和 firebase,注册时的实时数据库

c# - Powershell - 新对象 : Index was outside the bounds of the array

windows - 用于删除早于 N 天的文件的批处理文件