cozy logo CoffeeCat
profile

많이 게으른 ISTJ

인생 날로먹고싶다

 

 

  • 개발
  • windows

Windows batch Command

2023년 06월 15일 좋아요 0 댓글 0 조회수 0

윈도우 batch 파일 확장자명 : .bat


 

⚠️ Not implemented type

 


언젠가 다시 정리하겠지

 

주의점

  • path 와 같은 변수명은 내부에서 ㄹㅇ path 를 바꿔버리기 때문에 변수명 사용에 주의

  • SETLOCAL

    : 배치파일에서 환경변수의 가시성을 제어하는 옵션 설정

    Coffeescript
    Syntax SETLOCAL SETLOCAL {EnableDelayedExpansion | DisableDelayedExpansion} {EnableExtensions | DisableExtensions}
  • EnableDelayedExpansion : 실행 시간에 각 변수 확장

  • DisabledDelayedExpansion : 구문 분석 시간에 각 변수 확장 (default)

  •  

    현재 파일 이름 가져오기

  • d -- drive p -- path n -- file name x -- extension f -- full path

  • ex) c:\tmp\foo.bat

  • %~n0 foo
    %~nx0 foo.bat
    %~dpnx0 c:\tmp\foo.bat

     

    if문으로 string contain 확인하기

  • 아래 코드에서 if not 으로 하는 이유

  • %string:ㅁㅁㅁ% : 여기서 ㅁㅁㅁ를 삭제하기 때문에 if not으로 넣어야한다.

  • else는 if문의 닫는 괄호와 같은 line에 있어야 함.

  • Bash
    set YourString=This is a test If NOT "%YourString%"=="%YourString:test=%" ( echo Yes ) else ( echo No )

     

     

    Date

    Powershell
    @ECHO OFF :: Check WMIC is available WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_error :: Use WMIC to retrieve date and time FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO ( IF "%%~L"=="" goto s_done Set _yyyy=%%L Set _mm=00%%J Set _dd=00%%G Set _hour=00%%H SET _minute=00%%I SET _second=00%%K ) :s_done :: Pad digits with leading zeros Set _mm=%_mm:~-2% Set _dd=%_dd:~-2% Set _hour=%_hour:~-2% Set _minute=%_minute:~-2% Set _second=%_second:~-2% Set logtimestamp=%_yyyy%-%_mm%-%_dd%_%_hour%_%_minute%_%_second% goto make_dump :s_error echo WMIC is not available, using default log filename Set logtimestamp=_ :make_dump set FILENAME=database_dump_%logtimestamp%.sql ...

     

    Start .bat file

    Powershell
    cmd.exe /c '\my-app\my-file.bat'

     

    Get command output to variable

    Powershell
    FOR /F "tokens=*" %%g IN ('your command') do (SET VAR=%%g)

     

    Move Item

    Powershell
    Move-Item -Path C:\test.txt -Destination E:\Temp Move-Item -Path {원래 위치} -Destination {이동위치}

     

    for문

  • for문 내에서 변수 사용 시, !var! 사용

  •  

  •  

    Date 기록

    Bash
    SETcurrentDate=%date:~-4%-%date:~4,2%-%date:~7,2% SETdtStamp9=0%time:~1,1%.%time:~3,2%.%time:~6,2% SETdtStamp24=%time:~0,2%.%time:~3,2%.%time:~6,2% IF "%HOUR:~0,1%" == " " ( SETcurrentTime=%dtStamp9% ) ELSE ( SETcurrentTime=%dtStamp24% )

     

    관심 가질만한 포스트