如何比较批处理文件中的两个文件,并根据它们是否匹配执行操作?我尝试过类似的东西:
if file1.txt NEQ file2.txt goto label
但它比较实际的字符串“file1.txt”而不是文件。我已经阅读了关于COMP命令的内容,但如果我把它放在if语句中它似乎不起作用。有人知道怎么做这个吗?抱歉,我很少使用批处理文件,对它们的经验很少。
提前致谢。
答案 0 :(得分:29)
我相信您可以使用“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拉出来)
答案 1 :(得分:3)
似乎COMP程序实际上相当容易使用。请参阅有关雅虎答案的this question。
请注意,运行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.
答案 2 :(得分: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
)