Overview : 기본
- @echo off :
- rem : 주석
- SET yyyymmdd=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2% : 20230327
- @color 0a : 0a 배경화면 black , 폰트 칼러 green
- @cls : 화면 Clear
- @break off : 중간에 사용자가 Ctrl+Break 못하도록 하는 명령어
- @title My Script : Win 계열에서 Windows Command Prompt 에 나오게 하는 Title
- EXIT /B 0 : CMD.EXE 끝내기 , 0 1 exitCode
예제1
1 2 3 4 5 6 7 |
@echo off :: Test 입니다. SET yyyymmdd=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2% REM SET yyyymmdd=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2% ECHO %yyyymmdd% |
예제2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
@echo off :: log folder SET Folder=C:\Log SET yyyymmdd=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2% IF EXIST %Folder%\%yyyymmdd%\*.* ( rem file exist :: ECHO %F% is existing echo y| move %Folder%\%yyyymmdd%\*.* %Folder%\Temp\ ::winscp.com /ini=nul /script=C:\"Program Files (x86)"\WinSCP\Log.txt if %ERRORLEVEL% neq 0 goto error echo Success echo y| del %Folder%\Temp\*.* ::echo y| move %Folder%\Temp\*.xml %Folder%\Log\ exit /b 0 :error echo Error! echo "Error............" exit /b 1 ) |
예제 3
- 13일 전 로그 디렉토리 삭제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
@Echo off Call :GetDate yy mm dd ::Echo Today Year: %yy% Month: %mm% Day: %dd% Set /A n=-13 Call :DateAdd yy mm dd %n% ::Echo %n% Year: %yy% Month: %mm% Day: %dd% SET Folder=C:\Log SET yyyymmdd=%yy%%mm%%dd% :: 디렉토리 존재 여부 확인 if not exist %Folder%\%yyyymmdd% ( echo 지정된 경로에 디렉토리가 존재하지 않습니다: %Folder%\%yyyymmdd% exit /b ) :: 디렉토리 삭제 rd /s /q %Folder%\%yyyymmdd% echo 파일이 성공적으로 삭제되었습니다: %Folder%\%yyyymmdd% exit /b 0 ) ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :GetDate yy mm dd :: :: Func: Loads local system date components into args 1 to 3. :: :: Args: %1 var to receive year, 4 digits (by ref) :: %2 var to receive month, 2 digits, 01 to 12 (by ref) :: %3 Var to receive day of month, 2 digits, 01 to 31 (by ref) ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: SetLocal EnableExtensions for /f "tokens=1-3 delims=.+-" %%A in ( 'wmic os get LocalDateTime^|findstr ^^[0-9]' ) do Set _DT=%%A Set "yy=%_DT:~0,4%"&Set "MM=%_DT:~4,2%"&Set "dd=%_DT:~6,2%" endlocal&set %1=%yy%&set %2=%MM%&set %3=%dd%&goto :EOF :: GetDate.cmd ::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :DateAdd yy mm dd #days :: :: Func: Adds/subs Days from/to a given date by converting to a :: Julian Day adding the offset and converting back. :: Args: :: %1 year component used to create JD, 4 digits (by ref) :: %2 month component used to create JD, leading zero ret (by ref) :: %3 day of month used to create MJD, leading zero ret (by ref) :: %4 days offset may be positive or negative (by val) ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: SetLocal Call set /A "yy=%%%1%%,mm=100%%%2%% %%%%100,dd=100%%%3%% %%%%100" Set /A jd=(1461*(yy+4800+(mm-14)/12))/4 Set /A jd=jd+(367*(mm-2-12*((mm-14)/12)))/12 Set /A jd=jd-(3*((yy+4900+(mm-14)/12)/100))/4,jd=jd+dd-32075 Set /A jd=jd+%4 set /A l=jd+68569,n=(4*l)/146097,l=l-(146097*n+3)/4 Set /A i=(4000*(l+1))/1461001,l=l-(1461*i)/4+31,j=(80*l)/2447 Set /A dd=l-(2447*j)/80,l=j/11,mm=j+2-(12*l),yy=100*(n-49)+i+l (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%) Endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&Goto :Eof ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: |
예제 4
- IIS 로그 파일 일정 날짜 파일 삭제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
@Echo off Call :GetDate yy mm dd ::Echo Today Year: %yy% Month: %mm% Day: %dd% Set /A n=-7 Call :DateAdd yy mm dd %n% ::Echo %n% Year: %yy% Month: %mm% Day: %dd% SET Folder=C:\inetpub\logs\LogFiles SET yyyymmdd=%yy%%mm%%dd% :: 20241008 에서 인텍스 두번째 부터 끝가지 : 241008 만 가져오기 SET last_six=%yyyymmdd:~2% ::SET last_six=%yyyymm:~2% ::SET last_six=%yyyy:~2% for /d %%D in ("%Folder%\*") do ( echo Deleting log files in: %%D del /q "%%D\u_ex%last_six%*.log" ) echo 파일이 성공적으로 삭제되었습니다: %Folder%\%yyyymmdd% exit /b 0 ) ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :GetDate yy mm dd :: :: Func: Loads local system date components into args 1 to 3. :: :: Args: %1 var to receive year, 4 digits (by ref) :: %2 var to receive month, 2 digits, 01 to 12 (by ref) :: %3 Var to receive day of month, 2 digits, 01 to 31 (by ref) ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: SetLocal EnableExtensions for /f "tokens=1-3 delims=.+-" %%A in ( 'wmic os get LocalDateTime^|findstr ^^[0-9]' ) do Set _DT=%%A Set "yy=%_DT:~0,4%"&Set "MM=%_DT:~4,2%"&Set "dd=%_DT:~6,2%" endlocal&set %1=%yy%&set %2=%MM%&set %3=%dd%&goto :EOF :: GetDate.cmd ::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :DateAdd yy mm dd #days :: :: Func: Adds/subs Days from/to a given date by converting to a :: Julian Day adding the offset and converting back. :: Args: :: %1 year component used to create JD, 4 digits (by ref) :: %2 month component used to create JD, leading zero ret (by ref) :: %3 day of month used to create MJD, leading zero ret (by ref) :: %4 days offset may be positive or negative (by val) ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: SetLocal Call set /A "yy=%%%1%%,mm=100%%%2%% %%%%100,dd=100%%%3%% %%%%100" Set /A jd=(1461*(yy+4800+(mm-14)/12))/4 Set /A jd=jd+(367*(mm-2-12*((mm-14)/12)))/12 Set /A jd=jd-(3*((yy+4900+(mm-14)/12)/100))/4,jd=jd+dd-32075 Set /A jd=jd+%4 set /A l=jd+68569,n=(4*l)/146097,l=l-(146097*n+3)/4 Set /A i=(4000*(l+1))/1461001,l=l-(1461*i)/4+31,j=(80*l)/2447 Set /A dd=l-(2447*j)/80,l=j/11,mm=j+2-(12*l),yy=100*(n-49)+i+l (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%) Endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&Goto :Eof ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: |