将批处理控制台保持在屏幕上作为活动窗口 [英] Keeping batch console on the screen as active window

查看:92
本文介绍了将批处理控制台保持在屏幕上作为活动窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现的是在后台启动一些目录(文件夹),并将批处理控制台作为活动窗口保留在屏幕的前面. 例如,我有一个带有选择列表的批处理,当我按1)分配的目录在屏幕上打开,并且批处理控制台保留在顶部作为活动窗口,因此我可以选择其他操作来启动并继续...批处理控制台需要保留在顶部,因此我可以首先打开多个操作,关闭批处理文件并开始在打开的窗口上工作.

what I want to achieve is launch some directories(folders) but in the background and keep batch console on the front of screen as active windows. For example I have a batch with selection list,when I press 1) assigned directory opens on the screen and batch console remains on the top as active window so I can select other action to launch and go on...batch console needs to stay on the top so I can open several actions firstly, close batch file and start working on open windows.

希望很清楚:)

选择列表很长,因此下面是其中的一部分.

Selection list is very long so below is a part.

@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),按Enter,文件夹正在打开,但我仍然希望将Batch Console选为活动状态,因此可以对2)进行相同的操作-通常打开文件夹1)并将其设置为活动窗口,所以我必须通过单击鼠标或alt + tab返回批处理控制台.

For example I am selecting 1), push enter, folder is opening and I still want have Batch console selected as active so can do the same operation with 2) - normally folder 1) opens and is set as active window so I have to return to batch console by mouse click or alt+tab.

谢谢!

推荐答案

以下是此任务的批处理批处理代码,基于提供的代码,该代码在Windows XP SP3 x86上工作正常,但在Windows 10 x64上不工作:

Here is a commented batch code for this task based on provided code working as expected on Windows XP SP3 x86, but not on 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中进行了稍微修改.文件放在所有窗口的顶部.谢谢 npocmaka .

The most difficult code part - getting command prompt window after opening the folder back to top of all windows - was copied slightly modified from Bat file on top of all windows. Thanks npocmaka.

但是此方法在Windows 10 x64上不起作用.批处理命令窗口的选项卡仅在Windows任务栏上闪烁,指示该窗口需要用户注意,但不会再次完全变为活动窗口.因此,批处理用户必须单击批处理窗口的选项卡或批处理窗口本身,才能再次将其变为活动窗口.

But this method does not work on Windows 10 x64. The tab of the batch command window just flashes on Windows task bar to indicate that this window wants the user's attention, but does not become completely the active window again. So the batch user must click on the tab of the batch window or the batch window itself to make it again the active window.

但是,我添加了代码来验证

However, I added code to verify that

  1. 批处理用户根本没有输入任何内容,
  2. 批处理用户输入的字符串实际上是一个正数,
  3. 删除前导零,并验证输入的数字是否不为零,
  4. 如果输入的数字不大于EXIT选项的数字,则进行验证.

如果小于EXIT选项编号,则可以轻松跳转到与该编号匹配的选项的代码块.

This makes it simple to jump to the code block of the option matching the number if being less than the EXIT option number.

打开文件夹的代码被多次放入子例程中.

The code to open a folder is put into a subroutine as most likely needed several times.

还可以将文件夹路径分配给环境变量,通过环境变量跳转到打开该文件夹的代码块,调用脚本并以 GOTO 跳转回.

It would be also possible to assign the folder path to an environment variable, jump to the code block opening the folder via the environment variable, calling the script and jump with a GOTO back to List.

有关 Windows资源管理器命令行选项,请参见 KB314853 从Windows 95开始对于所有Windows都是相同的.

See KB314853 for the Windows Explorer Command-Line Options being the same for all Windows since Windows 95.

当然,也可以使用start "" "%~1"而不是命令行启动带有参数的Windows资源管理器,以打开指定的文件夹并显示树.在打开包含空格或其他字符的文件夹时,需要使用"",而这需要在文件夹路径周围使用双引号.命令start "%~1"将导致打开一个新的命令提示符窗口,并将文件夹路径作为窗口标题.因此,""用于指定一个空的窗口标题.

Of course it is also possible to use start "" "%~1" instead of the command line starting Window Explorer with the parameters to open the specified folder and showing also the tree. "" is necessary on opening a folder containing a space or another character which requires the usage of double quotes around folder path. The command start "%~1" would result in opening a new command prompt window with the folder path as window title. Therefore "" is used to specify an empty window title.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • cls /?
  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • title /?
  • call /?
  • cls /?
  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • title /?

韦斯·拉森提出了另一种方法:

@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

但这仅部分用于打开文件夹,因为打开的文件夹窗口已最小化,但仍是活动窗口.因此,批处理用户必须仍在顶部可见的批处理命令处理窗口上单击,因为它不再具有输入焦点.

But this works only partly for opening folders because the folder window is opened minimized, but is nevertheless the active window. So the batch user must click on still at top visible batch command processing window as it does not have the input focus anymore.

在启动任何其他GUI应用程序时,命令提示符窗口也不再是活动窗口.例如,使用命令行启动Windows资源管理器也会导致失去输入焦点,即使使用

And on starting any other GUI application, the command prompt window is also not the active window anymore. For example using the command line starting Windows Explorer results also in losing input focus even on using

start "" /min %SystemRoot%\explorer.exe /e,"%~1"

但是,如果批处理文件仅用于打开文件夹,并且可以让批处理用户在输入数字后单击批处理窗口,那么第二种解决方案肯定会更好,因为比第一种解决方案容易.

But if the batch file is just for opening folders and it is okay that batch user clicks on batch window after having entered a number, this second solution is definitely better because easier than the first one.

这篇关于将批处理控制台保持在屏幕上作为活动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆