2015/06/16

배치 스크립트 예제 (Update:20151212)

http://kiros33.blog.me/220317044016


Revision History
2015/03/31 23:40:07 - 최초 작성
2015/04/04 23:25:19 - 레지스트리 처리 Batch를 통한 특수문자 처리 정보
2015/06/16 23:47:31 - command-here.cmd 정보 추가
2015/12/26 16:37:57 - command-here.cmd 스크립트 수정, 한글과 영어 변경 가능하도록 변수화

Reference Page

Attach File
checkfilefolder.vbs - 전달인수로 받은 경로가 파일인지 폴더인지 확인
command-here.cmd - 파일탐색기에서 명령창을 관리자 권한으로 열 수 있도록 설정 (Shift + 우클릭)
createshortcut.bat - 전달인수를 사용해서 단축아이콘 생성
getparentfolder.vbs - 전달인수로 받은 경로의 부모 폴더 반환
getspecialfolders.vbs - 바탕화면, 다운로드 폴더 같은 특수 폴더의 경로 반환
invokeverb.vbs - 마우스 우클릭에 나오는 열기/출력 같은 명령 실행
removeshortcut.bat - 단축아이콘 제거
  

이것 또한 자꾸 잊어버리는 못난 머리를 위해 자주 쓰는 틀을 기록합니다.

다음과 같은 다양한 기법이 사용되었습니다. var.cmd 파일에 정의된 변수를 사용하는 예제이므로 정의되지 않은 변수는 있겠구나 하시면 됩니다.

  • 기본 디렉토리 지정
  • 외부 파일 참조
  • 관리자 권한으로 실행
  • 레지스트리 관리
  • 배치내 라벨을 이용한 함수 처리
  • 전달인자 처리
  • 단축아이콘 생성
  • STDIN, STDOUT, STDERR 사용 

@echo off
@rem @chcp 65001 2>&1 > NUL
@rem setlocal enableextensions enabledelayedexpansion
setlocal ENABLEDELAYEDEXPANSION
SET SETENV=..\bin\SetEnv.exe
SET RUNASADMIN=%WINDIR%\System32\runasadmin.txt SET LN=mklink SET RM=del
if not exist %RUNASADMIN% goto STEP_RUNASUSER
SET RMDIR=rmdir SET CP=copy /y echo runasadmin > %RUNASADMIN% %RM% /f /q %RUNASADMIN% 2>&1 > NUL
set PATH=%PATH%;%DEVELOPMENT_HOME%\bin
cd /d "%~dp0" call var.cmd REM ################### Base folders%SETENV% -ua "DEVELOPMENT_HOME" "%DEVELOPMENT_HOME%" %SETENV% -ua "PATH" %%"%%DEVELOPMENT_HOME%%\bin"
call createshortcut "AllUsersPrograms" "%SUBLIME_NAME%" "%SUBLIME_EXEC%" "" "%SUBLIME_ICON%"

REM ################### Sublime Text call createshortcut "Desktop" "%SUBLIME_NAME%" "%SUBLIME_EXEC%" "" "%SUBLIME_ICON%"
 
REM ################### Everything
reg add "HKLM\SOFTWARE\Classes\*\shell\Open With %SUBLIME_NAME%\command" /t REG_SZ /v "" /d "%SUBLIME_EXEC% \"%%1\"" /f 2>&1 > NUL call createshortcut "AllUsersPrograms" "%EVERYTHING_NAME%" "%EVERYTHING_EXEC%" "" "%EVERYTHING_ICON%"
%SETENV% -ua "PATH" %%"%%MSYS_HOME%%\bin"
call createshortcut "Desktop" "%EVERYTHING_NAME%" "%EVERYTHING_EXEC%" "" "%EVERYTHING_ICON%" REM ################### MinGW %SETENV% -ua "MINGW_HOME" "%MINGW_HOME%" %SETENV% -ua "PATH" %%"%%MINGW_HOME%%\bin" %SETENV% -ua "MSYS_HOME" "%MSYS_HOME%"
call createshortcut "Desktop" "%MSYS_NAME%" "%MSYS_EXEC%" "" "%MSYS_ICON%"
call createshortcut "AllUsersPrograms" "%MINGWINST_NAME%" "%MINGWINST_EXEC%" "" "%MINGWINST_ICON%" call createshortcut "Desktop" "%MINGWINST_NAME%" "%MINGWINST_EXEC%" "" "%MINGWINST_ICON%" call createshortcut "AllUsersPrograms" "%MSYS_NAME%" "%MSYS_EXEC%" "" "%MSYS_ICON%" %LN% "%DEVELOPMENT_HOME%\bin\find2.exe" "%MSYS_HOME%\bin\find.exe" 2>&1 > NUL
%LN% /d "%HOMEPATH%\.ssh" "%SSH_HOME%\.ssh"
REM ################### Git %SETENV% -ua "GIT_HOME" "%GIT_HOME%" %SETENV% -ua "PATH" %%"%%GIT_HOME%%\bin" %SETENV% -ua "PATH" %%"%%GIT_HOME%%\mingw\bin" %SETENV% -ua "PATH" %%"%%GIT_HOME%%\cmd" %SETENV% -ua "TERM" "msys" REM ################### SSH %SETENV% -ua "SSH_HOME" "%SSH_HOME%" %SETENV% -ua "HOME" %%"%%HOMEPATH%%" REM ################### Python %SETENV% -ua "PYTHON_HOME" "%PYTHON_HOME%" %SETENV% -ua "PATH" %%"%%PYTHON_HOME%%\App" REM ################### CodeBlocks call createshortcut "AllUsersPrograms" "%CODEBLOCKS_NAME%" "%CODEBLOCKS_EXEC%" "" "%CODEBLOCKS_ICON%" call createshortcut "Desktop" "%CODEBLOCKS_NAME%" "%CODEBLOCKS_EXEC%" "" "%CODEBLOCKS_ICON%"
%SETENV% -ua "PATH" %%"%%LUA_HOME%%\bin"
REM ################### ClipX call createshortcut "AllUsersPrograms" "%CLIPX_NAME%" "%CLIPX_EXEC%" "" "%CLIPX_ICON%" call createshortcut "Desktop" "%CLIPX_NAME%" "%CLIPX_EXEC%" "" "%CLIPX_ICON%" reg add "%SYSTEM_RUN_REG%" /t REG_SZ /v "%CLIPX_NAME%" /d "\"%CLIPX_EXEC%\"" /f 2>&1 > NUL REM ################### Lua %SETENV% -ua "LUA_HOME" "%LUA_HOME%"
call createshortcut "AllUsersPrograms" "%GOODSYNC_NAME%" "%GOODSYNC_EXEC%" "" "%GOODSYNC_ICON%"
set LUA_VER_STR=%LUA_VERSION:.=% %LN% "%DEVELOPMENT_HOME%\bin\lua.exe" "%LUA_HOME%\bin\lua%LUA_VERSION:.=%.exe" 2>&1 > NUL %LN% "%DEVELOPMENT_HOME%\bin\luac.exe" "%LUA_HOME%\bin\luac%LUA_VERSION:.=%.exe" 2>&1 > NUL %LN% "%DEVELOPMENT_HOME%\bin\wlua.exe" "%LUA_HOME%\bin\wlua%LUA_VERSION:.=%.exe" 2>&1 > NUL REM ################### GoodSync
call createshortcut "Desktop" "%SQLITEDATABASEBROWSERPORTABLE_NAME%" "%SQLITEDATABASEBROWSERPORTABLE_EXEC%" "" "%SQLITEDATABASEBROWSERPORTABLE_ICON%"
call createshortcut "Desktop" "%GOODSYNC_NAME%" "%GOODSYNC_EXEC%" "" "%GOODSYNC_ICON%" REM ################### SQLiteDatabaseBrowserPortable call createshortcut "AllUsersPrograms" "%SQLITEDATABASEBROWSERPORTABLE_NAME%" "%SQLITEDATABASEBROWSERPORTABLE_EXEC%" "" "%SQLITEDATABASEBROWSERPORTABLE_ICON%" REM ################### Premake %LN% "%DEVELOPMENT_HOME%\bin\premake4.exe" "%PREMAKE_HOME%\premake4.exe" 2>&1 > NUL
REM ################### SQLite
REM ################### Boost %SETENV% -ua "BOOST_HOME" "%BOOST_HOME%" %SETENV% -ua "PATH" %%"%%BOOST_HOME%%\lib" REM ################### jsoncpp %SETENV% -ua "JSONCPP_HOME" "%JSONCPP_HOME%" %SETENV% -ua "PATH" %%"%%JSONCPP_HOME%%\lib" REM ################### SOCI %SETENV% -ua "SOCI_HOME" "%SOCI_HOME%" %SETENV% -ua "PATH" %%"%%SOCI_HOME%%\bin" %SETENV% -ua "PATH" %%"%%SOCI_HOME%%\lib" %SETENV% -ua "SQLITE_HOME" "%SQLITE_HOME%"
call createshortcut "Desktop" "%BENTHICSQALL_NAME%" "%BENTHICSQALL_EXEC%" "" "%BENTHICSQALL_ICON%"
%SETENV% -ua "PATH" %%"%%SQLITE_HOME%%" REM ################### InstantClient %SETENV% -ua "ORACLE_HOME" "%ORACLE_HOME%" %SETENV% -ua "PATH" %%"%%ORACLE_HOME%%" rem %SETENV% -ua "PATH" %%"%%ORACLE_HOME%%\bin" %SETENV% -ud "TNS_ADMIN" %SETENV% -ua "TNS_ADMIN" %%"%%ORACLE_HOME%%\network\admin" REM ################### Benthic call createshortcut "AllUsersPrograms" "%BENTHICSQALL_NAME%" "%BENTHICSQALL_EXEC%" "" "%BENTHICSQALL_ICON%"
call createshortcut "Desktop" "%PLEDIT_NAME%" "%PLEDIT_EXEC%" "" "%PLEDIT_ICON%"
call createshortcut "AllUsersPrograms" "%GOLDVIEW_NAME%" "%GOLDVIEW_EXEC%" "" "%GOLDVIEW_ICON%" call createshortcut "Desktop" "%GOLDVIEW_NAME%" "%GOLDVIEW_EXEC%" "" "%GOLDVIEW_ICON%" call createshortcut "AllUsersPrograms" "%GOLDEN_NAME%" "%GOLDEN_EXEC%" "" "%GOLDEN_ICON%" call createshortcut "Desktop" "%GOLDEN_NAME%" "%GOLDEN_EXEC%" "" "%GOLDEN_ICON%" call createshortcut "AllUsersPrograms" "%PLEDIT_NAME%" "%PLEDIT_EXEC%" "" "%PLEDIT_ICON%"
goto STEP_END
reg add "HKCU\Software\Benthic\BenthicSQALL\Login" -t REG_SZ -v "RVal_3" -d "D62C45B53652B6DD2063A5" /f 2>&1 > NUL reg add "HKCU\Software\Benthic\Goldview32\Login" -t REG_SZ -v "RVal" -d "85F20C7EFE1D63A1E020" /f 2>&1 > NUL reg add "HKCU\Software\Benthic\Golden6\Login" -t REG_SZ -v "RVal" -d "EA5EC653B2D8B74E2954773FD3477B263C80F712E806540B97A6C44008B7BB1DF7B58A36F185906EE1733071" /f 2>&1 > NUL reg add "HKCU\Software\Benthic\PLEdit32\Login" -t REG_SZ -v "RVal" -d "2B58A9D91A7699E52852" /f 2>&1 > NUL REM ################### END :STEP_RUNASUSER echo. echo.
goto :EOF
echo ============================================================ echo Please run as administrator echo ============================================================ echo. pause goto STEP_END :STEP_END endlocal goto :EOF :STEP_CHECK_SETENV if not exist %SETENV% goto STEP_COPY_SETENV goto :EOF :STEP_COPY_SETENV copy /y "%~1" "%SETENV%" 2>&1 > NUL goto :EOF :STEP_DELETE_SETENV %RM% /f /q "%SETENV%" 2>&1 > NUL goto :EOF :STEP_GET_PARENT_DIR set GET_PARENT_DIR_VAR=%~1 for %%i in ("%~2..") do set "%GET_PARENT_DIR_VAR%=%%~fi" set GET_PARENT_DIR_VAR= :STEP_GET_ARGC set GET_ARGC_VAR=%~1 set /a "%GET_ARGC_VAR% = 0"
set GET_SPECIALFOLDER_VAR=
:STEP_GET_ARGC_LOOP if not x%2x==xx ( shift set /a "%GET_ARGC_VAR% = %GET_ARGC_VAR% + 1" goto :STEP_GET_ARGC_LOOP ) set GET_ARGC_VAR= goto :EOF :STEP_CALL_VBS wscript //nologo //b %* goto :EOF :STEP_GET_SPECIALFOLDER set GET_SPECIALFOLDER_VAR=%~1 >$$tempfile ( cscript.exe //nologo "%DEVELOPMENT_HOME%\bin\getspecialfolders.vbs" "%~2" ) && set /p %GET_SPECIALFOLDER_VAR%=<$$tempfile %RM% $$tempfile REM echo result GET_SPECIALFOLDER_VAR is ^!%GET_SPECIALFOLDER_VAR%^!
goto :EOF 

:STEP_GET_PARENTFOLDER
set GET_PARENTFOLDER_VAR=%~1
>$$tempfile (
cscript.exe //nologo "getparentfolder.vbs" "%~2"
) && set /p %GET_PARENTFOLDER_VAR%=<$$tempfile %RM% $$tempfile
REM echo result GET_PARENTFOLDER_VAR is ^!%GET_PARENTFOLDER_VAR%^!
set GET_PARENTFOLDER_VAR= goto :EOF :STEP_COPY_TO echo Copy %~1 to %~2 %CP% "%~1" "%~2" 2>&1 > NIL goto :EOF :STEP_MOVE_TO
goto :EOF
echo Move %~1 to %~2
%MV% "%~1" "%~2" 2>&1 > NIL
   
  
>>>>> 2015/04/04 23:25:19 - 레지스트리 처리 Batch를 통한 특수문자 처리 정보

명령창과 배치파일내에서 레지스트리 명령을 구성할 때 " 와 같은 특수문자를 처리하는 과정에 대한 시행착오를 없애기 위해 기록
#regedit에서 Double Quotes " 문자를 사용
"C:\Windows\System32\elevate.exe" "cmd.exe /s /k pushd \""%V\"""

#batch에서 reg 명령을 사용할 때
reg add "HKCR\Drive\shell\cmd.adm\command" /f /v "" /d "\"C:\Windows\System32\elevate.exe\" \"cmd.exe /s /k pushd \\\"\"%%V\\\"\"\""

#명령창에서 reg 명령을 사용할 때
reg add "HKCR\Drive\shell\cmd.adm\command" /f /v "" /d "\"C:\Windows\System32\elevate.exe\" \"cmd.exe /s /k pushd \\\"\"%V\\\"\"\""


>>>>> 2015/06/16 23:47:31 - command-here.cmd 정보 추가

command-here.cmd
elevate.exe
SetEnv.exe

"Open command window here (admin)" 혹은 "여기에 명령 창 열기 (관리자)" 이름으로 레지스트리 등록
@echo off
setlocal ENABLEDELAYEDEXPANSION

SET RUNASADMIN=%WINDIR%\System32\runasadmin.txt
SET LN=mklink
SET RM=del /f /q
SET RMDIR=rmdir
SET CP=copy /y

echo runasadmin > %RUNASADMIN%
if not exist %RUNASADMIN% goto STEP_RUNASUSER
%RM% /f /q %RUNASADMIN%  2>&1 > NUL

cd /d "%~dp0"

REM ################### Base folders

call :STEP_GET_PARENT_DIR "SYSTEM32_DIR" "%ComSpec%"

REM ################### Copy utilities

call :STEP_CHECK_FILE "%SYSTEM32_DIR%\SetEnv.exe" "SetEnv.exe"
call :STEP_CHECK_FILE "%SYSTEM32_DIR%\elevate.exe" "elevate.exe"

REM SET COMMAND_TITLE="여기서 명령 창 열기 (관리자)"
SET COMMAND_TITLE="Open command window here (admin)"

reg add "HKCR\Directory\shell\cmd.adm" /f /v "" /d %COMMAND_TITLE% 2>&1 > NUL
reg add "HKCR\Directory\shell\cmd.adm" /f /v "Extended" /d "" 2>&1 > NUL
reg add "HKCR\Directory\shell\cmd.adm\command" /f /v "" /d "\"C:\Windows\System32\elevate.exe\" \"cmd.exe /s /k pushd \\\"\"%%V\\\"\"\"" 2>&1 > NUL
reg add "HKCR\Directory\Background\shell\cmd.adm" /f /v "" /d %COMMAND_TITLE% 2>&1 > NUL
reg add "HKCR\Directory\Background\shell\cmd.adm" /f /v "Extended" /d "" 2>&1 > NUL
reg add "HKCR\Directory\Background\shell\cmd.adm\command" /f /v "" /d "\"C:\Windows\System32\elevate.exe\" \"cmd.exe /s /k pushd \\\"\"%%V\\\"\"\"" 2>&1 > NUL
reg add "HKCR\Drive\shell\cmd.adm" /f /v "" /d %COMMAND_TITLE% 2>&1 > NUL
reg add "HKCR\Drive\shell\cmd.adm" /f /v "Extended" /d "" 2>&1 > NUL
reg add "HKCR\Drive\shell\cmd.adm\command" /f /v "" /d "\"C:\Windows\System32\elevate.exe\" \"cmd.exe /s /k pushd \\\"\"%%V\\\"\"\"" 2>&1 > NUL

goto STEP_END

:STEP_RUNASUSER
echo.
echo.
echo ============================================================
echo Please run as administrator
echo ============================================================
echo.
pause
goto STEP_END

:STEP_END
endlocal
goto :EOF

:STEP_CHECK_FILE
if not exist "%~1" (
  call :STEP_COPY_FILE "%~1" "%~2"
) else (
  @REM
)
goto :EOF

:STEP_COPY_FILE
%CP% "%~2" "%~1" 2>&1 > NUL
goto :EOF

:STEP_DELETE_FILE
%RM% "%~1" 2>&1 > NUL
goto :EOF

:STEP_GET_PARENT_DIR
set GET_PARENT_DIR_VAR=%~1
set GET_PARENT_DIR_TARGET=%~2
if %GET_PARENT_DIR_TARGET:~-1%==\ set GET_PARENT_DIR_TARGET=%GET_PARENT_DIR_TARGET:~0,-1%
for %%i in ("%~2\..") do (
  set "%GET_PARENT_DIR_VAR%=%%~fi"
)
set GET_PARENT_DIR_VAR=
goto :EOF

:STEP_GET_ARGC
set GET_ARGC_VAR=%~1
set /a "%GET_ARGC_VAR% = 0"

:STEP_GET_ARGC_LOOP
if not x%2x==xx (
  shift
  set /a "%GET_ARGC_VAR% = %GET_ARGC_VAR% + 1"
  goto :STEP_GET_ARGC_LOOP
)
set GET_ARGC_VAR=
goto :EOF

:STEP_GET_SPECIALFOLDER
set GET_SPECIALFOLDER_VAR=%~1
>$$tempfile (
   cscript.exe //nologo "%DEVELOPMENT_HOME%\bin\getspecialfolders.vbs" "%~2"
) && set /p %GET_SPECIALFOLDER_VAR%=<$$tempfile
%RM% $$tempfile
REM echo result GET_SPECIALFOLDER_VAR is ^!%GET_SPECIALFOLDER_VAR%^!
set GET_SPECIALFOLDER_VAR=
goto :EOF

   
    

검색: Windows, 윈도우, Win, 윈, DOS, 도스, CMD, Command, 명령, Command line, Command-line, 명령줄, Script, 스크립트, Sample, 샘플, Example, 예제, 

댓글 없음:

댓글 쓰기