Search code examples
batch-filecmdwindows-explorer

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


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?


Solution

  • Try launching your programs from VBS (Windows Script Host) script via the batch file. If your VBS looks like this:

    'FILENAME: SetEnv.vbs
    Set Shell = WScript.CreateObject("WScript.Shell")
    Shell.Run "Explorer /n,c:\develop\jboss-4.2.3.GA\server\default\deploy", 4, False
    Shell.Run "Explorer /n,c:\develop\Project\Mapping\deploy", 4, False
    

    The 4 means to launch the window in its most recent size/position. False means it won't wait to return before executing the next line of your script. Unfortunately, this doesn't give you full control of your exact window size/positioning, but it should remember last size/positioning.

    More info here: http://www.devguru.com/Technologies/wsh/quickref/wshshell_Run.html

    So your new SetEnv.cmd could be:

    @echo off
    REM note there's a difference between cscript and wscript
    REM wscript is usually the default launcher
    cscript SetEnv.vbs
    cd C:\develop\jboss-4.2.3.GA\bin
    run