cmd find命令用法


Windows命令行(cmd)下快速查找文件(類似Linux下find命令)

版權聲明:本文為博主原創文章,遵循 CC 4.0 by-sa 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接: https://blog.csdn.net/Cashey1991/article/details/44993403

for /r 用法簡介

用了Linux下的find命令,覺得查找文件很方便,那么在windows下有沒有類似的命令可以遍歷目錄並通過文件名找到文件呢?答案是有:

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

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

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

  • *.jpg:所有.jpg后綴的文件
  • *test*:所有名稱中包含test的文件

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

如果僅是查找和輸出文件名,do后面的命令可以用@echo %變量名,即:

for /r 目錄名 %i in (匹配模式1,匹配模式2) do @echo %i
  • 1

常用的例子:

# 將TestDir目錄及所有子目錄中所有的文件列舉出來 C:\Users\cashey\Desktop>for /r TestDir %i in (*) do @echo %i C:\Users\cashey\Desktop\TestDir\b.jpg C:\Users\cashey\Desktop\TestDir\c.png C:\Users\cashey\Desktop\TestDir\doc\1.txt C:\Users\cashey\Desktop\TestDir\doc\2.txt C:\Users\cashey\Desktop\TestDir\src\test.py # 在TestDir目錄及所有子目錄中找出所有的txt文件 C:\Users\cashey\Desktop>for /r TestDir %i in (*.txt) do @echo %i C:\Users\cashey\Desktop\TestDir\doc\1.txt C:\Users\cashey\Desktop\TestDir\doc\2.txt # 在TestDir目錄及所有子目錄中找出所有的txt及jpg文件 C:\Users\cashey\Desktop>for /r TestDir %i in (*.txt,*.jpg) do @echo %i C:\Users\cashey\Desktop\TestDir\b.jpg C:\Users\cashey\Desktop\TestDir\doc\1.txt C:\Users\cashey\Desktop\TestDir\doc\2.txt # 在TestDir目錄及所有子目錄中找出所有文件名中包含test的文件 C:\Users\cashey\Desktop>for /r TestDir %i in (*test*) do @echo %i C:\Users\cashey\Desktop\TestDir\src\test.py

需要注意的點

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

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

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM