使用批处理文件比较两个C文件中的#defines

时间:2015-08-17 18:55:32

标签: batch-file

我有2个文件,内容如

Fil1.c
#define V1 2
...
int Var[V1] ;
******************************
Fil2.c
#define V2 3
...
int Var[V2];

我需要在批处理文件中比较V1和V2,并且 如果他们不匹配则抛出错误 使用 Findstr 会在单个文件中提供多个行匹配 并且不会从整个数据行中提取数值。 关于如何以有效的方式做到这一点的任何想法,而不需要外部程序来做到这一点? 由于
sedy

2 个答案:

答案 0 :(得分:0)

@echo off

set "file1=c:\file1.c"
set "file2=c:\file.c"

fc "%file1%" "%file2%" && (
  echo files are the same
  color
) || (
  echo files are different
)

答案 1 :(得分:0)

@echo off

rem Get value of *first* #define in both Fil1.c and Fil2.c
for /F "tokens=3" %%a in ('findstr "^#define" Fil1.c') do set "Fil1=%%a" & goto continue1
:continue1
for /F "tokens=3" %%a in ('findstr "^#define" Fil2.c') do set "Fil2=%%a" & goto continue2
:continue2

if "%Fil1%" neq "%Fil2%" (
   echo They don't match"
)

如果您想要比较Fil1.c中的 V1和Fil2.c中的V2

@echo off

for /F "tokens=3" %%a in ('findstr /B /C:"#define V1" Fil1.c') do set "Fil1=%%a" & goto continue1
:continue1
for /F "tokens=3" %%a in ('findstr /B /C:"#define V2" Fil2.c') do set "Fil2=%%a" & goto continue2
:continue2

if "%Fil1%" neq "%Fil2%" (
   echo They don't match"
)