為了讓備份檔案不會重複於是利用date time 方式來產生資料夾,日期解決了但是時間部份就無法自動補0,於是就利用 if 的方式解決了這個問題
set dd=D:\DB_Backup\%date:~0,4%%date:~5,2%%date:~8,2% set hour=%time:~0,2% if "%hour:~0,1%" == " " set hour=0%hour:~1,1% echo hour=%hour% set min=%time:~3,2% if "%min:~0,1%" == " " set min=0%min:~1,1% echo min=%min% set secs=%time:~6,2% if "%secs:~0,1%" == " " set secs=0%secs:~1,1% echo secs=%secs% set fn=%dd%%hour%%min%%secs% md %fn% NET STOP MSSQL$SQLEXPRESS COPY "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\DB_Data.mdf" %dd% COPY "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\DB_log.ldf" %dd% NET START MSSQL$SQLEXPRESS