# 批次檔 BAT

## 批次檔基本介紹

將命令提示字元(Command Prompt)中輸入的指令集結起來，輸入在文字檔中，用以批次執行，稱之為批次檔(Batch file)。

**請注意！**&#x547D;令提示字元預設的字碼頁為 ANSI/BIG5 編碼，因此檢視 UTF-8 編碼檔案時會出現亂碼，所以批次檔的編寫應盡量使用 ANSI 的編碼方式。

所以這邊的指令也只適用於Windows系統喔 \~

\
大大推薦的網站:(<http://steve-jansen.github.io/guides/windows-batch-scripting/index.html>)

參考資料:(<https://blog.poychang.net/note-batch/>)<br>

## 範例1

參考資料:(<https://davidhu0903ex3.pixnet.net/blog/post/443069270-%5B%E6%95%99%E5%AD%B8%5D-dos%E6%89%B9%E6%AC%A1%E6%AA%94%E8%A3%BD%E4%BD%9C---bat%E6%AA%94%E8%AA%9E%E6%B3%95>)

`@ECHO OFF` \
`CD C:\Users\brian\Documents`\
`PAUSE` \
`REM 複製到D:\底下~~~` \
`COPY *.txt C:\Users\brian\Documents\backup` \
`ECHO finish`

第一行打 "ECHO OFF"，這樣一來之後的命令都不會顯示，改成 "ECHO ON" 的話，每行輸入的命令就都會出現在螢幕上～

第二行 **CD <路徑>** 就是前往那個路徑，這是基本的 DOS 語法

第三行 **PAUSE** 會暫停，cmd 視窗就會停住等你按任意鍵繼續

第四行 **REM** 是註解用的 (remark)，DOS 會略過此行(另外 兩個冒號)`::`也可以達到同樣的效果

第五行 **COPY  \*.txt  <路徑>**，是複製所有 txt 結尾的檔案，到路徑目標資料夾那裡（ \*是萬用字元，也是基本 DOS 語法喔xD）

第六行 **ECHO finish**，會在螢幕上顯示 ECHO 後面的字串，所以就會顯示 "finish"

## 範例2(IF)

參考資料:(<https://davidhu0903ex3.pixnet.net/blog/post/443069270-%5B%E6%95%99%E5%AD%B8%5D-dos%E6%89%B9%E6%AC%A1%E6%AA%94%E8%A3%BD%E4%BD%9C---bat%E6%AA%94%E8%AA%9E%E6%B3%95>)

`@ECHO OFF` \
`IF "%1"=="A" ECHO Apple` \
`IF "%1"=="B" ECHO Boy`

輸入\
\>C:\sp.bat A&#x20;

輸出：\
Apple

假設這個程式叫做 "sp"，並且放在 C:\ 底下，

那麼，當我們在cmd 輸入 "C:\sp.bat  A" 時，參數 "A" 就會被讀到 %1 裡面，於是程式最後就印出了 "Apple"。

補充一點：最多可以傳送九個參數喔，%1 \~ %9

## 範例3(GOTO)

參考資料:(<https://davidhu0903ex3.pixnet.net/blog/post/443069270-%5B%E6%95%99%E5%AD%B8%5D-dos%E6%89%B9%E6%AC%A1%E6%AA%94%E8%A3%BD%E4%BD%9C---bat%E6%AA%94%E8%AA%9E%E6%B3%95>)

`@ECHO OFF` \
`CD C:\Users\brian\Documents` \
`IF EXIST %1 GOTO PrintFile` \
`GOTO FileNotExist` \
`:PrintFile` \
`TYPE %1` \
`GOTOEnd` \
`:FileNotExist` \
`ECHO File Not Exist!` \
`GOTO End` \
`:End`

輸入：\
C:\test.bat list.txt&#x20;

輸出：\
< list.txt 檔案裡的內容 >

假設這個程式叫做 "test.bat"，並且 "D:\David\備份BA\~1\電腦\Chrome\\" 底下有放一個叫 "list.txt" 的檔案，

那麼當輸入上面那一串內容時，就會把 txt 裡面內容都印出來。而若找不到此一檔案名稱，就會印出 "File Not Exist!"

補充一點：BAT 程式裡，所有的 "%1" 都是會被輸入參數直接取代掉的喔～

第三行，**IF EXIST <路徑> <檔名> <動作>** ；路徑如果是同個資料夾就可以忽略；其實也可以改成 **IF NOT EXIST <.....>**

第三行，**GOTO PrintFile**，效果是直接跳到第五行 ":PrintFile" 那裡執行，也就是說中間的都會略過不執行；標籤的名字可以隨便取，像是 "ABC\_NAME001" 等等

第四行，如果第三行沒有跳走的話，就會 GOTO 跳到 FileNotExist

第五\~七行，是 PrintFile 所要執行的內容；**TYPE <檔名>** 會把那個檔案印出來，eg. TYPE list.txt

第七行，執行完記得要再 GOTO 到底下的 :End，否則它會繼續往下執行第八行、第九行喔！

第八\~十行，是 FileNotExist 所要執行的內容

##


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://brianwu.gitbook.io/brian/brians-za-ji/pi-ci-dang-bat.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
