使用if Batch File将数组变量与变量合并

时间:2016-04-01 04:51:47

标签: arrays batch-file

我有一点for循环:

for /f "delims=: tokens=*" %%e in (include#.forms) do (
set/a C+=1
set In!C!=%%e
)

但是我想用另一个for循环来读它:

for /l %%N in (1 1 !C!) do (
for /f "delims=: tokens=*" %%a in (!In!C!!) do.....

但是你如何使用数组中已经使用的特殊变量和变量。因为,!在!! C!会结果!在!和!C!没有合并但是分开了。

1 个答案:

答案 0 :(得分:0)

我自己发现了这个哈哈。合并到变量ex:

!%array_row%:%arrow_id%!

我们希望回应for /f %%x in ("!array_row!") do for /f %%y in ("!array_id!") do echo !%%x:%%y! ,但这不起作用。我们将使用for语句:

%%
首先执行

!1:1!个变量,以便我们使用它。回显之前的输出将为set file1_name=Text.txt set file2_name=Run.bat

我想要这个的原因是因为我正在处理数组ex:

for /L %%C in (1 1 2) do echo !file%%C_name!

我想比较用户输入是否等于要运行的文件(实际上不是文件)。为此,我们将(回应他们)

void encode_out(const char *s){
    for(;;++s){
        char ch = *s;
        if(ch == '\0')
            break;
        if(isalpha(ch)){
            ch = toupper(ch);
            fputs(table[ALPHA][ch - 'A'], stdout);//`-'A'` depend on the sequence of character code
        } else if(isdigit(ch))
            fputs(table[NUM][ch - '0'], stdout);
        else if(ch == ' ')
            fputc('/', stdout);//need rest space skip ?
        else 
            ;//invalid character => ignore
        fputc(' ', stdout);
    }
    fputc('\n', stdout);
}

static void decode_out_aux(MTree *tree, const char *s){
    if(tree == NULL) return;
    if(*s == '\0')
        fputc(tree->value, stdout);
    else if(*s == '/')
        fputc(' ', stdout);
    else if(*s == '.')
        decode_out_aux(tree->dot, ++s);
    else if(*s == '-')
        decode_out_aux(tree->bar, ++s);
}

void decode_out(const char *s){
    char *p;
    while(*s){
        p = strchr(s, ' ');
        if(p){
            if(p-s != 0){
                char code[p-s+1];
                memcpy(code, s, p-s);
                code[p-s]='\0';
                decode_out_aux(root, code);
            }
            s = p + 1;
        } else {
            decode_out_aux(root, s);
            break;
        }
    }
    fputc('\n', stdout);
}

static void insert_aux(MTree **tree, char ch, const char *s){
    if(*tree == NULL)
        *tree = calloc(1, sizeof(**tree));
    if(*s == '\0')
        (*tree)->value = ch;
    else if(*s == '.')
        insert_aux(&(*tree)->dot, ch, ++s);
    else if(*s == '-')
        insert_aux(&(*tree)->bar, ch, ++s);
}

static inline void insert(char ch, const char *s){
    if(*s == '.')
        insert_aux(&root->dot, ch, ++s);
    else if(*s == '-')
        insert_aux(&root->bar, ch, ++s);
}

void make_tree(void){
    root = calloc(1, sizeof(*root));
    //root->value = '/';//anything
    int i;
    for(i = 0; i < 26; ++i)
        insert('A'+i, table[ALPHA][i]);
    for(i = 0; i < 10; ++i)
        insert('0'+i, table[NUM][i]);
}

static void drop_tree_aux(MTree *root){
    if(root){
        drop_tree_aux(root->dot);
        drop_tree_aux(root->bar);
        free(root);
    }
}

void drop_tree(void){
    drop_tree_aux(root);
}

由于它是一个数组,我们使用for / L(循环)而不是for / f(读取)。两者都使用%%变量。

相关问题