How can a batch file run a program and set the position and size of the window?

Sean Mickey picture Sean Mickey · May 1, 2012 · Viewed 97.7k times · Source

I have batch file that sets up the desktop environment for me when I am writing code. The file is named: SetEnv.cmd and it opens 3 other windows:

  1. An instance of Windows Explorer that is set to the app server's deploy directory.
  2. A second instance of Windows Explorer that is set to the directory where my deployment file is written.
  3. A console window to start my application server.

Here are the contents of SetEnv.cmd:

Explorer /n,c:\develop\jboss-4.2.3.GA\server\default\deploy
Explorer /n,c:\develop\Project\Mapping\deploy
cmd /c SetupEnvCmd.cmd

And here are the contents of SetupEnvCmd.cmd:

cd C:\develop\jboss-4.2.3.GA\bin
run

Every time I run this, I have to waste time rearranging and resizing the windows. I don't want to run the windows minimized, because I interact with each window many times while writing and testing code. Is there any way I can control the position and/or size of the windows that are opened from within the script?

Answer

Skip R picture Skip R · Jun 21, 2012

Here is an alternate way with nircmd util from http://www.nirsoft.net/utils/nircmd.html
Examples:

nircmd win move ititle "cmd.exe" 5 5 10 10
nircmd win setsize ititle "cmd.exe" 30 30 100 200
nircmd cmdwait 1000 win setsize ititle "cmd.exe" 30 30 1000 600

Here are the contents of SetEnv.cmd:

    Explorer /n,c:\develop\jboss-4.2.3.GA\server\default\deploy
    nircmd wait 1000 win setsize ititle "something" x, y, width, height
    Explorer /n,c:\develop\Project\Mapping\deploy
    nircmd wait 1000 win setsize ititle "something" x, y, width, height

Where x,y top left corner location and width,height are the window size "something" is window title usually the folder name eg. "c:\develop\jboss-4.2.3.GA\server\default\deploy" The "wait" may need to be adjusted to give the time for the application window to initialize. So you would increase time if you have virus scanners that delay. Not so much a problem with explorer.exe or cmd.exe but something like Firefox's or java applications can vary from few seconds to several dozen seconds depending on the speed your hardware and OS tuning. You can also customize cmd.exe window or the "run" application by adding lines to "SetupEnvCmd.cmd assuming "run" is asynchronous win32 application otherwise add "start" command.