使用批处理文件在两个C文件中比较#define [英] compare #defines in two C files using a batch file

查看:93
本文介绍了使用批处理文件在两个C文件中比较#define的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件,其内容类似

I have 2 files with content like

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

我需要在批处理文件中比较V1和V2,然后 如果它们不匹配,则会引发错误.
使用 Findstr 将在单个文件中提供多行匹配 并且不会从整个数据行中提取数值. 关于如何有效地做到这一点的任何想法,而无需调用外部程序来做到这一点? 谢谢
赛迪

I need to compare V1 with V2 in a batch file, and throw an error if they don't match.
Using Findstr would give multiple line matches in single file and would not extract numerical value from entire line of data. Any ideas on how to do that in an efficient manner without calling external program to do that?
Thanks
sedy

推荐答案

@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:

If you want to compare precisely V1 in Fil1.c and V2 in Fil2.c:

@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"
)

这篇关于使用批处理文件在两个C文件中比较#define的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆