On Batch script, How to get X and Y from Start to End of the Window without using third party software like Cmdow
Asked
Active
Viewed 494 times
-2
-
Possible duplicate: https://stackoverflow.com/questions/7977322/set-the-window-position-of-an-application-via-command-line – Jerry Jeremiah Jan 17 '22 at 04:02
-
Does this do what you want? https://stackoverflow.com/questions/57554007/mpv-get-window-position-and-size-or-window-moved-resized-event It requires powershell but that's built in so it isn't third party. – Jerry Jeremiah Jan 17 '22 at 04:22
-
@JerryJeremiah Yes but how would it be part of one batch script ? – Robot Jan 17 '22 at 04:50
-
If you must have exactly only one file then this talks about embedding the powershell script inside the batch file https://stackoverflow.com/questions/2609985/how-to-run-a-powershell-script-within-a-windows-batch-file Or is it ok to have a separate powershell script file and just call it from the vatch file? – Jerry Jeremiah Jan 17 '22 at 05:11
2 Answers
0
Ok, combining answers from:
- https://superuser.com/questions/74620/is-there-a-way-to-get-access-to-a-window-handle-in-windows-using-wsh-or-wmi-or
- MPV: Get window position and size? Or window-moved/resized event?
- How to run a PowerShell script within a Windows batch file
I came up with this:
<# : batch portion (begins PowerShell multi-line comment block)
set TITLE=Untitled - Notepad
for /f "tokens=1,2,3,4" %%a in ('powershell -NoProfile -NoLogo "iex (${%~f0} | out-string)"') DO (
set LEFT=%%a
set TOP=%%b
set RIGHT=%%c
set BOTTOM=%%d
)
echo LEFT=%LEFT% TOP=%TOP% RIGHT=%RIGHT% BOTTOM=%BOTTOM%
@GOTO :EOF
: end batch / begin PowerShell #>
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Window {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
}
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
"@
$Handle = Get-Process | Where-Object { $_.MainWindowTitle -match $env:TITLE } | ForEach-Object { $_.MainWindowHandle }
if ( $Handle -is [System.Array] ) { $Handle = $Handle[0] }
$WindowRect = New-Object RECT
$GotWindowRect = [Window]::GetWindowRect($Handle, [ref]$WindowRect)
Write-Host $WindowRect.Left $WindowRect.Top $WindowRect.Right $WindowRect.Bottom

Jerry Jeremiah
- 9,045
- 2
- 23
- 32
-
Thank You! I loop over the batch it is slow but they is probably no shorcut to C. `:Loop <#..` to `..%BOTTOM% GOTO Loop` – Robot Jan 17 '22 at 20:09
0
I use the following:
You can set the location by changing the cords in following line cscript //nologo "%temp%\pos.vbs" "%~F0" 75 15
NOTE: In this instance 75x pixels from the left and 15x pixels from the top.
@echo off &cls
mode con: cols=70 lines=15 &color f0
:: - Position the CMD Window Using .VBS -----------------------------------------
:: == MUST be at the beginning of the Batch =====================================
IF "%~1" == "RestartedByVBS" Goto :Code
:: Create the VBScript, if not exist
if not exist "%temp%\pos.vbs" (
(for /F "tokens=1*" %%a in ('findstr "^VBS:" ^< "%~F0"') do (
echo(%%b
)) > "%temp%\pos.vbs"
)
cscript //nologo "%temp%\pos.vbs" "%~F0" 75 15
exit /b
:code
if exist "%temp%\pos.vbs" ( del /q "%temp%\pos.vbs" )
:: ------------------------------------------------------------------------------
echo. The rest of your batch script here.
echo. Press any key to exit &>nul timeout /t -1 &exit /B
:: - Position the CMD Window Using .VBS -----------------------------------------
:Pos <BatchFileName> <X_Coordinate> <Y_Coordinate>
:: This Function will take three inputs: The name of the Batch file to execute
:: 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)
:: ------------------------------------------------------------------------------
Disclaimer: Originally posted in this thread: Positioning CMD Window.

Bri
- 59
- 6