我想要实现的是启动一些目录(文件夹)但在后台并将批处理控制台保留在屏幕的前面作为活动窗口。 例如,我有一个带有选择列表的批处理,当我按下1)分配的目录在屏幕上打开,批处理控制台仍然在顶部作为活动窗口,所以我可以选择其他操作来启动并继续...批处理控制台需要保持在顶部,所以我可以先打开几个动作,关闭批处理文件并开始处理打开的窗口。
希望很明显:)
选择列表很长,所以下面是一个部分。
@echo off
:List
cls
echo select from list
echo.
echo 1) Postage
echo 2) Documents
echo 3) EXIT
echo.
set /p option=select:
if %option%==1 goto option1
if %option%==2 goto documents
:option1
Start d:\Postage
goto List
pause
:option2
Start d:\documents
goto List
pause
例如我选择1),按回车,文件夹打开,我仍然希望将批处理控制台选为活动状态,因此可以执行相同的操作2) - 通常文件夹1)打开并设置为活动窗口所以我必须通过鼠标单击或alt + tab返回批处理控制台。
THX!
答案 0 :(得分:1)
以下是此任务的注释批处理代码,基于提供的代码在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选项号,这样可以很容易地跳转到匹配数字的选项的代码块。
打开文件夹的代码被放入子程序中,因为很可能需要多次。
也可以将文件夹路径分配给环境变量,跳转到通过环境变量打开文件夹的代码块,调用脚本并使用 GOTO 跳回{{ 1}}。
请参阅KB314853, Windows资源管理器命令行选项与Windows 95以来的所有Windows相同。
当然也可以使用List
代替命令行启动Window Explorer,并使用参数打开指定的文件夹并显示树。打开包含空格或其他字符的文件夹时需要start "" "%~1"
,这需要在文件夹路径周围使用双引号。命令""
将导致打开一个新的命令提示符窗口,其中文件夹路径为窗口标题。因此start "%~1"
用于指定空窗口标题。
要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。
""
call /?
cls /?
echo /?
endlocal /?
exit /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
Wes Larson提出了另一种方法:
title /?
但这只能部分用于打开文件夹,因为文件夹窗口是最小化打开的,但仍然是活动窗口。因此,批处理用户必须单击仍处于顶部可见批处理命令处理窗口,因为它不再具有输入焦点。
在启动任何其他GUI应用程序时,命令提示符窗口也不再是活动窗口。例如,使用命令行启动Windows资源管理器,即使使用
也会导致输入焦点丢失@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
但是如果批处理文件只是用于打开文件夹而且批处理用户在输入数字后点击批处理窗口是可以的,那么第二种解决方案肯定更好,因为它比第一种更容易。