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

命令提示符查找文件

命令提示符查找文件

Find 0

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.

查找是每个Windows用户都应该知道的另一个出色的命令行工具,因为它可用于搜索文件内容中的特定文本字符串。

查找的开关和参数 (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.

与Windows中每个基于命令提示符的工具一样,为了有效地使用这些工具,您需要了解某些开关和参数。 这些在下面列出并说明。

  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.

与Windows中的每个工具一样,您将需要知道如何输入命令。 下面的语法是完美的模型。

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

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

根据您的命令,您将收到三个%errorlevel%响应之一。

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

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

    1 –找不到您要搜索的字符串。
  3. 2 – This means you had a bad switch or your parameters were incorrect.

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

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

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

在开始之前,您应该下载我们的三个样本文本文档,我们将用于测试。

  1. document

    文件

  2. sample

    样品

  3. exercise

    行使

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.

现在,您将需要打开提升的命令提示符窗口。 在Windows 7和10中打开开始菜单,或在Windows 8中打开搜索功能并搜索CMD。 接下来,右键单击它,然后按“以管理员身份运行”。 虽然您不需要打开提升的命令提示符窗口,但它将帮助您避免出现任何讨厌的确认对话框。

找到1

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.

方案1 –在单个文档中搜索一串单词。

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.

现在您已经下载了三个文档,我们将输入一个命令,在名为“ exercise”的文本文件中搜索“ martin hendrikx”一词。 使用下面显示的命令。 切记将搜索字符串放在引号中,并更改路径以匹配保存文档的文件夹。

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

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”开关,以便FIND忽略搜索字符串的大小写。

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

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.

现在您可以看到FIND弹出了与搜索字符串匹配的一行,这意味着它正在工作。 让我们再试一次,但是将搜索字符串更改为“ sushi”; 如果您的结果如下图所示,则说明操作正确。

找到4

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

方案2 –在多个文档中搜索相同的字符串。

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.

现在您知道了如何进行基本搜索,让我们尝试扩大搜索范围。 现在,我们将在两个文本文件(练习和示例)中搜索术语“寿司”。 通过输入以下字符串来执行此操作。 切记更改路径以匹配文件的位置,并添加“ / i”开关,以使搜索不区分大小写。

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

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.

您会注意到,在两个文档中都找到了搜索词,并且在它们对应的文件名和位置下列出了找到它们的句子。 再试一次,但是这次,将第三个文件添加到FIND命令中,然后搜索单词“ potato”。 您的搜索结果应如下图所示。

Find 6

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.

方案3 –计算文件中的行数。

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:

如果您想知道一个文件中有多少行,可以使用下面的搜索命令。 请记住在所有交换机之间添加一个空格。 在这种情况下,我们将路径名称替换为“ sample.txt”文件的路径名称。 如果只需要一个数字作为结果,请使用以下命令:

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

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
找到8

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
Find 9

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

图片来源:Flickr.com上的Littlehaulic

翻译自: https://www.howtogeek.com/206097/how-to-use-find-from-the-windows-command-prompt/

命令提示符查找文件

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

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值