以下是此任务的注释批处理代码,基于提供的代码在 Windows XP SP3 x86 上按预期工作,但在 Windows 10 x64 上不正常:
@if (@X)==(@Y) @end /* JScript comment
@echo off
rem A window title is needed so the appActivate function is
rem able to recognize the console window of this batch file.
title Open Folders
setlocal EnableExtensions EnableDelayedExpansion
:List
cls
echo Select from list.
echo.
echo 1) Postage
echo 2) Documents
echo 3) EXIT
echo.
set "Option="
set /p "Option=Enter the number: "
rem Prompt user once more if nothing entered.
if "!Option!" == "" goto List
rem Prompt user once more if entered string is not a positive
rem integer because it contains characters not being a digit.
for /F "delims=0123456789" %%N in ("!Option!") do goto List
rem Trim all leading zeros from number.
set "Number="
for /f "tokens=* delims=0" %%N in ("!Option!") do set "Number=%%N"
rem Prompt user once more if entered string is zero.
if "%Number%" == "" goto List
rem Prompt user once more if entered number is too large.
if %Number% GTR 3 goto List
rem Jump to appropriate option if not being the exit option.
if %Number% LSS 3 goto Opt%Number%
endlocal
exit /B
:Opt1
call :OpenDirectory "D:\Postage"
goto List
:Opt2
call :OpenDirectory "D:\documents"
goto List
rem This is a batch subroutine used for opening a folder with
rem Windows Explorer and getting the command prompt window
rem again back to top of all windows for next user input.
:OpenDirectory
if "%~1" == "" (
%SystemRoot%\explorer.exe
) else (
rem start "" "%~1"
%SystemRoot%\explorer.exe /e,"%~1"
)
rem The jscript part with the appActivate is called now
rem to make the command window again the top most window.
%SystemRoot%\System32\cscript.exe //E:JScript //nologo "%~f0"
rem This GOTO command ends the subroutine and results in continuing
rem batch processing on the line below the line called this routine.
goto :EOF
@if (@X)==(@Y) @end JScript comment */
var sh=new ActiveXObject("WScript.Shell");
WScript.Echo(sh.AppActivate("Open Folders"));
最困难的代码部分 - 打开文件夹后返回到所有窗口顶部后获取命令提示符窗口 - 复制自 Bat file on top of all windows 稍作修改。谢谢npocmaka。
但此方法不适用于 Windows 10 x64。批处理命令窗口的选项卡只是在 Windows 任务栏上闪烁,表示该窗口需要用户注意,但并未再次完全变为活动窗口。所以批处理用户必须单击批处理窗口的选项卡或批处理窗口本身才能使其再次成为活动窗口。
但是,我添加了代码来验证这一点
- 批处理用户输入了一些内容,
- 批处理用户输入了一个真正为正数的字符串,
- 删除前导零并验证输入的数字是否不为零,
- 验证输入的数字是否不大于 EXIT 选项数字。
如果小于 EXIT 选项编号,则可以轻松跳转到与该编号匹配的选项的代码块。
打开文件夹的代码被放入子例程中,因为很可能需要多次。
也可以将文件夹路径分配给环境变量,跳转到通过环境变量打开文件夹的代码块,调用脚本并使用 GOTO 跳转回@987654326 @。
请参阅KB314853,了解自 Windows 95 以来所有 Windows 的 Windows 资源管理器命令行选项。
当然也可以使用start "" "%~1" 代替命令行启动窗口资源管理器的参数来打开指定的文件夹并显示树。 "" 在打开包含空格或其他字符的文件夹时是必需的,这需要在文件夹路径周围使用双引号。命令start "%~1" 将导致打开一个新的命令提示符窗口,其中文件夹路径作为窗口标题。因此"" 用于指定一个空的窗口标题。
要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。
call /?
cls /?
echo /?
endlocal /?
exit /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
title /?
Wes Larson 提出了另一种方法:
@echo off
rem A window title for better identifying what this cmd window is for.
title Open Folders
setlocal EnableExtensions EnableDelayedExpansion
:List
cls
echo Select from list.
echo.
echo 1) Postage
echo 2) Documents
echo 3) EXIT
echo.
set "Option="
set /p "Option=Enter the number: "
rem Prompt user once more if nothing entered.
if "!Option!" == "" goto List
rem Prompt user once more if entered string is not a positive
rem integer because it contains characters not being a digit.
for /F "delims=0123456789" %%N in ("!Option!") do goto List
rem Trim all leading zeros from number.
set "Number="
for /f "tokens=* delims=0" %%N in ("!Option!") do set "Number=%%N"
rem Prompt user once more if entered string is zero.
if "%Number%" == "" goto List
rem Prompt user once more if entered number is too large.
if %Number% GTR 3 goto List
rem Jump to appropriate option if not being the exit option.
if %Number% LSS 3 goto Opt%Number%
endlocal
exit /B
:Opt1
start "" /min "D:\Postage"
goto List
:Opt2
start "" /min "D:\documents"
goto List
但这仅适用于打开文件夹,因为文件夹窗口是最小化打开的,但仍然是活动窗口。所以批处理用户必须点击仍然在顶部可见的批处理命令处理窗口,因为它不再具有输入焦点。
在启动任何其他 GUI 应用程序时,命令提示符窗口也不再是活动窗口。例如,使用命令行启动 Windows 资源管理器也会导致失去输入焦点,即使使用
start "" /min %SystemRoot%\explorer.exe /e,"%~1"
但是如果批处理文件只是用于打开文件夹,并且批处理用户在输入数字后单击批处理窗口是可以的,那么第二个解决方案肯定比第一个解决方案更好。