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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5124243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:17:16  来源:igfitidea点击:

Windows batch script: Perform a command if two files are the same!

windowsfilebatch-filecompare

提问by Merott

I would have thought this was going to be a very simple task but I have been struggling with it for a couple of days now, and slightly frustrated! I am not very familiar with Windows batch scripts so please if you know the answer, keep it as simple as possible :)

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

Basically, I have a Windows Shutdown script (.bat file) in which I would like to know if two text files are the same (i.e. their content is exactly the same), and if so, perform a goto command (e.g. goto line10)

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

I can't figure out how to do this! Your help is much appreciated!

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

采纳答案by schnaader

This script should work:

这个脚本应该可以工作:

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]

回答by SystemParadox

Without the goto:

没有转到:

fc /b file1 file2 > nul
if errorlevel 1 (
    echo different
) else (
    echo same
)