如何比较批处理文件中的两个文件?

时间:2023-01-15 12:36:41

How can I compare two files in a batch file, and perform an action based on whether or not they match? I've tried something like:

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

if file1.txt NEQ file2.txt goto label

but it compares the actual string "file1.txt" rather than the file. I've read about the COMP command, but it doesn't seem to work if I put it in an if statement. Does anybody know how to do this? Sorry, but I rarely use batch files and have little experience in them.

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

Thanks in advance.

提前致谢。

3 个解决方案

#1


28  

I believe you can use the "FC" command and then check the errorlevel. Here's some code:

我相信你可以使用“FC”命令,然后检查errorlevel。这是一些代码:

@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

(Pulled from http://www.computing.net/answers/dos/batch-file-command/15753.html)

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

#2


3  

It seems like the COMP program is actually fairly easy to use. See this question on Yahoo answers.

似乎COMP程序实际上相当容易使用。关于雅虎答案,请参阅此问题。

Note that running comp /? will print the help text for the program (as does specifying the /? argument with any native Windows command line program). This outputs the same text you see in the answer of the question linked above.

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

Content from the Yahoo Answer:

来自雅虎的内容答案:

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.

#3


0  

I use the below example to build reports based on file differences:

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

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
 )

#1


28  

I believe you can use the "FC" command and then check the errorlevel. Here's some code:

我相信你可以使用“FC”命令,然后检查errorlevel。这是一些代码:

@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

(Pulled from http://www.computing.net/answers/dos/batch-file-command/15753.html)

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

#2


3  

It seems like the COMP program is actually fairly easy to use. See this question on Yahoo answers.

似乎COMP程序实际上相当容易使用。关于雅虎答案,请参阅此问题。

Note that running comp /? will print the help text for the program (as does specifying the /? argument with any native Windows command line program). This outputs the same text you see in the answer of the question linked above.

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

Content from the Yahoo Answer:

来自雅虎的内容答案:

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.

#3


0  

I use the below example to build reports based on file differences:

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

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
 )