在批处理中,无法读取" *"正确地从文件

时间:2016-06-07 14:16:06

标签: batch-file cmd

我想使用批处理脚本分析.h文件。在.h文件中有一些结构可能如下所示:

typedef struct _sample {
    int *a;
    char* b;
    float * c;
} Sample;

如你所见," *"可以是数据类型和成员名称之间的任何位置。下面是我在.bat文件中写的内容(简化):

for /F "tokens=*" %%i in (sample.h) do (
    for %%j in (%%i) do echo %%j
)

这个.bat做的是回显它读取的所有单词,因为我简化了它。结果是:

typedef
struct
_sample
{
int         ::"*a" is skipped
b           ::"char*" is skipped
float
sample.bat  ::"*" is solved as reading current path files
sample.h
c
}
Sample

所以问题是:如何用" *"分析这3种类型的指针声明?正确?

3 个答案:

答案 0 :(得分:1)

aschipfl说,EXTRACT(DAY FROM (tev.date_return - tev.date_taken)) as day_differencefor*解释为通配符,并将它们扩展为匹配的文件名。要绕过它,您必须解释?循环之外的字符串:

for

答案 1 :(得分:0)

如果您尝试解析C代码,则应使用更稳定的版本 因为FOR / F将删除以;开头的空行和行。

此外,线路本身的拆分可以通过换行以安全的方式完成。

setlocal DisableDelayedExpansion
(set \n=^
%=EMPTY=%
)
for /F "usebackq delims=" %%L in (`findstr /n "^" t2.txt`) do (
    set "line=%%L"
    setlocal EnableDelayedExpansion
    set "line=!line:*:=!"
    echo LINE: !line!
    FOR %%N in ("!\n!") do set "parts= !line: =%%~N !"
    FOR /F "delims=" %%p in ("!parts!") do (
        echo(   %%p
    )
    endlocal
)

答案 2 :(得分:-1)

根据for /?输出:

tokens=x,y,m-n  - specifies which tokens from each line are to
                  be passed to the for body for each iteration.
                  This will cause additional variable names to
                  be allocated.  The m-n form is a range,

请注意这一部分:

                  specifying the mth through the nth tokens.  If
                  the last character in the tokens= string is an
                  asterisk, then an additional variable is
                  allocated and receives the remaining text on
                  the line after the last token parsed.
相关问题