いくつかの小さなヘルパープログラムと一緒にバッチファイルを実行することに満足している場合、完全なソリューションがここに投稿されます:
バッチファイルでプログラムを実行し、ウィンドウの位置とサイズを設定する方法
以下の内容。。。。
このデモバッチファイルは、コマンドラインプログラムを実行し、コマンドプロンプトコンソールウィンドウの位置とサイズを設定します。このバッチの一般的なテーマは前の2つのソリューション(上記のリンクの[スタックオーバーフロー]ページ)と同じですが、この作業を行うためのいくつかの固有の問題と回避策がありました-これは 'CMDがホストするコマンドプロンプトウィンドウです「EXPLORER.EXE」のようなGUIウィンドウではなく、「EXE」。
詳細については、バッチファイルのすべてのコメントと説明をお読みください。
使用ツール:
1. MonitorInfoView ニールSoferによって(41キロバイト)
2. NirCmd ニールSoferによって(43キロバイト)
3. GetPIDs ダニエルScheibliによって(280キロバイト)
4. upx.exe ---私たちは、このデモで使用するコマンドラインプログラムです。バッチファイルと同じディレクトリに配置します。
5.バッチファイル---以下を参照
5つのファイルすべてをディレクトリに収集します。これは、すべてのWindowsシステムで実行可能なバッチファイルです。
このデモバッチは、カスタムコンソールウィンドウを表示する以外upx.exe
に、コマンドラインスイッチで実行されます--best -v -o getpids-compressed.exe getpids.exe
。ツール「GetPIDs」を88 KBに圧縮します........起動後、このコンソールウィンドウのタイトルバーをダブルクリックして、興味深い現象を確認します。このコンソールウィンドウはGUIのように動作します。
@echo off
setlocal enabledelayedexpansion enableextensions
pushd %~dp0%
REM ----- ADD SOME USEFUL INFORMATION TO THIS CONSOLE WINDOW TITLE.
for /f "tokens=3" %%A in ('getpids.exe') do set _PROCESS_ID_=%%A
title This batch file runs a command-line program and sets the position and size of its console window (PROCESS ID = %_PROCESS_ID_%)
REM ********************** DESCRIPTION ************************************
REM ** This script opens a command-line program console window with specified
REM ** screen properties at the primary monitor (containing the taskbar).
REM ** The "X/Y position" and "W/H size" of the console window is auto-set by
REM ** this script and the monitor resolution is auto-calculated to suit.
REM ** 'MonitorInfoView.exe' is the helper tool used to capture the resolution
REM ** info of the monitor.
REM ** 'nircmd.exe' is the tool performing all the display trickery.
REM ** 'getpids.exe' is the helper tool used to capture the Process ID of the
REM ** working batch file, thus allowing us to identify which CMD.EXE host this
REM ** console window belongs to when looking at Task Manager.
REM **
REM ** To tweak this script, go to the code section named:
REM ** >>>>> USER INPUT/PREFERENCES ARE ALL SET HERE <<<<<
REM ***********************************************************************
REM ----- CLEAR ANY PREVIOUS JOB OUTPUTS IF THEY EXIST
if exist ~TMP.TXT type NUL > ~TMP.TXT
REM ----- OUTPUT THE PRIMARY MONITOR INFORMATION TO A TEXT FILE
MonitorInfoView.exe /hideinactivemonitors 1 /stext ~TMP.TXT
REM ----- ISOLATE THE RESOLUTION LINE, REMOVING ALL THE OTHER LINES IN THE TEXT FILE
for /f "delims=" %%A in ('type "~TMP.TXT" ^|find.exe /i "Maximum Resolution"') do echo %%A>~TMP.TXT
REM ----- GET THE RESOLUTION NUMBERS, AND SET THEM AS VARIABLES
for /f "tokens=3,4 delims=:X " %%A in ('type "~TMP.TXT"') do set _SCREENW_=%%A& set _SCREENH_=%%B
REM >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
REM >>>>>>>>>> USER INPUT/PREFERENCES ARE ALL SET HERE [BEGIN] <<<<<<<<<<<<
REM ----- ----------------------------------------
REM ----- |COMMAND-LINE PROGRAM TO BE RUN: FILE NAME (WITH EXTENSION)
REM ----- |
REM ----- | |IF PROG TO BE RUN IS 'CMD.EXE' (WINDOWS COMMAND PROCESSOR)
REM ----- | |ALSO READ THE NEXT COMMENTS ABOUT SETTING SWITCHES
set _MYPROGRAM_=upx.exe
REM ----- ----------------------------------------
REM ----- |COMMAND-LINE PROGRAM TO BE RUN: SWITCHES (TO BE PASSED TO THE PROGRAM)
REM ----- |
REM ----- | |YOU CAN LEAVE THIS VARIABLE WITH A BLANK VALUE IF RUNNING THE PROG WITHOUT ANY PARAMETERS
REM ----- | |
REM ----- | | |IF THE VALUE CONTAINS THE CHARS '|' OR '<' OR '>' THIS BATCH WILL FAIL TO RUN !!!
REM ----- | | |
REM ----- | | | |YOU CAN APPEND EXTRA TEXT TO THE END OF THE COMMAND BY USING THE AMPERSAND CHARACTER '&'
REM ----- | | | | EXAMPLES:
REM ----- | | | | set _MYPROGRAMSWITCHES_=dir "%systemroot%\system32" /a/o/s/4 & this text will be ignored; useful for adding some inline comments.
REM ----- | | | | set _MYPROGRAMSWITCHES_=dir "%systemroot%\system32" /a/o/s/4 & this works too, and the extra blank spaces will also be ignored.
REM ----- | | | |
REM ----- | | | | |IF PROG TO BE RUN IS 'CMD.EXE' (WINDOWS COMMAND PROCESSOR), PATH ARGUMENTS MUST POINT TO A SPECIFIC LOCATION
REM ----- | | | | |AND BE ABSOLUTE OR INCLUDE ENVIRONMENT VARIABLES, AND ALWAYS ENCLOSE PATHS IN QUOTES.
REM ----- | | | | | EXAMPLES:
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "C:\Folder With Spaces\File.txt"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "C:\FolderWithoutSpaces\File.txt"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "%SYSTEMROOT%\..\Folder With Spaces\File.txt"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "%SYSTEMROOT%\..\FolderWithoutSpaces\File.txt"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type %0 <<<<<<<this batch file (quotes are optional; the only exception to the rule)
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "%~dp0%\..\File.txt" <<<<<<<goes back one Dir from this batch file
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "%~dp0%\..\SomeFolder\File.txt" <<<<<<<goes back one Dir from this batch file and then forwards into "SomeFolder"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=dir "%systemroot%\system32" /a/o/s/4 <<<<<<<displays directory listing of the System folder. NOTE: 'dir' by itself and
REM ----- | | | | | without a specific path argument will resolve to the folder of this batch file.
set _MYPROGRAMSWITCHES_=--best -v -o getpids-compressed.exe getpids.exe &// original is 280 KB, will compress down to 88 KB
REM ----- ----------------------------------------
REM ----- |COMMAND-LINE PROGRAM TO BE RUN: DIRECTORY PATH (OF THE PROGRAM)
REM ----- |
REM ----- | |YOU CAN LEAVE THIS VARIABLE WITH A BLANK VALUE IF THE PROG IS AT SAME LOCATION AS BATCH
REM ----- | |THIS VARIABLE WILL BE IGNORED IF THE PROG IS 'CMD.EXE'
REM ----- | |
REM ----- | | EXAMPLES:
REM ----- | | set _MYPROGRAMDIR_=.\ <<<<<<<relative path, program and batch in same directory
REM ----- | | dot backslash *OR* dot (trailing backslash is optional).
REM ----- | | set _MYPROGRAMDIR_=%dp0% <<<<<<<relative path, program and batch in same directory
REM ----- | | same locatiion as previous but using environment variable.
REM ----- | | set _MYPROGRAMDIR_=..\ <<<<<<<relative path, program is back one directory from batch
REM ----- | | dot dot backslash *OR* dot dot (trailing backslash is optional).
REM ----- | | set _MYPROGRAMDIR_=%dp0%\.. <<<<<<<relative path, program is back one directory from batch
REM ----- | | same as location as previous but using environment variable.
REM ----- | | set _MYPROGRAMDIR_=%SYSTEMROOT%\..\My Utilities <<<<<<<relative path, program is back one directory from the Windows folder
REM ----- | | and then forwards into the folder 'My Utilities'
REM ----- | | set _MYPROGRAMDIR_=C:\My Utilities\ <<<<<<<absolute path, enclosing quotes are optional; trailing backslash is optional
REM ----- | | same location as previous
set _MYPROGRAMDIR_=
REM ----- ----------------------------------------
REM ----- |SET THE WANTED DIMENSIONS OF THIS CONSOLE WINDOW
set /a _WINW_=(%_SCREENW_% / 2) + 250
set /a _WINH_=(%_SCREENH_% / 2) + 150
set /a _WINX_=(%_SCREENW_% - %_WINW_%) / 2
set /a _WINY_=(%_SCREENH_% - %_WINH_%) / 2
REM ----- ----------------------------------------
REM ----- |STYLE THIS CONSOLE WINDOW !!!IMPORTANT!!!
REM ----- |
REM ----- | |IF WE DO NOT USE THE 'MODE' COMMAND HERE ALONG WITH ITS 'COLS/LINES' VALUES THEN
REM ----- | |THE POSITIONING/SIZING OF THIS WINDOW BY 'NIRCMD.EXE' FURTHER DOWN WILL NOT WORK !!!
REM ----- | |
REM ----- | | |SOME COMMANDS WILL OUTPUT A LOT OF LINES AND AND THE CONSOLE DISPLAY WILL BE TRUNCATED
REM ----- | | |RUN THIS FOR AN EXAMPLE=====> cmd.exe dir "%systemroot%\system32" /a/o/s/4 <=====
REM ----- | | |TO SOLVE THIS PROBLEM, SET THE 'LINES' VALUE OF 'MODE' TO A VERY HIGH NUMBER
REM ----- | | |THE MAXIMUM NUMBER IS 32000 (ON MY SYSTEM); 'MODE' WILL BE IGNORED IF THE NUMBER IS HIGHER !!!
color 0A
mode.com con:cols=%_SCREENW_% lines=10000
REM ----- ----------------------------------------
REM ----- |DELETE THE TEMP TEXT FILE MADE BY 'MonitorInfoView.exe'
REM ----- |COMMENT OUT THE LINE IF YOU DON'T CARE ABOUT DELETING IT
del /f /q ~TMP.TXT 2>nul >nul
REM >>>>>>>>>> USER INPUT/PREFERENCES ARE ALL SET HERE [END] <<<<<<<<<<<<<<
REM >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
REM ----- RUN THE TASK . . .
nircmd.exe win setsize foreground %_WINX_% %_WINY_% %_WINW_% %_WINH_%
popd
if not defined _MYPROGRAMDIR_ set _MYPROGRAMDIR_=%~dp0
if "!_MYPROGRAMDIR_!" == "." set _MYPROGRAMDIR_=%~dp0
if "!_MYPROGRAMDIR_!" == ".\" set _MYPROGRAMDIR_=%~dp0
pushd %_MYPROGRAMDIR_%
if not exist %_MYPROGRAMDIR_% goto _SKIP_
set _CONVERT_PATH_="%CD%"
set _CONVERT_PATH_=%_CONVERT_PATH_:"=%
set _MYPROGRAMDIR_=%_CONVERT_PATH_%
:_SKIP_
popd
echo ===============================================================================
echo PROGRAM ..............: !_MYPROGRAM_!
if "!_MYPROGRAM_!"=="cmd.exe" (echo PROGRAM DIR ..........: %SYSTEMROOT%\system32) else (echo PROGRAM DIR ..........: !_MYPROGRAMDIR_!)
echo PROGRAM SWITCHES .....: !_MYPROGRAMSWITCHES_!
echo ===============================================================================
if "!_MYPROGRAM_!"=="cmd.exe" (goto _PROGRAM_IS_CMD_) else (goto _PROGRAM_IS_OTHER_)
:_PROGRAM_IS_CMD_
cd /d "%SYSTEMROOT%\system32"
!_MYPROGRAMSWITCHES_!
echo. & echo. & echo.
goto _END_
:_PROGRAM_IS_OTHER_
cd /d "!_MYPROGRAMDIR_!"
if "%CD%\!_MYPROGRAM_!" == "%CD%\" goto _ERRORMESSAGE_
if not exist "%CD%\!_MYPROGRAM_!" goto _ERRORMESSAGE_
"!_MYPROGRAMDIR_!\!_MYPROGRAM_!" !_MYPROGRAMSWITCHES_!
echo. & echo. & echo.
goto _END_
:_ERRORMESSAGE_
cls
color 4F
echo ===============================================================================
echo PROGRAM ..............: !_MYPROGRAM_!
echo PROGRAM DIR ..........: !_MYPROGRAMDIR_!
echo PROGRAM SWITCHES .....: !_MYPROGRAMSWITCHES_!
echo ===============================================================================
echo ^^!^^!^^! E R R O R ^^!^^!^^!
echo.
echo The program has failed to run; the path set by the above values does not exist.
echo Please exit this window and check the values you have set in this batch file.
echo.
echo This batch file is located here:
echo %0
echo. & echo. & echo. & echo. & echo.
:_END_
REM ----- CLEAR VARIABLE VALUES FROM MEMORY . . .
set _PROCESS_ID_=
set _SCREENW_=
set _SCREENH_=
set _MYPROGRAM_=
set _MYPROGRAMSWITCHES_=
set _MYPROGRAMDIR_=
set _WINW_=
set _WINH_=
set _WINX_=
set _WINY_=
set _CONVERT_PATH_=
REM ----- STOP THIS CONSOLE WINDOW FROM CLOSING !!!IMPORTANT!!! . . .
REM ----- |
REM ----- | http://superuser.com/questions/306167/how-to-prevent-the-command-prompt-from-closing-after-execution
REM ----- | | by 'kitsu.eb' (April 26, 2014)
REM ----- | | | "My solution (just tested on Win7) is to add CMD as the last line in the batch file.
REM ----- | | | This runs a nested command prompt that inherits the environment of its parent.
REM ----- | | | That child shell holds the batch process open until you EXIT, at which point the
REM ----- | | | batch has no child processes and also exits."
REM ----- | Comment . . .
REM ----- | | by me
REM ----- | | | "This is a workaround. You can't use PAUSE as the last command to stop this window from closing
REM ----- | | | because the window will terminate after you press a key. While this batch is running, if you open
REM ----- | | | Task Manager/Process Hacker/Process Explorer you will see an extra CMD.EXE child process running
REM ----- | | | under the parent CMD.EXE of this batch file."
cmd.exe