Windows批处理脚本:如果两个文件相同,则执行命令!

Mer*_*ott 11 windows compare file batch-file

我原本以为这将是一项非常简单的任务,但我现在已经苦苦挣扎了几天,而且有点沮丧!我对Windows批处理脚本不是很熟悉,所以如果你知道答案,请尽量保持简单:)

基本上,我有一个Windows关闭脚本(.bat文件),我想知道两个文本文件是否相同(即它们的内容完全相同),如果是这样,执行goto命令(例如goto line10)

我无法弄清楚如何做到这一点!非常感谢您的帮助!

Sys*_*dox 14

没有goto:

fc /b file1 file2 > nul
if errorlevel 1 (
    echo different
) else (
    echo same
)
Run Code Online (Sandbox Code Playgroud)

  • 请注意,`if errorlevel 1` [实际上意味着errorlevel为1或更多](https://blogs.msdn.microsoft.com/oldnewthing/20080926-00/?p=20743).如果其中一个文件不存在,`FC`设置errorlevel 2.所以第一种情况确实是"回声不同,或者至少有一种不存在". (2认同)

sch*_*der 10

这个脚本应该工作:

fc /b file1 file2 > nul
if errorlevel 1 goto files_differ
[files are the same, do something here]

:files_differ
[files are not the same, do something here]
Run Code Online (Sandbox Code Playgroud)