windows批處理

來源:互聯網
上載者:User

標籤: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.