Introduction to FOR/R Usage
Using the Find command under Linux, found it convenient to find files, then there are no similar commands under Windows can traverse the directory and find the file by file name? The answer is:
Commands under Windows for /r
have features similar to those under the Linux find
command, using syntax similar to the following:
find /r 目录名 %变量名 in (匹配模式1,匹配模式2) do 命令
The matching pattern can be a wildcard similar to:
*.jpg
: All files of the. jpg suffix
*test*
: All files with the test in the name
Attention:
1. Take at least 1 * numbers in the matching mode
2. Match content is limited to file name, does not match directory name
If only the lookup and output file names are available, the command behind do can be used @echo %变量名
, i.e.:
for /r 目录名 %i in (匹配模式1,匹配模式2) do @echo %i
Examples of common use:
# List all the files in the TestDir directory and all subdirectoriesC:\users\cashey\desktop> for/R TestDir%iinch(*) 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# Find all txt files in the TestDir directory and all subdirectoriesC:\users\cashey\desktop> for/R TestDir%iinch(*.txt) Do@Echo%ic:\users\cashey\desktop\testdir\doc\1. txtc:\users\cashey\desktop\testdir\doc\2. txt# Find all txt and jpg files in the TestDir directory and all subdirectoriesC:\users\cashey\desktop> for/R TestDir%iinch(*.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# Find all files in the TestDir directory and all subdirectories that contain the test file nameC:\users\cashey\desktop> for/R TestDir%iinch(*test*) Do@Echo%ic:\users\cashey\desktop\testdir\src\test.py
Points to be aware of
If the match pattern does not take the * number and does not exactly match the file name, the result of the matching pattern is printed on all subdirectories, as follows:
# # 目录中事实上不包含任何名称为abc的文件C:\Users\cashey\Desktop>forindo @echo %iC:\Users\cashey\Desktop\TestDir\abcC:\Users\cashey\Desktop\TestDir\doc\abcC:\Users\cashey\Desktop\TestDir\src\abc
The above output is generally not expected, you need to take at least 1 * numbers in the pattern
Quick Find files under Windows command Line (CMD) (similar to the Linux find command)