有时您需要比较两个文件并检查它们的内容是否不同。在某些情况下,您可以打开这两个文件并查看它们的内容,例如当您要比较两张图片或两个文本文件(text file)时。但是,这是一项相当繁琐的任务,您可能会错过细微的差异,例如照片色调的细微变化或文本文件(text file)中的短语。这就是为什么最好了解一些以编程方式比较两个文件的内容的方法,使用软件而不是您的感官。🙂 您可以使用PowerShell或适用于 Windows 的第三方应用程序执行此操作的三种方法:
如何使用PowerShell比较两个文件(PowerShell)
Windows 10 没有用于比较两个文件的内置工具。但是,您可以使用PowerShell来执行此操作。从打开PowerShell开始:一种快速的方法是同时按下键盘上的Win + X键,然后如果您想以标准用户身份运行PowerShell ,请按(PowerShell)I ,如果您想以管理员身份运行,请按A .
打开PowerShell后,运行以下命令:
if((Get-FileHash "第一个文件的路径").hash -eq (Get-FileHash "第二个文件的路径").hash) {"比较的两个文件是一样的"} else {"比较的两个文件不相同"}(if((Get-FileHash "Path to the first file").hash -eq (Get-FileHash "Path to the second file").hash) {"The two compared files are identical"} else {"The two compared files are NOT identical"})
当然,您应该将“第一个文件的路径”("Path to the first file")和“第二个文件("Path to the second file")的路径”更改为文件的真实路径。例如,我们想要比较两个名为DigitalCitizen1.txt和DigitalCitizen2.txt的文件,它们都位于我们的D:驱动器上。
为了比较它们的内容,我们必须运行这个命令:
if((Get-FileHash "D:DigitalCitizen1.txt").hash -eq (Get-FileHash "D:DigitalCitizen2.txt").hash) {"比较的两个文件是一样的"} else {"比较的两个文件不相同"}(if((Get-FileHash "D:DigitalCitizen1.txt").hash -eq (Get-FileHash "D:DigitalCitizen2.txt").hash) {"The two compared files are identical"} else {"The two compared files are NOT identical"})
如果两个文件的内容相同,您在PowerShell中获得的输出会告诉您“两个比较的文件是相同的”。("The two compared files are identical.")
如果两个文件的内容不同,您在PowerShell中获得的输出会告诉您“两个比较的文件不相同”。("The two compared files are NOT identical.")
尽管使用PowerShell既快速又相当容易,但有些人不喜欢使用命令行环境。因此,我们还将向您展示如何使用提供图形用户界面的第三方应用程序比较Windows中的两个文件。(Windows)
如何使用 Total Commander 比较两个文件
Total Commander是适用于所有Windows版本的共享软件文件管理器。(shareware file manager)你可以从它的官方网站上获得它,在这里:Total Commander。安装并打开它后,使用它的窗口导航到要比较的两个文件。在Total Commander(Total Commander)的左侧,导航到第一个文件,在右侧,导航到第二个文件。然后,通过单击选择这两个文件,然后按空格键。选择文件后,它们的名称将显示为红色,如下面的屏幕截图所示。
现在从Total Commander(Total Commander's)左上角的菜单中打开文件(Files)选项,然后单击“按内容比较("Compare By Content)”。
如果您选择比较的两个文件相同,Total Commander会打开一个小弹出窗口(popup window),告诉您"The two files have the same content!".
如果两个比较文件的内容不同,Total Commander会打开一个窗口,在其中显示两个文件之间的差异,以红色突出显示。
Total Commander还为您提供了检查差异和编辑两个文件的其他选项,但它们超出了本教程的范围,因此我们让您自己发现它们。
如何使用AptDiff比较两个文件(AptDiff)
一个可以帮助您按内容比较文件的免费软件应用程序是AptDiff。尽管该应用程序不再维护,但它仍然适用于Windows 2000及更高版本的所有(Windows 2000)Windows版本,包括Windows 10。您可以从Softpedia获得它。
安装并打开AptDiff后,它会立即要求您选择要比较的文件。输入(Enter)两个文件的路径或使用“...”按钮浏览您的计算机并选择它们。
然后选择是要将这两个文件作为文本文件进行比较,还是希望在二进制级别上进行比较。无论您比较的文件类型如何,您都可以选择任何选项。但是,要知道,一旦比较了文件的内容,应用程序就会在两个单独的窗格中加载它们的内容,并向您显示识别出的差异(如果有)。如果您选择“作为文本比较”("Compare as Text,"),两个窗格将显示文本内容和差异。否则,如果您选择“比较为二进制”("Compare as Binary,"),则两个文件的内容将以十六进制代码显示。
就像Total Commander一样,AptDiff还允许您使用其他工具来比较和编辑两个文件的内容。
您最喜欢按内容比较两个文件的方式是什么?
现在您知道了在Windows(Windows)中按内容比较两个文件的三种简单方法。你最喜欢他们中的哪(Which)一个?您更喜欢在PowerShell中使用命令,还是更愿意安装和使用也具有可视化界面的第三方应用程序?如果您知道其他比较文件的方法,请在下面的部分中留下评论让我们知道。
How to compare two files by content, in Windows
There are times whеn yоυ need to compare two files and check whethеr their content is different. In ѕome situatіons, you could just open the two files and look through their contents, like when you want to compare two piсtures or two text fіles. However, that's a rather tedious task to perform, аnd you might miss small differеnces, like a slight chаnge in tint on a рhoto, or a phrase in a text file. That's why it is good to know a few methods to programmаticallу compare the contеnts of two files, using softwаre instead of your senseѕ. 🙂 Here are three ways in which you can dо that, υsing PowerShell or third-party apps for Windows:
How to compare two files using PowerShell
Windows 10 doesn't have a built-in tool for comparing two files. However, you can use PowerShell to do so. Start by opening PowerShell: a quick way to do it is to press the Win + X keys on your keyboard simultaneously, and then press I if you want to run PowerShell as a standard user, or A if you want to run it as an administrator.
Once you've opened PowerShell run the following command:
if((Get-FileHash "Path to the first file").hash -eq (Get-FileHash "Path to the second file").hash) {"The two compared files are identical"} else {"The two compared files are NOT identical"}
Of course, you should change "Path to the first file" and "Path to the second file" with the real paths to your files. For example, we wanted to compare two files called DigitalCitizen1.txt and DigitalCitizen2.txt, both found on our D: drive.
To compare their contents, we had to run this command:
if((Get-FileHash "D:DigitalCitizen1.txt").hash -eq (Get-FileHash "D:DigitalCitizen2.txt").hash) {"The two compared files are identical"} else {"The two compared files are NOT identical"}
If the content of the two files is the same, the output you get in PowerShell tells you that "The two compared files are identical."
If the contents of the two files are different, the output you get in PowerShell tells you that "The two compared files are NOT identical."
Although using PowerShell is both fast and fairly easy, some people do not like using command-line environments. Because of that, we're also going to show you how to compare two files in Windows using third-party apps that offer graphical user interfaces.
How to compare two files using Total Commander
Total Commander is a shareware file manager that is available for all Windows versions. You can get it from its official website, here: Total Commander. Once you've installed and opened it, use its windows to navigate to the two files that you want to compare. In the left side of Total Commander, navigate to the first file, and on the right, navigate to the second file. Then, select the two files by clicking on them and then pressing the spacebar. When the files are selected, their name is colored in red, as you can see in the screenshot below.
Now open the Files options from Total Commander's menu, on the top-left corner, and click on "Compare By Content."
If the two files that you chose to compare are identical, Total Commander opens a small popup window in which it tells you that "The two files have the same content!".
If the contents of the two compared files differ, Total Commander opens a window in which it shows you the differences between the two files, highlighted in red.
Total Commander also gives you other options for checking the differences and editing the two files, but they are out of the scope of this tutorial, so we're letting you discover them on your own.
How to compare two files using AptDiff
A freeware app that can help you compare files by contents is AptDiff. Although the app is not maintained anymore, it still works in all Windows versions from Windows 2000 and up, including Windows 10. You can get it from Softpedia.
After you install and open AptDiff, it immediately asks you to select the files that you want to compare. Enter the paths to the two files or use the "…" buttons to browse through your computer and select them.
Then choose whether you want to compare the two files as text files, or if you prefer to compare them on a binary level. You can choose any option, regardless of the type of files that you compare. However, know that once the contents of the files are compared, the app loads their contents in two separate panes and shows you the differences identified, if any. If you choose to "Compare as Text," the two panes show the text contents and differences. Otherwise, if you choose to "Compare as Binary," the contents of the two files are shown in hexadecimal code.
Just like Total Commander, AptDiff also lets you use additional tools for comparing and editing the contents of the two files.
What is your favorite way of comparing two files by content?
Now you know three easy ways of comparing two files by content in Windows. Which of them is your favorite? Do you prefer using commands in PowerShell, or would you rather install and use a third-party app that also has a visual interface? If you know other methods to compare files, let us know by leaving a comment in the section below.