몇 가지 작은 도우미 프로그램과 함께 배치 파일을 실행하는 것이 행복하다면 여기에 완전한 솔루션이 게시되어
있습니다. 배치 파일을 사용하여 프로그램을 실행하고 창의 위치와 크기를 어떻게 설정할 수 있습니까?
아래와 같이 내용물. . . .
이 데모 배치 파일은 명령 줄 프로그램을 실행하고 명령 프롬프트 콘솔 창의 위치와 크기를 설정합니다. 이 배치의 일반적인 주제는 이전 두 솔루션 (위 링크의 스택 오버플로 페이지)과 동일하지만이 문제를 해결하기위한 몇 가지 고유 한 문제와 해결 방법이있었습니다. .EXE '및'EXPLORER.EXE '와 같은 GUI 창은 아닙니다.
자세한 정보는 배치 파일의 모든 주석 및 설명을 읽으십시오.
사용되는 도구 :
1. MonitorInfoView Nir 씨 소퍼하여 (41킬로바이트)
2. NirCmd Nir 씨 소퍼하여 (43킬로바이트)
3. GetPIDs 다니엘 Scheibli하여 (2백80킬로바이트)
4. upx.exe --- 우리는이 데모에 사용하는 명령 행 프로그램이다; 배치 파일과 동일한 디렉토리에 배치하십시오.
5. 배치 파일 --- 아래
5 개 파일을 모두 디렉토리에 모으십시오. 이것은 모든 Windows 시스템에서 실행할 수있는 배치 파일입니다.
이 데모 배치는 사용자 정의 콘솔 창을 제공하는 것과는 별도로 upx.exe
명령 행 스위치로 실행 됩니다.--best -v -o getpids-compressed.exe getpids.exe
. 도구 'GetPIDs'를 88KB로 압축합니다. ........ 시작한 후이 콘솔 창의 제목 표시 줄을 두 번 클릭하면 흥미로운 현상이 나타납니다. 이 콘솔 창은 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