windows批處理

1.日期做爲變量當作文件名的一部分.html

C:\Documents and Settings\Simon>echo %date%
2008-09-09 星期二htm

C:\Documents and Settings\Simon>echo %date:~5,5%
09-09 blog

若是咱們須要加入自訂的分隔符,好比想要YYYY--MM--DD的格式,只須要分別取出YYYY, MM, DD再鏈接起來便可:
C:\Documents and Settings\Simon>echo %date:~0,4%--%date:~5,2%--%date:~8,2%
2008--09—09 ip

2.命令選擇 ci

C:\Users\hp>choice /c dme /m defrag,mem,end
defrag,mem,end [D,M,E]?Dget

@echo off
            choice /c dme /m defrag,mem,end
            if errorlevel 3 goto defrag (應先判斷數值最高的錯誤碼)
            if errorlevel 2 goto mem
            if errotlevel 1 goto end cmd

      :defrag
            c:dosdefrag
            goto endit


            :mem
            mem
            goto endio


            :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

相關文章
相關標籤/搜索