Windows 如何用where找檔案

在 Windows 11 中,where 是用來搜尋檔案路徑的命令。它支援遞迴搜尋、環境變數、顯示檔案大小與修改時間等功能。以下是完整參數說明與範例組合。


🧭 基本語法


where [/r <Dir>] [/q] [/f] [/t] [$<ENV>:|<Path>:]<Pattern>[...]

🛠️ 參數解釋

參數 說明
/r <Dir> 從指定目錄開始,遞迴搜尋所有子目錄
/q 不顯示結果,只回傳 退出碼(0=找到,1=未找到)
/f 將結果用 引號括起來(方便處理含空格路徑)
/t 顯示檔案的 大小與最後修改時間
$<ENV>:<Pattern> 使用環境變數指定搜尋路徑,例如 $PATH:notepad.exe
<Path>:<Pattern> 指定目錄搜尋,例如 C:\Windows:notepad.exe
* / ? 支援萬用字元,例如 *.txtfile?.exe
/? 顯示說明文件

📦 常見參數組合與範例

1. 🔍 搜尋目前目錄與 PATH 中的檔案

where notepad.exe

→ 搜尋 notepad.exe 是否存在於目前目錄或 PATH 中。

2. 🔁 遞迴搜尋整個磁碟

where /r C:\ test.txt

→ 在 C:\ 及其所有子目錄中搜尋 test.txt

3. 📏 顯示檔案大小與修改時間

where /t *.log

→ 顯示所有 .log 檔案的路徑、大小與修改時間。

4. 🧵 結果加引號(處理空格路徑)

where /f "My File.txt"

→ 顯示 "C:\Users\Juiwen\Documents\My File.txt"(加引號)。

5. 🧪 測試檔案是否存在(不顯示結果)

where /q config.ini
echo %errorlevel%

→ 若找到則 %errorlevel% 為 0,否則為 1。

6. 🌐 使用環境變數指定搜尋路徑

where $PATH:notepad.exe

→ 在 PATH 指定的路徑中搜尋 notepad.exe

7. 📂 指定目錄搜尋(非遞迴)

where C:\Windows:notepad.exe

→ 只在 C:\Windows 目錄中搜尋 notepad.exe


📌 注意事項

  • 若未指定副檔名,會自動套用 PATHEXT 中的副檔名(如 .EXE, .BAT)。
  • $ENV:<Path>: 的語法 不可與 /r 同時使用
  • 支援多個搜尋模式,例如:
    where /r D:\ *.cpp *.h
    

Also See

File5FileLast Modified
Windows PowerShell Execution PolicyWindows PowerShell Execution Policy1:26 AM - December 07, 2025
windows batch file with path namewindows batch file with path name1:26 AM - December 07, 2025
UntitledUntitled1:25 AM - December 07, 2025
Everything極高速Windows 檔案搜尋工具Everything極高速Windows 檔案搜尋工具1:25 AM - December 07, 2025
Windows多種命令列工具搜尋檔案Windows多種命令列工具搜尋檔案8:21 PM - December 06, 2025