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:Ex4rch@hotmail.com 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 toEx4rch@hotmail.com.
echo
echo.
echo ------------------------------------------------------
echo.
:EOF
echo end of the batch file