How to specify notepad text file location when opened through the command file

brgdotnet
brgdotnet used Ask the Experts™
on
If I run the commands below the first one opens a notepad text file in a maximized notepad window. The second one opens the same file but the notepad window is minimized.
So I am wondering two things.

1. Is there a way to open the notepad window so that it is half or fractionally open? Like 1/2 the normal size.
2. Is there a way to specify the location of the notepad window when it initially opens? Like left of center?
    I ask this because I will open a two notepad windows and do not want them to all be in the same location/coords. I would prefer
    that the last window open is displayed slightly to the right of the first notepad text file displayed.



1. C:\cmd.exe /c start /max notepad.exe c:\mySampleFile.txt

1. C:\cmd.exe /c start /min notepad.exe c:\mySampleFile.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
There's no "easy way" to do this from a command prompt, none of the tools to launch a windowed program include the ability to set location and size at the same time.

There are a number of ways to try and work around this, using either Powershell and it's window positioning / sizing capability, or utils like CMDOW and Auto Hot KEY (AHK).

If you want more info on those let me know which, but wasn't sure if that would be acceptable.  The fact that you're loading multiple notepad windows makes it a little trickier too I suspect, but as long as the windows have different titles that you can count on then it might work out.

EDIT: A quick search turned up these discussions, might help explain what I was referring to a bit...


»bp
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018

Commented:
Following up on Bill's comment, if you're interested in an AutoHotkey solution, I can probably help you with such a script. Regards, Joe
Darrell PorterEnterprise Business Process Architect

Commented:
There is a Powershell method of doing this - would that be acceptable?
Function Set-Window {
<#
.SYNOPSIS
Retrieve/Set the window size and coordinates of a process window.

.DESCRIPTION
Retrieve/Set the size (height,width) and coordinates (x,y) 
of a process window.

.PARAMETER ProcessName
Name of the process to determine the window characteristics. 
(All processes if omitted).

.PARAMETER Id
Id of the process to determine the window characteristics. 

.PARAMETER X
Set the position of the window in pixels from the left.

.PARAMETER Y
Set the position of the window in pixels from the top.

.PARAMETER Width
Set the width of the window.

.PARAMETER Height
Set the height of the window.

.PARAMETER Passthru
Returns the output object of the window.

.NOTES
Name:   Set-Window

.OUTPUTS
None
System.Management.Automation.PSCustomObject
System.Object

.EXAMPLE
Get-Process powershell | Set-Window -X 20 -Y 40 -Passthru -Verbose
VERBOSE: powershell (Id=11292, Handle=132410)

Id          : 11292
ProcessName : powershell
Size        : 1134,781
TopLeft     : 20,40
BottomRight : 1154,821

Description: Set the coordinates on the window for the process PowerShell.exe

.EXAMPLE
$windowArray = Set-Window -Passthru
WARNING: cmd (1096) is minimized! Coordinates will not be accurate.

    PS C:\>$windowArray | Format-Table -AutoSize

  Id ProcessName    Size     TopLeft       BottomRight  
  -- -----------    ----     -------       -----------  
1096 cmd            199,34   -32000,-32000 -31801,-31966
4088 explorer       1280,50  0,974         1280,1024    
6880 powershell     1280,974 0,0           1280,974     

Description: Get the coordinates of all visible windows and save them into the
             $windowArray variable. Then, display them in a table view.

.EXAMPLE
Set-Window -Id $PID -Passthru | Format-Table
​‌‍
  Id ProcessName Size     TopLeft BottomRight
  -- ----------- ----     ------- -----------
7840 pwsh        1024,638 0,0     1024,638

Description: Display the coordinates of the window for the current 
             PowerShell session in a table view.



#>
[cmdletbinding(DefaultParameterSetName='Name')]
Param (
    [parameter(Mandatory=$False,
        ValueFromPipelineByPropertyName=$True, ParameterSetName='Name')]
    [string]$ProcessName='*',
    [parameter(Mandatory=$True,
        ValueFromPipeline=$False, ParameterSetName='Id')]
    [int]$Id,
    [int]$X,
    [int]$Y,
    [int]$Width,
    [int]$Height,
    [switch]$Passthru
)
Begin {
    Try { 
        [void][Window]
    } Catch {
    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);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public extern static bool MoveWindow( 
            IntPtr handle, int x, int y, int width, int height, bool redraw);

        [DllImport("user32.dll")] 
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool ShowWindow(
            IntPtr handle, int state);
        }
        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
        }
"@
    }
}
Process {
    $Rectangle = New-Object RECT
    If ( $PSBoundParameters.ContainsKey('Id') ) {
        $Processes = Get-Process -Id $Id -ErrorAction SilentlyContinue
    } else {
        $Processes = Get-Process -Name "$ProcessName" -ErrorAction SilentlyContinue
    }
    if ( $null -eq $Processes ) {
        If ( $PSBoundParameters['Passthru'] ) {
            Write-Warning 'No process match criteria specified'
        }
    } else {
        $Processes | ForEach-Object {
            $Handle = $_.MainWindowHandle
            Write-Verbose "$($_.ProcessName) `(Id=$($_.Id), Handle=$Handle`)"
            if ( $Handle -eq [System.IntPtr]::Zero ) { return }
            $Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
            If (-NOT $PSBoundParameters.ContainsKey('X')) {
                $X = $Rectangle.Left            
            }
            If (-NOT $PSBoundParameters.ContainsKey('Y')) {
                $Y = $Rectangle.Top
            }
            If (-NOT $PSBoundParameters.ContainsKey('Width')) {
                $Width = $Rectangle.Right - $Rectangle.Left
            }
            If (-NOT $PSBoundParameters.ContainsKey('Height')) {
                $Height = $Rectangle.Bottom - $Rectangle.Top
            }
            If ( $Return ) {
                $Return = [Window]::MoveWindow($Handle, $x, $y, $Width, $Height,$True)
            }
            If ( $PSBoundParameters['Passthru'] ) {
                $Rectangle = New-Object RECT
                $Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
                If ( $Return ) {
                    $Height      = $Rectangle.Bottom - $Rectangle.Top
                    $Width       = $Rectangle.Right  - $Rectangle.Left
                    $Size        = New-Object System.Management.Automation.Host.Size        -ArgumentList $Width, $Height
                    $TopLeft     = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Left , $Rectangle.Top
                    $BottomRight = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Right, $Rectangle.Bottom
                    If ($Rectangle.Top    -lt 0 -AND 
                        $Rectangle.Bottom -lt 0 -AND
                        $Rectangle.Left   -lt 0 -AND
                        $Rectangle.Right  -lt 0) {
                        Write-Warning "$($_.ProcessName) `($($_.Id)`) is minimized! Coordinates will not be accurate."
                    }
                    $Object = [PSCustomObject]@{
                        Id          = $_.Id
                        ProcessName = $_.ProcessName
                        Size        = $Size
                        TopLeft     = $TopLeft
                        BottomRight = $BottomRight
                    }
                    $Object
                }
            }
        }
    }
}
}

Open in new window


This essentially embeds the C# source code for GetWindow, MoveWindow, and ShowWindow into a usable form within Powershell without the need for compiling an EXE to get the same effect.
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Notepad stores its location in the registry and starts a new instance with the sizes saved there, so here's a batch solution to start Notepad with a customized size and position:
@echo off
setlocal
set /a OffsetX = 40
set /a OffsetY = 0

set /a n1.PosX = 20
set /a n1.PosY = 20
set /a n1.SizeX = 500
set /a n1.SizeY = 500

set /a n2.PosX = n1.PosX + OffsetX
set /a n2.PosY = n1.PosY + OffsetY
set /a n2.SizeX = 500
set /a n2.SizeY = 500

call :StartNotepad %n1.PosX% %n1.PosY% %n1.SizeX% %n1.SizeY% "First.txt"

call :StartNotepad %n2.PosX% %n2.PosY% %n2.SizeX% %n2.SizeY% "Second.txt"

REM ================================================================================
REM No changes below this line
REM ================================================================================
REM Restore the original position
call :StartNotepad %iWindowPosX% %iWindowPosY% %iWindowPosDX% %iWindowPosDY%
goto :eof

:StartNotepad PosX, PosY, SizeX, SizeY, Path
REM Backup the original position
if not defined iWindowPosX for /f "tokens=1-3" %%a in ('reg.exe query HKEY_CURRENT_USER\Software\Microsoft\Notepad ^| find /i "REG_DWORD"') do set /a %%a = 1 * %%c

reg.exe add HKEY_CURRENT_USER\Software\Microsoft\Notepad /t REG_DWORD /v iWindowPosX /d %~1 /f >NUL
reg.exe add HKEY_CURRENT_USER\Software\Microsoft\Notepad /t REG_DWORD /v iWindowPosY /d %~2 /f >NUL
reg.exe add HKEY_CURRENT_USER\Software\Microsoft\Notepad /t REG_DWORD /v iWindowPosDX /d %~3 /f >NUL
reg.exe add HKEY_CURRENT_USER\Software\Microsoft\Notepad /t REG_DWORD /v iWindowPosDY /d %~4 /f >NUL
if not "%~5"=="" (
	start "" notepad.exe "%~5"
)
goto :eof

Open in new window

brgdotnetcontractor

Author

Commented:
Thanks Experts. It sounds like PowerShell is the way to go. I won't use that solution but thank you all kindly for the help.
Bill PrewTest your restores, not your backups...
Top Expert 2016
@brgdotnet,

Are you all set with this now, or do you need more help?  If all set, could you please close it out now.  If you need help with the question close process take a look at:



»bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial