17

如何比较批处理文件中的两个文件,并根据它们是否匹配执行操作?我试过类似的东西:

if file1.txt NEQ file2.txt goto label

但它比较的是实际字符串“file1.txt”而不是文件。我已经阅读了 COMP 命令,但如果我将它放在 if 语句中,它似乎不起作用。有人知道怎么做这个吗?抱歉,但我很少使用批处理文件,并且对它们没有什么经验。

提前致谢。

4

3 回答 3

33

我相信您可以使用“FC”命令然后检查错误级别。这是一些代码:

@echo off
:main
fc c:\filename r:\filemame > nul
if errorlevel 1 goto error

:next
echo insert next CD
pause
goto main

:error
echo failed check

(来自http://www.computing.net/answers/dos/batch-file-command/15753.html

于 2009-03-22T21:35:40.377 回答
4

看起来 COMP 程序实际上相当容易使用。在雅虎答案上看到这个问题。

请注意,运行comp /?将打印程序的帮助文本(/?与使用任何本机 Windows 命令行程序指定参数一样)。这会输出您在上面链接的问题的答案中看到的相同文本。

来自雅虎答案的内容:

C:\>comp /? 
Compares the contents of two files or sets of files. 

COMP [data1] [data2] [/D] [/A] [/L] [/N=number] [/C] [/OFF[LINE]] 

data1 Specifies location and name(s) of first file(s) to compare. 
data2 Specifies location and name(s) of second files to compare. 
/D Displays differences in decimal format. 
/A Displays differences in ASCII characters. 
/L Displays line numbers for differences. 
/N=number Compares only the first specified number of lines in each file. 
/C Disregards case of ASCII letters when comparing files. 
/OFF[LINE] Do not skip files with offline attribute set. 

To compare sets of files, use wildcards in data1 and data2 parameters.
于 2009-03-22T21:32:39.510 回答
0

我使用以下示例根据文件差异构建报告:

set %Batch_Work_Space_Dir%=folder for your batch file and temp resource files
set file_1=name of file
set file_2=name of file

fc %file_1% %file_1%t > %Batch_Work_Space_Dir%\Are_They_Different.txt

powershell -command "(Get-Content %Batch_Work_Space_Dir%\Are_They_Different.txt) | select -skip 1 | Set-Content %Batch_Work_Space_Dir%\Are_They_Different.txt"
set /p Diff_Found=<%Batch_Work_Space_Dir%\Are_They_Different.txt
if %Diff_Found:~0,17%" == "FC: no difference" (
  execute commands
 )
于 2017-12-05T23:18:34.617 回答