11

Is there a command I can put into a Windows XP .bat file to bring the command shell to the front?

mirabilos
  • 5,123
  • 2
  • 46
  • 72
Lauren Samuels
  • 2,419
  • 3
  • 19
  • 20

7 Answers7

12

nircmd will do this, though it involves a little scripting.

nircmd win activate "titleofwindow"

You basically need to know the title of the cmd window you are executing (you can set this via the TITLE command in windows)

thus:

TITLE %SOME_UNIQUE_VALE%
nircmd win activate %SOME_UNIQUE_VALE%

should do the trick.

Note some malware tools make use of the NirCmd executable (it requires no deployment and is very powerful); this may cause you problems.

ShuggyCoUk
  • 36,004
  • 6
  • 77
  • 101
6

Another way to get the cmd prompt window to show in front is by ending file1.bat with a command to call a second file2.bat file, followed by an exit command.

EXAMPLE using file1.bat

....
[your code here]
start C:\file2.bat
exit

This closes file1.bat and opens a second .bat file where you can continue with your code. This second .bat command prompt will open in front of other windows

Rodger
  • 69
  • 1
  • 1
5

I had a similar problem and I had to develop a simple C# console application that brings to front a Window. The windows is selected using the window title pass as argument.

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using  System.Runtime.InteropServices; 

 namespace ConsoleApplication1
 {
    class Program
    {

        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("User32.dll")]
        private static extern bool IsIconic(IntPtr handle);
        [DllImport("User32.dll")]
        private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
        const int SW_RESTORE = 9;
        public static void bringToFront(string title)
        {
            // Get a handle to the Calculator application.
            IntPtr handle = FindWindow(null, title);

            // Verify that Calculator is a running process.
            if (handle == IntPtr.Zero)
            {
                return;
            }
            if (IsIconic(handle))
            {
                ShowWindow(handle, SW_RESTORE);
            }

            Console.WriteLine("Founded ");
            SetForegroundWindow(handle);

        }

        static void Main(string[] args)
        {

            if (args.Length > 0)
                bringToFront(args[0]);
            else
                Console.WriteLine("specify program window title");

        }
    }
}

the code of my batch script is then something similar to

tasklist /FI "IMAGENAME eq program.exe" | find "program.exe" if errorlevel 1 (program.exe) else (BringToFront.exe "Program Window Title")

Panciz
  • 2,183
  • 2
  • 30
  • 54
4

From a batch file, no. If you want to activate a window you have to use SetActiveWindow(). If you don't want to get dirty with windows programming but still want to activate windows and simple stuff like that, I highly recommend checking out Autoit. You could always call this program from your batchfile to have it do the task.

ryeguy
  • 65,519
  • 58
  • 198
  • 260
3

CMDOW is also useful for this and for other DOS programming tasks where a little added functionality is needed. Simple to use and well documented. Mind your anti-virus program, though - CMDOW has the ability to hide windows which your anti-virus program will pick up as a possible virus. Just add it to your exception list. CMDOW is completely portable, is definitely NOT a virus and if you have any concerns about it being used by a third party to hide something, simply tuck it away in some non obvious folder somewhere.

Shadeclan
  • 173
  • 2
  • 14
2

Try with focusOn.bat

call focusOn.bat "My Title"
npocmaka
  • 55,367
  • 18
  • 148
  • 187
-1

Another quick way to switch to a window by name is via Ctrl+Shift+Esc, which opens Task Manager. Then just type the first few letters of the windows title to select the process, then hit Enter.

user1491819
  • 1,790
  • 15
  • 20
  • This is a pretty great tip! Just one problem... it'll open with whichever tab was most recently open. If it was the "Application" tab, great, but if it's another tab, you'll need a more elaborate set of keystrokes... it looks like if you do `Ctrl+Shift+Esc`, that'll launch Task Manager. `Shift+Tab` will select the tab bar. Hitting the left arrow a few times results in the Applications tab eventually being selected (and since it doesn't loop and that's left most, you're good.) `Tab` to get back to the list, type the name of the window, and hit `Enter`. – ArtOfWarfare Feb 17 '17 at 15:51
  • 2
    If someone thinks I'm crazy for thinking that's useful, automating keystrokes is super easy and has very few external library dependencies. Python alone is enough to automate keystrokes. Reading windows coordinates and automating the mouse, on the other hand, is very tricky. So automation solutions that can work with nothing but keyboard input are great. Also, hitting the left arrow several times can be replaced with hitting home once. – ArtOfWarfare Feb 17 '17 at 16:05
  • Press Ctrl+Shift+Esc, wait for 1 second, press Alt+Tab can be an easier option. – Cagin Uludamar Jan 14 '22 at 09:33