我如何比较在批处理文件中的两个文件,并基于它们是否匹配的动作?我已经试过类似:

if file1.txt NEQ file2.txt goto label

,但它比较实际字符串“FILE1.TXT”而不是该文件。我读过有关COMP命令,但它似乎没有工作,如果我把它放在一个if语句。有人知道怎么做这个吗?很抱歉,但我很少使用批处理文件,并在他们缺乏经验。

预先感谢。

有帮助吗?

解决方案

我相信你可以使用“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

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

其他提示

这似乎是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.

我用下面的例子中基于文件的差异来构建报告:

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
 )
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top