Positioning CMD Window

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Positioning CMD Window

#16 Post by abc0502 » 14 Feb 2013 05:12

:lol: I wouldn't even notice the error if i wasn't using a keyboard shortcut keys to repeatedly run the file to see the error :)

also small modification: instead of " RestartedByVBS" i changed it to " " & "RestartedByVBS" in order not to confuse any one else as the space is needed to separate the file name and the word that will be used to identify if the file was running from vbscript or not.

Code: Select all

VBS: intReturn = objNewProcess.Create( chr(34) & WScript.Arguments(0) & chr(34) & " " & "RestartedByVBS", Null, objConfig, intProcessID)


Thanks Foxidrive for your help :)

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Positioning CMD Window

#17 Post by miskox » 21 May 2014 14:43

I modified the code to this:

Code: Select all

@Echo OFF

REM ===== Header ====================================
REM == MUST BE AT The Begining of The Batch =========
   IF "%~1" == "RestartedByVBS" Goto :Code

      (FOR /F "tokens=1*" %%a in ('findstr /B "^VBS:" ^< "%~F0"') do (
         echo(%%b
      )) > "%~DP0pos.vbs"
   Cscript //nologo "%~DP0pos.vbs" "%~F0" 350 350&del "%~DP0pos.vbs" 2>nul
   EXIT



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

Exit /B


REM ===== Position Script============================

:Pos <BatchFileName> <X_Coordinate> <Y_Coordinate>

REM This Function will take three inputs: the name of the Batch file to execute
REM and the X and Y Coordinates to Position its CMD window

VBS: Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
VBS: Set objConfig = objWMIService.Get("Win32_ProcessStartup")
VBS: objConfig.SpawnInstance_
VBS: objConfig.X = WScript.Arguments(1)
VBS: objConfig.Y = WScript.Arguments(2)
VBS: Set objNewProcess = objWMIService.Get("Win32_Process")
VBS: intReturn = objNewProcess.Create( chr(34) & WScript.Arguments(0) & chr(34) & " " & "RestartedByVBS", Null, objConfig, intProcessID)



REM ===== End of VBScript============================




Changes:
- makes .vbs every time
- now it closes the original cmd window

Next question: what should be added to this line

Code: Select all

VBS: intReturn = objNewProcess.Create( chr(34) & WScript.Arguments(0) & chr(34) & " " & "RestartedByVBS", Null, objConfig, intProcessID)


so all the original arguments are preserved and passed back to the .cmd repositioned window:

Code: Select all

c:\>@position.cmd par1 par2 par3


so when new cmd window opens %1 is RestartedByVBS, all other parameters are lost.

Modify the :code section to display parameters:

Code: Select all

Echo This is a Test Batch File in 350, 350 Coordinates on the screen
echo par1=%1
echo par2=%2
echo par3=%3


Saso

Post Reply