我有以下问题。我正在从数据库中读取字段。这些字段不都是必填的。因此,它们并非全部填写。我遇到的问题与批处理(ms dos)和 Tokens 函数有关。
让我举个例子:
涉及的字段如下:(示例)
First Name: John
Last Name: Smith
Address: 123 Fake Street
Postal Code: 45612
Company: SomeCo
Department: Accounting
Floor: 4
Phone: 123-555-5555
Mobile: 123-555-5556
当我运行这段代码时:
FOR /F "tokens=1-9, delims=," %%a in (info_file.txt) DO echo %%a, %%b, %%c, %%d, %%e, %%f, %%g, %%h, %%i
输出将类似于这样:
%%a= John
%%b= Smith
%%c= 123 Fake Street
%%d= 45612
%%e= SomeCo
%%f= Accounting
%%g= 4
%%h= 123-555-5555
%%i= 123-555-5556
一切都很好。我成功地显示了所有回声。但是!如果其中任何一个字段缺失,例如:
First Name: John
Last Name: Smith
Address: 123 Fake Street
Postal Code: <missing info; consider this line blank>
Company: SomeCo
Department: <missing info; consider this line blank>
Floor: 4
Phone: 123-555-5555
Mobile: 123-555-5556
我的输出将会是这样的:
%%a= John
%%b= Smith
%%c= 123 Fake Street
%%d= SomeCo
%%e= 4
%%f= 123-555-5555
%%g= 123-555-5556
%%h= <not used; Because there is not enough lines available>
%%i= <not used; Because there is not enough lines available>
你可以看到这可能会导致沮丧。
我的问题是:我该如何确保所有%%<variables>
都对齐,即使该空间中的信息为空?