Windows Batch Scripting 教學

教學網站:

系統內建指令:

Command Description
ASSOC Associates an extension with a file type (FTYPE).
BREAK Sets or clears extended CTRL+C checking.
CALL Calls one batch program from another.
CD, CHDIR Displays or sets the current directory.
CHCP Displays or sets the active code page number.
CLS Clears the screen.
COLOR Sets the console foreground and background colors.
COPY Copies files.
DATE Displays and sets the system date.
DEL, ERASE Deletes one or more files.
DIR Displays a list of files and subdirectories in a directory.
ECHO Displays messages, or turns command echoing on or off.
ELSE Performs conditional processing in batch programs when “IF" is not true.
ENDLOCAL Ends localization of environment changes in a batch file.
EXIT Quits the CMD.EXE program (command interpreter).
FOR Runs a specified command for each file in a set of files.
FTYPE Sets the file type command.
IF Performs conditional processing in batch programs.
MD, MKDIR Creates a directory.
MOVE Moves a file to a new location
PATH Sets or modifies the PATH environment
PAUSE Causes the command session to pause for user input.
POPD Changes to the drive and directory poped from the directory stack
PROMPT Sets or modifies the string displayed when waiting for input.
PUSHD Pushes the current directory onto the stack, and changes to the new directory.
RD / RMDIR Removes the directory.
REM A comment command. Unlike double-colon (::), the command can be executed.
REN / RENAME Renames a file or directory
SET Sets or displays shell environment variables
SETLOCAL Creates a child-environment for the batch file.
SHIFT Moves the batch parameters forward.
START Starts a program with various options.
TIME Displays or sets the system clock
TITLE Changes the window title
TYPE Prints the content of a file to the console.
VER Shows the command processor, operating system versions.
VERIFY Verifies that file copy has been done correctly.
VOL Shows the label of the current volume.

指令範例:

FOR

Iterates over a series of values, executing a command.

In the following examples, %i is to be used from the command line while %%i is to be used from a batch.

Examples:

  • for %%i in (1,2,3) do echo %%i
    • In a batch, echoes 1, 2, and 3. In a batch, the command must use a double percent sign.
    • The remaining examples are intended to be directly pasted into a command line, so they use a single percent sign and include “@" to prevent repetitive display.
  • for %i in (1,2,3) do @echo %i
    • From a command line, echoes 1, 2, and 3.
    • The for command tries to interpret the items as file names and as patterns of file names containing wildcards.
    • It does not complain if the items do not match existing file names, though.
  • for %i in (1,2,a*d*c*e*t) do @echo %i
    • Unless you happen to have a file matching the third pattern, echoes 1 and 2, discarding the third item.
  • for %i in (1 2,3;4) do @echo %i
    • Echoes 1, 2, 3, and 4. Yes, a mixture of item separators is used.
  • for %i in (*.txt) do @echo %i
    • Echoes file names of files located in the current folder and having the .txt extension.
  • for %i in (“C:\Windows\system32\*.exe") do @echo %i
    • Echoes file names matching the pattern.
  • for /r %i in (*.txt) do @echo %i
    • Echoes file names with full paths, of files having the extension .txt located anywhere in the current folder including nested folders.
  • for /d %i in (*) do @echo %i
    • Echoes the names of all folders in the current folder.
  • for /r /d %i in (*) do @echo %i
    • Echoes the names including full paths of all folders in the current folder, including nested folders.
  • for /l %i in (1,1,10) do @echo %i
    • Echoes the numbers from 1 to 10.
  • for /f “tokens=*" %i in (list.txt) do @echo %i
    • For each line in a file, echoes the line.
  • for /f “tokens=*" %i in (list1.txt list2.txt) do @echo %i
    • For each line in the files, echoes the line.
  • for /f “tokens=*" %i in (*.txt) do @echo %i
    • Does nothing. Does not accept wildcards to match file names.
  • for /f “tokens=1-3 delims=:" %a in (“First:Second::Third") do @echo %c-%b-%a
    • Parses a string into tokens delimited by “:".
    • The quotation marks indicate the string is not a file name.
    • The second and third tokens are stored in %b and %c even though %b and %c are not expressly mentioned in the part of the command before “do".
    • The two consecutive colons are treated as one separator; %c is not “" but rather “Third".
    • Does some of the job of the cut command from other operating systems.
  • for /f “tokens=1-3* delims=:" %a in (“First:Second::Third:Fourth:Fifth") do @echo %c-%b-%a: %d
    • As above, just that the 4th and 5th items get captured in %d as “Fourth:Fifth", including the separator.
  • for /f “tokens=1-3* delims=:," %a in (“First,Second,:Third:Fourth:Fifth") do @echo %c-%b-%a: %d
    • Multiple delimiters are possible.
  • for /f “tokens=1-3″ %a in (“First Second Third,item") do @echo %c-%b-%a
    • The default delimiters are space and tab. Thus, they differ from the separators used to separate arguments passed to a batch.
  • for /f “tokens=*" %i in (‘cd’) do @echo %i
    • For each line of the result of a command, echoes the line.
  • for /f “tokens=*" %i in (‘dir /b /a-d-h’) do @echo %~nxai
    • For each non-hidden file in the current folder, displays the file attributes followed by the file name. In the string “%~nxai", uses the syntax described at #Percent tilde.
  • for /f “usebackq tokens=*" %i in (`dir /b /a-d-h`) do @echo %~nxai
    • As above, but using the backquote character (`) around the command to be executed.
  • for /f “tokens=*" %i in (‘tasklist ^| sort ^& echo End’) do @echo %i
    • Pipes and ampersands in the command to be executed must be escaped using caret (^).
  • (for %i in (1,2,3) do @echo %i) > anyoldtemp.txt
    • To redirect the entire result of a for loop, place the entire loop inside brackets before redirecting. Otherwise, the redirection will tie to the body of the loop, so each new iteration of the body of the loop will override the results of the previous iterations.
  • for %i in (1,2,3) do @echo %i > anyoldtemp.txt
    • An example related to the one above. It shows the consequence of failing to put the loop inside brackets.
--------------------------------------------
for %%i in (a b c) do (
    echo 1 %%i
    goto :cont
    echo 2 %%i
  :cont
    echo 3 %%i
)
--------------------------------------------

--------------------------------------------
for %%i in (a b c) do call :for_body %%i
exit /b
 
:for_body
    echo 1 %1
    goto :cont
    echo 2 %1
  :cont
exit /b
--------------------------------------------

String processing

Getting a substring of a variable by position and length:

Before running the following examples, ensure that %a% equals “abcd" by running this:

  • set a=abcd

The examples:

  • echo %a:~0,1%
    • Result: a
  • echo %a:~1,1%
    • Result: b
  • echo %a:~0,2%
    • Result: ab
  • echo %a:~1,2%
    • Result: bc
  • echo %a:~1%
    • Result: bcd
  • echo %a:~-1%
    • Result: d
  • echo %a:~-2%
    • Result: cd
  • echo %a:~0,-2%
    • Result: ab
  • echo %a:~0,-1%
    • Result: abc
  • echo %a:~1,-1%
    • Result: bc

Testing substring containment:

  • if not “%a:bc=%"=="%a%" echo yes
    • If variable a contains “bc" as a substring, echo “yes".
    • This test is a trick that uses string replacement, discussed below.
    • This test does not work if the variable contains a quotation mark.

Testing for “starts with":

  • if %a:~0,1%==a echo yes
    • If variable a starts with “a", echo “yes".
  • if %a:~0,2%==ab echo yes
    • If variable a starts with “ab", echo “yes".

String replacement:

  • set a=abcd & echo %a:c=%
    • Result: abd
  • set a=abcd & echo %a:c=e%
    • Result: abed
  • set a=abcd & echo %a:*c=%
    • Result: d
    • The asterisk only works at the beginning of the sought pattern; it does not work at the end or in the middle.

See also the help for SET command: set /?.

Splitting a string by any of " “, “,", and “;":

set myvar=a b,c;d
for %%a in (%myvar%) do echo %%a

Splitting a string by semicolon, assuming the string contains no quotation marks:

@echo off
set myvar=a b;c;d
set strippedvar=%myvar%
:repeat
for /f "delims=;" %%a in ("%strippedvar%") do echo %%a
set prestrippedvar=%strippedvar%
set strippedvar=%strippedvar:*;=%
if not "%prestrippedvar:;=%"=="%prestrippedvar%" goto :repeat

 

HTTP協議16年來的首個更新HTTP/2正式定稿

HTTP協議16年來的首個更新HTTP/2正式定稿,Google的SPDY協議上位成功

引文自數位時代 (2015/2/19)

據TNW援引IFTF HTTP工作組主席Mark Nottingham的部落格消息,萬維網(WWW)的基礎協議HTTP 16年來的首次重大升級HTTP 2正式定稿,已提交RFC Editor,開始全面標準化的工作。

作為WWW的基礎協議,HTTP的重要性不言而喻。但是自從1999年HTTP 1.1獲採用以來,雖然其間也推出過10多個草案版本,但是由於這些版本在各方面(尤其是性能方面)被認為仍有改進空間,這個基礎協議一直未進行任何重大升級。

不過各組織和企業並未放棄改進甚至取代HTTP協議的努力,業界巨頭Google就是其中之一。2012年,Google推出了基於TCP的應用層協議SPDY取speedy諧音,意謂更快),該協議基本沿用了HTTP的語義表示,同時在性能上進行了大幅改進,以減少連接數為目的,通過壓縮、多路復用以及優先級賦權來縮短網頁的加載時間,同時提高了安全性。

自提出SPDY以來,Google就展開了推動SPDY成為互聯網標準的努力。儘管HTTP/2.0標準化工作組2012年接納了SPDY作為HTTP/2的原型,但是與之競爭的包括了10多個HTTP草案版本以及微軟的HTTP Speed+Mobility等提案。Google在自己的瀏覽器Chrome上部署了SPDY,其優秀的性能表現很快獲得了用戶的青睞。儘管SPDY的採用率只有3.4%,但是由於獲得了Facebook、Yahoo!、Twitter、YouTube、Wordpress等重要網站的支持,在流量承載上SPDY分量不小。經過近3年的競爭比較,Google終於笑到了最後,SPDY成為HTTP/2的基礎,而Google也宣布將在2016年棄用SPDY,轉而支持HTTP/2。麻雀變鳳凰,搜索巨頭篡位成功。

儘管HTTP/2 的全面標準化尚未完成,但目前已有Firefox、Chrome(PC 及Android版)、Safari、Opera、iOS版Safari、Windows 8 IE 11 都已經支持HTTP/2.0。HTTP/2可以實現更快的頁面加載、更長的連接時間以及服務器推送等。而對於開發者來說,HTPP/2並不需要開發上做太多的改動,因為其使用的API與之前還是一樣的。

正反皆可插,USB 3.1 Type-C 樣式出爐

引文自:維基百科 http://zh.wikipedia.org/zh-tw/USB_3.1

USB 3.1

USB 3.1SuperSpeed+,官方全名:SuperSpeed USB 10 Gbps)是基於USB 3.0改良推出的USB連接介面的最新版本。2013年底,負責制定USB 3.0規範的組織USB 3.0 Promoter Group公佈了下一代USB 3.1介面的標準規範。2014年4月,負責USB介面規範的USB開發者論壇(USB-IF)公佈了USB 3.1連接介面設計圖,包括Type-A、Type-B以及全新設計的Type-C:標準的Type-A是目前應用最廣泛的介面方式,Micro-B則主要應用於智慧手機和平板電腦等裝置,而Type-C主要面向更輕薄、更纖細的裝置。

與USB 3.0比較

  • 供電最大100W,電壓和電流都會提高
  • 新增Type-C介面,其插座和線材不會相容原有Type-A / Type-B
  • 降低編碼損耗,從3.0的20%降低為3%,並將編碼方式改為128b/132b
  • 雖然向下相容USB 2.0/1.0,但如果要得到10Gb/s的傳輸速度仍需在主機、目標端同時具備對應的晶片才能達成。

Type-C介面

基於USB 3.1規範全新設計的USB Type-C,外觀上最大特點在於其上下端完全一致,這意味著用戶不必再區分USB正反面,如同蘋果Lightning介面一樣,兩個方向都可以插入。蘋果也正好在USB 3.1標準制定擔任貢獻者(Contributor)的角色,與其它系統廠一起主導連接器及接腳的規格。

USB Type-C介面尺寸為8.3×2.5毫米,小於當前PC的USB介面,但大於許多手機採用的尺寸6.85×1.8毫米的micro-USB介面。至於電力傳輸規格,線材標準為3A,而連接器為5A。Type-C由於不相容現有的任何類型,因此需要額外設計轉接裝置。全球首套USB 3.1主控端與裝置端原型,則由祥碩科技於2014年的USB-IF年會中發表, 但因速度僅較USB 3.0快一倍(理論值, 實際上不到一倍), 因此眾多大廠仍以主流規格USB 3.0為主及裝置端廠商也極少願意跟進投入開發對應的USB3.1產品。

USB 3.0 Promoter Group主席Brad Saunders在接受採訪時表示: 「雖然USB技術在裝置連接和裝置充電中是最受歡迎的,但是我們仍然意識到我們需要開發一個新的接頭來滿足不斷發展的設計趨勢,比如裝置尺寸和可用性的進步。新的Type-C接頭將迎合市場的需求並為未來的USB版本奠定一個基礎。」

USB Type-C 的其他特性如下:

  • 正確插入後發出聲音。
  • 通過USB Power Delivery技術,可用於3C產品充電。
  • 增進的電磁干擾與RFI mitigation特性
  • 支援1萬次拔插。

USB Type-C 還支援 DisplayPort,功能終於能與 Thunderbolt 看齊。

usb-31-reversible