# 命令提示符查找文件_如何使用Windows命令提示符中的查找

Find is another great command line tool that every Windows user should know about because it can be used to search content of files for specific strings of text.

## 查找的开关和参数 (Find’s Switches and Parameters)

As with every command prompt based tool in Windows, there are certain switches and parameters you will need to know in order to use the tools effectively. These are listed and explained below.

1. /v – This switch will show any lines that don’t contain the string of words you specified.

/ v –此开关将显示任何不包含您指定的单词字符串的行。
2. /c – This switch tells the find tool to count how many lines contain your search terms.

/ c –此开关告诉查找工具计算包含搜索词的行数。
3. /n – This switch shows the numbers that correspond with the lines.

/ n –此开关显示与行相对应的数字。
4. /i – This switch tells find to ignore the case of text you are searching for.

/ i –此开关告诉find忽略正在搜索的文本的大小写。

In addition to these switches, there are two parameters which you can specify with this tool.

1. “String” – The string will be the words you are searching for in your documents. You must always remember to keep this secrtion surrounded by quotation marks, otherwise your command will return an error.

“字符串” –字符串将是您在文档中搜索的单词。 您必须始终记住将此部分用引号引起来，否则您的命令将返回错误。

2. Pathname – This parameter is where you will specify the location that you want to search. This can be as broad as listing a drive or as specific as defining a single or multiple files. If you don’t specify a path, FIND will ask you for text input or may accept text piped from another command. When you are ready to end the manual text input, you can press “Ctrl + Z.” We will discuss this more later.

路径名 –此参数是您要在其中指定要搜索的位置的位置。 这可以像列出驱动器一样广泛，也可以像定义单个或多个文件一样具体。 如果您未指定路径，FIND会要求您输入文本，或者可以接受从其他命令传递来的文本。 准备结束手动文本输入时，可以按“ Ctrl + Z”。 我们将在后面讨论。

## 查找的语法 (Find’s Syntax)

Like every tool in windows, you will need to know how to enter your commands. The syntax below is the perfect model.

FIND [SWITCH] "String" [Pathname/s]

FIND [SWITCH] "String" [Pathname/s]

Depending on your command, you will receive one of three %errorlevel%  responses.

1. 0 – The string you were searching for was found.

0 –找到要搜索的字符串。
2. 1 – The string you were searching for was not found.

1 –找不到您要搜索的字符串。

2 –这表示您的开关错误或参数不正确。

## 让我们来练习 (Let’s Practice)

Before we get started, you should download our three sample text documents which we will use for the test.

These documents each contain a paragraph of text with a few similar word groupings. Once you have downloaded these three documents, you can copy them into any folder on your computer. For the purposes of this tutorial, we will put all three text documents on the desktop.

Now you will need to open up an elevated command prompt window. Open the start menu in Windows 7 and 10 or open the search function in Windows 8 and search for CMD. Next, right-click on it and then press “Run as administrator.” While you don’t need to open an elevated command prompt window, it will help you to avoid any pesky confirmation dialog boxes.

Our tutorial today will cover several simple scenarios which will be elaborated on below.

1. Search a single document for a string of words.

在单个文档中搜索一串单词。
2. Search multiple documents for the same string of words.

在多个文档中搜索相同的字符串。
3. Count the number of lines in a file or multiple files.

计算一个文件或多个文件中的行数。

Scenario 1 – Search a single document for a string of words.

Now that you have your three documents downloaded, we will enter a command to search the text file called “exercise” for the words “martin hendrikx.” Use the command shown below. Remember to put your search string in quotation marks and change the path to match the folder where your documents are saved.

find "martin hendrikx" C:\Users\Martin\Desktop\exercise.txt


You will notice that no results showed up. Don’t worry, you did nothing wrong. The reason you have no results is because FIND is looking for an exact match to your search string. Let’s try it again, but this time, let’s add the “/i” switch so that FIND ignores the case of your search string.

find /i "martin hendrikx" C:\Users\Martin\Desktop\exercise.txt

find /i "martin hendrikx" C:\Users\Martin\Desktop\exercise.txt

Now you can see that FIND brought up one line that matches the search string, which means it is working. Let’s try this again, but change the search string to “sushi”; if your results look like the image below, you did it right.

Scenario 2 – Search multiple documents for the same string of words.

Now that you know how to do a basic search, let’s try to widen the span of the search. We will now search two of the text files (exercise and sample) for the term “sushi.” Do this by entering the following string. Remember to change the path to match the location of your files and add the “/i” switch so that your search is not case-sensitive.

find /i "sushi" C:\Users\Martin\Desktop\exercise.txt C:\Users\Martin\Desktop\sample.txt


You will notice that the search terms were found in both documents and the sentences in which they were found, are listed under their corresponding file names and locations. Try this again, but this time, add the third file to the FIND command and search for the word “potato” instead. Your search results should look like the image below.

Note that the text found in each document is actually “potatoes” which means that even if you type a part of a word, you will see any phrases that contain the search string. Alternatively, you could use this command to check all text files.

find /i "sushi" C:\Users\Martin\Desktop\*.txt

find /i "sushi" C:\Users\Martin\Desktop\*.txt

Scenario 3 – Count the number of lines in a file.

If you want to know how many lines there are in a file, you can use the search command below. Remember to add a space between all your switches. In this case, we will replace the pathname with that of the “sample.txt” file. If you want only a number as your result, use this command:

type C:\Users\Martin\Desktop\sample.txt| find "" /v /c


If you want the number and the file info, use this command:

find /v /c “” C:\Users\Martin\Desktop\sample.txt

find /v /c “” C:\Users\Martin\Desktop\sample.txt

If you want to count the lines in multiple files on the desktop, use the following command.

find /v /c “” C:\Users\Martin\Desktop\*.txt


You can now experiment with a few different commands and familiarize yourself with the tool. It can help to save a lot of time in the future once you have a system created. Have fun and keep on geeking.

Image Credit: Littlehaulic on Flickr.com

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

09-23 2846
02-15 4866

02-03 544
10-05 8325
04-09 2万+
11-22 809
03-15 3495
08-22 262
08-04 1895
04-20 36
05-31 377

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。