How to open a CMD Window at a specified location on the Desktop?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

How to open a CMD Window at a specified location on the Desktop?

#1 Post by PaperTronics » 12 Aug 2017 02:52

Hey Guys,

This time, my problem is nearly impossible to solve (at least for me :mrgreen:), I need open a Command Prompt window at a specified location on the desktop without using any plugin. Kinda like Batbox /o which has the ability to open the window at the top right corner, top left corner etc. So is there a way to do the same kind of thing using no plugins?


Any help is greatly appreciated,
PaperTronics

aGerman
Expert
Posts: 3667
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to open a CMD Window at a specified location on the Desktop?

#2 Post by aGerman » 12 Aug 2017 06:26

The only possibility without 3rd party tools is via registry (which is an ugly solution).

If value "WindowPosition" is not available the position will be set automatically. If the value is available then the 16 high bits are for the top position and the 16 low bits are for the left position.

So you can try to read the old position, set the new value, open a new cmd prompt, and immediately reset the value.

Code: Select all

set /a "left=100, top=50"

set "oldpos="
for /f "tokens=3" %%i in ('2^>nul reg query "HKCU\Console" /v "WindowPosition"') do set "oldpos=%%i"
set /a "newpos=(top << 16) | left"
>nul reg add "HKCU\Console" /v "WindowPosition" /t REG_DWORD /d %newpos% /f
start cmd /k
>nul pathping 127.0.0.1 -n -q 1 -p 500
if defined oldpos (>nul reg add "HKCU\Console" /v "WindowPosition" /t REG_DWORD /d %oldpos% /f) else >nul reg delete "HKCU\Console" /v "WindowPosition" /f

Steffen

ShadowThief
Expert
Posts: 901
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to open a CMD Window at a specified location on the Desktop?

#3 Post by ShadowThief » 12 Aug 2017 08:22

It can also be done in PowerShell, which is somewhat less ugly, but far more verbose (although it has the added benefit of not touching the registry at all).

Code: Select all

<# :
:: Based on https://gist.github.com/coldnebo/1148334
:: Converted to a batch/powershell hybrid via http://www.dostips.com/forum/viewtopic.php?p=37780#p37780
@echo off
setlocal
set "POWERSHELL_BAT_ARGS=%*"
if defined POWERSHELL_BAT_ARGS set "POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%"
endlocal & powershell -NoLogo -NoProfile -Command "$_ = $input; Invoke-Expression $( '$input = $_; $_ = \"\"; $args = @( &{ $args } %POWERSHELL_BAT_ARGS% );' + [String]::Join( [char]10, $( Get-Content \"%~f0\" ) ) )"
goto :EOF
#>

Add-Type @"
  using System;
  using System.Runtime.InteropServices;
 
  public class Win32 {
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  }
 
  public struct RECT
  {
    public int Left;        // x position of upper-left corner
    public int Top;         // y position of upper-left corner
    public int Right;       // x position of lower-right corner
    public int Bottom;      // y position of lower-right corner
  }
"@

# Start the desired application
$application_path = "C:\Windows\system32\cmd.exe"
Start-Process $application_path

# You wouldn't ordinarily need the [1], but running the script inherently opens a window,
# and you want to do things with the window that that window opens.
$h = (Get-Process | where {$_.Path -eq $application_path})[1].MainWindowHandle
$rcWindow = New-Object RECT

[Win32]::GetWindowRect($h,[ref]$rcWindow)

$win_width = 1280
$win_height = 720
$screen_x=0
$screen_y=0

[Win32]::MoveWindow($h, $screen_x, $screen_y, $win_width, $win_height, $true )


You may want to tweak the $win_width, $win_height, $screen_x, and $screen_y settings to your liking.

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to open a CMD Window at a specified location on the Desktop?

#4 Post by PaperTronics » 12 Aug 2017 22:38

@ShadowThief - The powershell method is extremely slow compared to aGerman's registry method. So I can't use your method.

@aGerman - As I mentioned above, your method is prompt (went for a pun there!) and opens the Cmd Windows in almost no time at all!


Thanks for your help,
PaperTronics

ShadowThief
Expert
Posts: 901
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to open a CMD Window at a specified location on the Desktop?

#5 Post by ShadowThief » 12 Aug 2017 23:16

Weird, it opens and move for me instantly.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: How to open a CMD Window at a specified location on the Desktop?

#6 Post by thefeduke » 13 Aug 2017 15:10

ShadowThief wrote:

Code: Select all

# You wouldn't ordinarily need the [1], but running the script inherently opens a window,
# and you want to do things with the window that that window opens.
$h = (Get-Process | where {$_.Path -eq $application_path})[1].MainWindowHandle
Unfortunately, I tried, and a normal window opened, but I got this and two follow-on errors in the originating window:

Code: Select all

Cannot index into a null array.
At line:46 char:60
+ $h = (Get-Process | where {$_.Path -eq $application_path})[ <<<< 1].MainWindowHandle
    + CategoryInfo          : InvalidOperation: (1:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

John A.

ShadowThief
Expert
Posts: 901
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to open a CMD Window at a specified location on the Desktop?

#7 Post by ShadowThief » 13 Aug 2017 15:34

Try changing the [1] to [0].

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to open a CMD Window at a specified location on the Desktop?

#8 Post by PaperTronics » 13 Aug 2017 23:04

ShadowThief wrote:Weird, it opens and move for me instantly.


If the problem is occurring due to an extension fault then:
I'm currently saving the code as a .bat file, do I need to save it as a .ps1 file?


PaperTronics

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: How to open a CMD Window at a specified location on the Desktop?

#9 Post by thefeduke » 14 Aug 2017 01:14

ShadowThief wrote:Try changing the [1] to [0].
I did not want to leave this hanging. Eventually, I refreshed my copy of your code.
After a phase of sometimes getting a display of false,false and true,true with unpredictable results, it seems to be behaving and even worked from the started prompt.

John A.

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to open a CMD Window at a specified location on the Desktop?

#10 Post by PaperTronics » 14 Aug 2017 06:10

@thefeduke - That's exactly what's happening with me right now, the window opens, says False,False and sometimes closes without the expected results, and the other times it just closes without saying anything. Can you please describe how you fixed it?


PaperTronics

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: How to open a CMD Window at a specified location on the Desktop?

#11 Post by thefeduke » 14 Aug 2017 13:05

PaperTronics wrote:Can you please describe how you fixed it? PaperTronics

I cannot say that I "fixed" anything. I am only watching behavior, now. It appears similar whether I start the script by START command, by double-click in File Explorer, by a desktop shortcut or by storing the script file on the Desktop and opening it from there. How have you tried it and how do you actually want it to be used?

I was mostly using Win7Pro. What is your software, please?

Here is what one shortcut to a DOS Prompt went:

Code: Select all

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Zyltch>pushd ~scripts~

C:\Users\Zyltch\~Scripts~>CMDlayoutPShbd
True
True

C:\Users\Zyltch\~Scripts~>echo %cmdcmdline%
C:\Windows\system32\cmd.exe

C:\Users\Zyltch\~Scripts~>echo.Shortcut target: %windir%\system32\cmd.exe /E /K %comspec%
Shortcut target: C:\Windows\system32\cmd.exe /E /K C:\Windows\system32\cmd.exe

C:\Users\Zyltch\~Scripts~>echo.Shortcut Start in: %HOMEDRIVE%%HOMEPATH%
Shortcut Start in: C:\Users\Zyltch
So that is a manual direct approach for testing.

The success of the positioning seems more to do with history and luck. Starting with no DOS prompts active and the script file stored as a Desktop file that I opened repeatedly, active windows grew and here is what happened.

Code: Select all

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Zyltch\Desktop>rem 1st open window at default coordinates

C:\Users\Zyltch\Desktop>rem 2nd run moved this window

C:\Users\Zyltch\Desktop>rem 3rd run hard to tell - now moving window by dragging

C:\Users\Zyltch\Desktop>rem 4th run moved this window

C:\Users\Zyltch\Desktop>rem changing $Screen_y value in script

C:\Users\Zyltch\Desktop>rem changing $Screen_x value in script

C:\Users\Zyltch\Desktop>rem 5th run moved this window to updated location
So that is quite consistent, except for the first run. Closed all DOS windows and repeated with results not appearing repeatable. This would not appear to be a problem if the first run always worked.

That's it for now. I have a nomenclature problem. By plugin, do you mean a non-Windows executable file? I ask because, I noticed that you have used this term to describe a useful DOS script file, as well.

John A.

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to open a CMD Window at a specified location on the Desktop?

#12 Post by PaperTronics » 15 Aug 2017 06:02

thefeduke wrote:It appears similar whether I start the script by START command, by double-click in File Explorer, by a desktop shortcut or by storing the script file on the Desktop and opening it from there. How have you tried it and how do you actually want it to be used?


I want to start the .bat file using the START command in another batch program. I've tried opening it using the start command as well as using the file explorer but still, I didn't get the desired results. Guess I'm gonna have to use the registry method by aGerman.


thefeduke wrote:I was mostly using Win7Pro. What is your software, please?


I'm using the script on Windows 7 Ultimate, although I want it to be compatible with all Windows platforms


thefeduke wrote:By plugin, do you mean a non-Windows executable file? I ask because, I noticed that you have used this term to describe a useful DOS script file, as well.


When I say plugin, I mean any file that has an extension different than .bat

I may have used it to describe a batch file by mistake.


PaperTronics

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: How to open a CMD Window at a specified location on the Desktop?

#13 Post by thefeduke » 16 Aug 2017 09:47

Thanks for the replies.
PaperTronics wrote:When I say plugin, I mean any file that has an extension different than .bat
Let me qualify that with: that are not native to Windows.

I have been working successfully on your behalf in another topic. Please see post by elzooilogico (Thank you) at "commands to change CmdPrompt buffer size?" http://www.dostips.com/forum/viewtopic.php?p=53399#p53399 This involves creating a temporary .exe file which you may reuse or recreate each run. It seems to do everything for me.

John A

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to open a CMD Window at a specified location on the Desktop?

#14 Post by PaperTronics » 18 Aug 2017 03:20

thefeduke wrote:This involves creating a temporary .exe file which you may reuse or recreate each run.



Even creating a temporary .exe file isn't okay with me cuz' there is some other programming language being used. I think the registry method will be my choice, at the end of this topic.


PaperTronics

elzooilogico
Posts: 112
Joined: 23 May 2016 15:39
Location: Spain

Re: How to open a CMD Window at a specified location on the Desktop?

#15 Post by elzooilogico » 18 Aug 2017 04:08

Does this help? Positioning CMD Window

Post Reply