Positioning CMD Window
Posted: 09 Feb 2013 20:26
In order to Position the cmd window to a specific location, you must change the registry setting, then when you are done, you need then to reset the registry settings.
Another way, is by using this vbscript i found on the Internet.
as you can see objConfig.X and objConfig.Y where you set the X & Y Coordinates, and the location of the batch File in intReturn
By this you can position any batch file window by setting it's X and Y Coordinates,
But This will require Two Files, Batch File and VBscript,
you can make it in one file by creating two functions "labels" one to create the VBscript and one to create the Batch File but then you will have to escape every (, ), > , & characters in your code and if it a big code that will be a waste of time, so to avoid that here is a work around:
> First, you make a start code or "header", this 3 lines any batch you want to set it's coordinate must start with,
> Then, in the code section put your batch file codes, and don't forget the label ":Code"
> Create a function that create only the vbscript, which you call it in the 2nd command in our "header"
AND Make Sure BEFORE your batch file end, YOU MUST DELETE the vbscript. so add a delete command to your code to delete the vbscript you created in the "header"
This is a Sample, will set the position to 350 and 350 (X,Y) Coordinates.
The Pos label here, i made it take 2 arguments, X and Y Coordinates, you can put the coordinates directly in the vbscript.
Edited
There is a fix for that,
You can delete the VBscript at the beginning of your batch code "in code section",
So even if your batch didn't work from the beginning and crashed, the first thing to do is to delete the vbscript,
I think this should solve it.
The Code section should be something like that :
Another way, is by using this vbscript i found on the Internet.
Code: Select all
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objConfig = objWMIService.Get("Win32_ProcessStartup")
objConfig.SpawnInstance_
objConfig.X = 350
objConfig.Y = 350
Set objNewProcess = objWMIService.Get("Win32_Process")
intReturn = objNewProcess.Create("C:\Documents and Settings\admin\desktop\Fixed_location.bat", Null, objConfig, intProcessID)
By this you can position any batch file window by setting it's X and Y Coordinates,
But This will require Two Files, Batch File and VBscript,
you can make it in one file by creating two functions "labels" one to create the VBscript and one to create the Batch File but then you will have to escape every (, ), > , & characters in your code and if it a big code that will be a waste of time, so to avoid that here is a work around:
> First, you make a start code or "header", this 3 lines any batch you want to set it's coordinate must start with,
> Then, in the code section put your batch file codes, and don't forget the label ":Code"
> Create a function that create only the vbscript, which you call it in the 2nd command in our "header"
AND Make Sure BEFORE your batch file end, YOU MUST DELETE the vbscript. so add a delete command to your code to delete the vbscript you created in the "header"
This is a Sample, will set the position to 350 and 350 (X,Y) Coordinates.
Code: Select all
@Echo OFF
REM ===== Header ====================================
REM == MUST BE AT The Begining of The Batch =========
IF EXIST "%temp%\pos.vbs" Goto :Code
CALL :Pos "350" "350" %= [350 350] is X, Y Coordinate =%
EXIT /B
REM ===== Code ======================================
REM == Put All Your Code Down Here ==================
:Code
Echo This is a Test Batch File in 350, 350 Coordinates on the screen
Pause
REM == MUST Delete The Vbscript before Your Batch Ends
DEL /Q "%temp%\Pos.vbs"
Exit /B
REM ===== Position Script============================
:Pos <X_Coordinate> <Y_Coordinate>
REM This Function will take two inputs X and Y Coordinates to Position the CMD window
REM %~dpnx0 in the script will get this batch full path and add it
REM to the vbscript, so the vbscript run the batch in position.
(
Echo Set objWMIService = GetObject^("winmgmts:\\.\root\cimv2"^)
Echo Set objConfig = objWMIService.Get^("Win32_ProcessStartup"^)
Echo objConfig.SpawnInstance_
Echo objConfig.X = %~1
Echo objConfig.Y = %~2
Echo Set objNewProcess = objWMIService.Get^("Win32_Process"^)
Echo intReturn = objNewProcess.Create^("%~dpnx0", Null, objConfig, intProcessID^)
)>"%temp%\pos.vbs"
Start "" "%temp%\pos.vbs"
Goto :EOF
Edited
Final Note:
This has a problem, because if your batch file was interrupted or crashed and didn't exit in the right way, the vbscript will not be deleted and then when you run the batch again it won't be positioned in the right coordinates you have set.
There is a fix for that,
You can delete the VBscript at the beginning of your batch code "in code section",
So even if your batch didn't work from the beginning and crashed, the first thing to do is to delete the vbscript,
I think this should solve it.
The Code section should be something like that :
REM ===== Code ======================================
REM == Put All Your Code Down Here ==================
:Code
REM == MUST Delete The Vbscript First
DEL /Q "%temp%\Pos.vbs"
Echo This is a Test Batch File in 350, 350 Coordinates on the screen
Pause
Exit /B
REM ===== Position Script============================
.
.
.