Windows命令列(cmd)下快速尋找檔案(類似Linux下find命令),快速尋找find

來源:互聯網
上載者:User

Windows命令列(cmd)下快速尋找檔案(類似Linux下find命令),快速尋找find
for /r 用法簡介

用了Linux下的find命令,覺得尋找檔案很方便,那麼在windows下有沒有類似的命令可以遍曆目錄並通過檔案名稱找到檔案呢?答案是有:

Windows下的 for /r 命令具有與Linux下 find 命令類似的功能,使用文法上類似:

find /r 目錄名 %變數名 in (匹配模式1,匹配模式2) do 命令

匹配模式可以是通配類似於:

  • *.jpg:所有.jpg尾碼的檔案
  • *test*:所有名稱中包含test的檔案

注意:
1. 匹配模式中至少帶上1個*號
2. 匹配內容僅限於檔案名稱,不會匹配目錄名

如果僅是尋找和輸出檔案名,do後面的命令可以用@echo %變數名,即:

for /r 目錄名 %i in (匹配模式1,匹配模式2) do @echo %i
常用的例子:
# 將TestDir目錄及所有子目錄中所有的檔案列舉出來C:\Users\cashey\Desktop>for /r TestDir %i in (*) do @echo %iC:\Users\cashey\Desktop\TestDir\b.jpgC:\Users\cashey\Desktop\TestDir\c.pngC:\Users\cashey\Desktop\TestDir\doc\1.txtC:\Users\cashey\Desktop\TestDir\doc\2.txtC:\Users\cashey\Desktop\TestDir\src\test.py# 在TestDir目錄及所有子目錄中找出所有的txt檔案C:\Users\cashey\Desktop>for /r TestDir %i in (*.txt) do @echo %iC:\Users\cashey\Desktop\TestDir\doc\1.txtC:\Users\cashey\Desktop\TestDir\doc\2.txt# 在TestDir目錄及所有子目錄中找出所有的txt及jpg檔案C:\Users\cashey\Desktop>for /r TestDir %i in (*.txt,*.jpg) do @echo %iC:\Users\cashey\Desktop\TestDir\b.jpgC:\Users\cashey\Desktop\TestDir\doc\1.txtC:\Users\cashey\Desktop\TestDir\doc\2.txt# 在TestDir目錄及所有子目錄中找出所有檔案名稱中包含test的檔案C:\Users\cashey\Desktop>for /r TestDir %i in (*test*) do @echo %iC:\Users\cashey\Desktop\TestDir\src\test.py
需要注意的點

匹配模式中如果不帶*號,並不會精確匹配檔案名稱,而會輸出所有子目錄拼接上該匹配模式的結果,像下面這樣:

# # 目錄中事實上不包含任何名稱為abc的檔案C:\Users\cashey\Desktop>for /r TestDir %i in (abc) do @echo %iC:\Users\cashey\Desktop\TestDir\abcC:\Users\cashey\Desktop\TestDir\doc\abcC:\Users\cashey\Desktop\TestDir\src\abc

上面這樣的輸出一般不是你預期的,模式裡至少需要帶上1個*號

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.