標籤:blog http os 檔案 io for
1.日期作為變數當做檔案名稱的一部分.
C:\Documents and Settings\Simon>echo %date%
2008-09-09 星期二
C:\Documents and Settings\Simon>echo %date:~5,5%
09-09
如果我們需要加入自訂的分隔字元,比如想要YYYY--MM--DD的格式,只需要分別取出YYYY, MM, DD再串連起來即可:
C:\Documents and Settings\Simon>echo %date:~0,4%--%date:~5,2%--%date:~8,2%
2008--09—09
2.命令選擇
C:\Users\hp>choice /c dme /m defrag,mem,end
defrag,mem,end [D,M,E]?D
@echo off
choice /c dme /m defrag,mem,end
if errorlevel 3 goto defrag (應先判斷數值最高的錯誤碼)
if errorlevel 2 goto mem
if errotlevel 1 goto end
:defrag
c:dosdefrag
goto end
:mem
mem
goto end
:end
echo good bye
3.錯誤判斷
@net use /%1ipc$ %3 /u:"%2"
@if errorlevel 1 goto failed
4.組合命令
& Usage:第一條命令 & 第二條命令 [& 第三條命令...]
用這種方法可以同時執行多條命令,而不管命令是否執行成功
&& Usage:第一條命令 && 第二條命令 [&& 第三條命令...]
用這種方法可以同時執行多條命令,當碰到執行出錯的命令後將不執行後面的命令,如果一直沒有出錯則一直執行完所有命令;
|| Usage:第一條命令 || 第二條命令 [|| 第三條命令...]
用這種方法可以同時執行多條命令,當碰到執行正確的命令後將不執行後面的命令,如果沒有出現正確的命令則一直執行完所有命令;
Sample:
C:>dir z: && dir c:Ex4rch
The system cannot find the path specified.
C:>dir c:Ex4rch && dir z:
Volume in drive C has no label.
Volume Serial Number is 0078-59FB
Directory of c:Ex4rch
2002-05-14 23:55 <DIR> .
2002-05-14 23:55 <DIR> ..
2002-05-14 23:55 14 sometips.gif
1 File(s) 14 bytes
2 Dir(s) 768,671,744 bytes free
The system cannot find the path specified.
Sample:
C:Ex4rch>dir sometips.gif || del sometips.gif
Volume in drive C has no label.
Volume Serial Number is 0078-59FB
Directory of C:Ex4rch
2002-05-14 23:55 14 sometips.gif
1 File(s) 14 bytes
0 Dir(s) 768,696,320 bytes free
綜合執行個體:參見http://www.cnblogs.com/shiney/archive/2011/07/04/2097236.html
------------------------ cut here then save as .bat or .cmd file ---------------------------
@echo preparing to delete all the default shares.when ready pres any key.
@pause
@echo off
:Rem check parameters if null show usage.
if {%1}=={} goto :Usage
:Usage
echo.
echo ------------------------------------------------------
echo.
echo ☆ A example for batch file ☆
echo ☆ [Use batch file to change the sysytem share properties.] ☆
echo.
echo Author:Ex4rch
echo Mail:[email protected] QQ:1672602
echo.
echo Error:Not enough parameters
:END
echo.
echo ------------------------------------------------------
echo.
echo OK,delshare.bat has deleted all the share you assigned.
echo.Any questions ,feel free to mail [email protected].
echo
echo.
echo ------------------------------------------------------
echo.
:EOF
echo end of the batch file