Quick file search in Windows Command Line (cmd) (similar to the find command in Linux)
For/r usage
Using the find command in Linux makes it very convenient to search for files. Is there a similar command in windows that can traverse directories and find files through file names? The answer is:
The for/r command in Windows has a function similar to the find command in Linux, and the syntax is similar:
Find/r directory name % variable name in (matching Mode 1, matching mode 2) do command
The matching mode can be wildcard similar:
*. Jpg: All files suffixed with .jpg * test *: All files whose names contain test
Note:
1. At least 1 * is included in the matching mode.
2. The matching content is limited to the file name and does not match the directory name.
If you only search for and output file names, run the @ echo % variable name command after do, that is:
For/r directory name % I in (matching Mode 1, matching mode 2) do @ echo % I
Common examples:
# List all files in the TestDir directory and all subdirectories in C: \ Users \ cashey \ Desktop> for/r TestDir % I in (*) do @ echo % iC: \ 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 # Find all txt files C: \ Users \ cashey \ Desktop> for/r TestDir % I in (*. txt) do @ echo % iC: \ Users \ cashey \ Desktop \ TestDir \ doc \ 1.txt C: \ Users \ cashey \ Desktop \ TestDir \ doc \ 2.txt# find all txt files and jpg files in the TestDir directory and all subdirectories C: \ Users \ cashey \ Desktop> for/r TestDir % I in (*. txt ,*. jpg) do @ echo % iC: \ Users \ cashey \ Desktop \ TestDir \ B .jpg C: \ Users \ cashey \ Desktop \ TestDir \ doc \ 1.txt C: \ Users \ cashey \ Desktop \ TestDir \ doc \ 2.txt# find the file C whose file name contains test in the TestDir directory and all subdirectories: \ Users \ cashey \ Desktop> for/r TestDir % I in (* test *) do @ echo % iC: \ Users \ cashey \ Desktop \ TestDir \ src \ test. py
NotesIf the * sign is not included in the matching mode, the file name will not be exactly matched, but the matching mode will be spliced in all subdirectories, as shown below:
# In fact, the directory does not contain any file 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
The above output is generally not what you expected. At least one * number is required in the mode.