Matt
Matt

Reputation: 23

Keeping batch console on the screen as active window

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.

Hope it is clear :)

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

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.

Thx!

Upvotes: 2

Views: 8549

Answers (1)

Mofi
Mofi

Reputation: 49086

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"));

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.

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. the batch user has entered something at all,
  2. the batch user entered a string which is really a positive number,
  3. removed leading zeros and verified if entered number is not zero,
  4. verified if entered number is not greater than the EXIT option number.

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.

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.

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

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 /?

Wes Larson suggested another method:

@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.

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.

Upvotes: 1

Related Questions