:引用網址
http://blog.xuite.net/m740138.m740138/blog/159018379-DOS%E5%8F%96%E5%BE%97%E6%97%A5%E6%9C%9F%E6%99%82%E9%96%93%E5%AD%97%E4%B8%B2
:echo %date% 結果為
:yyyy/MM/dd
:echo %date:~0,4% 結果為
:yyyy
:echo %date:~5,2% 結果為
:MM
:echo %date:~8,2% 結果為
:dd
:重點就在於 date 後 :~x,y 這個動作
:x 代表從第幾個位置開始取,從 0 起算
:y 代表要取幾個字
%date:~0,4%%date:~5,2%%date:~8,2%
----------------------------------------------------------
:小於10點前面補零
@echo off
set nowYear=%date:~0,4%
set nowMonth=%date:~5,2%
set nowDay=%date:~8,2%
set nowHr=%time:~0,2%
:若時小於10,前面補0 例: 9→09, 8→08
if %nowHr% LSS 10 set nowHr=0%nowHr%
set nowMin=%time:~3,2%
set NewFileName=Log_File_%nowYear%%nowMonth%%nowDay%%nowHr%%nowMin%.log
:例:用日期+時間當成檔案名稱
:取 '時' 要注意,在10點前是空白+1位數字,為避免檔名出現空白,所以要先處理,':'及'='之間有空白
set hour=%time: =0%
set LogFile=%DATE:~0,4%-%DATE:~5,2%-%DATE:~8,2%_%hour:~0,2%.%TIME:~3,2%.%TIME:~6,2%.log
echo %LogFile%
echo %DATE:~0,4%%DATE:~5,2%%DATE:~8,2%_%hour:~0,2%%TIME:~3,2%%TIME:~6,2%
:結果: 2012-10-26_12.30.17.log
沒有留言:
張貼留言