﻿# 如何在批次檔(Batch)中實現 sleep 命令讓任務暫停執行 n 秒

在批次檔(\*.bat)中內建並沒有 SLEEP 命令，當你在執行批次任務時若需要暫停執行幾秒鐘，就需要一些小技巧來實現了，以下分享幾個我之前用過的技巧：

**1\. 利用 PING 指令幫忙停 5 秒**

   每壹台電腦都有 PING 執行檔，這個最好用啦!

```
@ping 127.0.0.1 -n 5 -w 1000 > nul
```

**2\. 利用 CHOICE 指令**

   CHOICE 命令在 Windows XP 中找不到，但在 Windows Server 2003 或 Vista 都有內建。

```
@CHOICE /C YN /N /T 5 /D y > nul
```

**3\. 安裝** [**Windows Server 2003 Resource Kit Tools**](http://web.archive.org/web/20120101053958/http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17657) **即可獲得 sleep.exe 工具**

   預設安裝路徑在 **C:\\Program Files\\Windows Resource Kits\\Tools** 目錄下會有個 sleep.exe 執行檔

```
sleep 5
```

**4\. 利用 TIMEOUT 指令**

   TIMEOUT 命令在 Windows Server 2003 或 Vista 之後都有內建。

```
timeout /t 5
```

**相關連結**

-   [Windows Server 2003 Resource Kit Tools](http://web.archive.org/web/20120101053958/http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17657)
-   Implementing the WAIT Command in a Batch File
-   Batch file SLEEP Command
-   [Batch files - The CHOICE command](http://www.robvanderwoude.com/choice.php)
