批处理脚本令牌

3

我有以下问题。我正在从数据库中读取字段。这些字段不都是必填的。因此,它们并非全部填写。我遇到的问题与批处理(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>都对齐,即使该空间中的信息为空?


你可以发布实际的文件吗?在我看来,它似乎不匹配。批处理命令指示一个逗号分隔的文件。 - GolezTrol
3个回答

3
第一个问题是你的样本文本与代码不匹配。
你的代码使用逗号分隔字符串,但你的样本只使用换行符。我假设你有一个CSV文件。
那么你只需要将每个逗号替换为,#,这样就不会有空字段了,稍后再删除第一个字符即可。
Set line=#!line:,=,#!

1
虽然不如william-bettridge-radford的回答详细,但是大致思路相同,而且更快。+1因为这是正确的方向(而且批处理问题总体被低估了)。 - GolezTrol

2
另一种语言,如带有CSV库的Python,可能是最好的选择。如果您真的想要批处理,您可以暂时将另一个字符附加到每个部分,例如在每个部分末尾添加并删除下划线。
@echo off

setlocal EnableDelayedExpansion

for /f "tokens=*" %%z in (test.csv) do (
    set line=%%z
    rem append underscores
    set line=!line:,=_,!_
    for /f "tokens=1-9 delims=," %%a in ("!line!") do (
        call :remove_underscore arg1 "%%a"
        call :remove_underscore arg2 "%%b"
        call :remove_underscore arg3 "%%c"
        call :remove_underscore arg4 "%%d"
        echo arg1: '!arg1!'
        echo arg2: '!arg2!'
        echo arg3: '!arg3!'
        echo arg4: '!arg4!'
    )
    echo new line
    echo.
)
exit /b 0

:remove_underscore rval input_string
    set input_string=%~2
    set %1=%input_string:~0,-1%
    exit /b 0

+1,但如果使用延迟扩展来删除下划线,则解决方案会更好(更安全)。在循环内部完成所有操作也显著提高了速度,而无需调用:set "arg1=%%a"&set "arg1=!arg1:~0,-1!" - dbenham

1
下面的批处理文件采用了Jeb和William提出的想法,并将它们整合到一个真正可行的程序中。该程序不受文件中字段数量或缺失字段位置的限制,这是在使用“tokens=1-...”FOR选项时所要求的。相反,它使用描述文件字段的变量名称列表,因此程序会将值加载到变量中(而不是在FOR令牌中)。这样,只需更改变量列表中的内容,就可以非常容易地更改字段数量、特定字段的位置或文件中的任何其他修改。
@echo off
setlocal EnableDelayedExpansion

rem Define names for variables (with NO spaces) in a comma-separated list
set fields=FirstName,LastName,Address,PostalCode,Company,Departament,Floor,Phone,Mobile
rem Previous list may also be read from the first line (header) of a DataBase file

rem Separate the list in an array of variable names
set i=0
for %%a in (%fields%) do (
   set /A i+=1
   set name[!i!]=%%a
)
set numFields=%i%

rem Process the file
for /F "delims=" %%a in (info_file.txt) do (
   set line=%%a
   rem Replace spaces by Ascii-128 (to avoid split values that may have spaces)
   set line=!line: =Ç!
   rem Insert any char. at beginning of each field, and separate fields with spaces
   set i=0
   for %%b in (X!line:^,^= X!) do (
      set field=%%b
      rem Recover spaces in this field, if any
      set field=!field:Ç= !
      rem And assign this field to corresponding variable (removing first character)
      set /A i+=1
      for %%i in (!i!) do set !name[%%i]!=!field:~1!
   )

   rem At this point all variables have the values of current record.
   rem They may be accessed explicitly:
   echo/
   echo Record of !FirstName! !LastName!
   rem ... or implicilty via the NAME array:
   for /L %%i in (3,1,%numFields%) do (
      for %%b in (!name[%%i]!) do echo    %%b: !%%b!
   )
)

info_file.txt:

John,Smith,123 Fake Street,45612,SomeCo,Accounting,4,123-555-5555,123-555-5556
Jane,Doe,123 Fake Street,,SomeCo,,4,123-555-5555,123-555-5556

输出:

Record of John Smith
   Address: 123 Fake Street
   PostalCode: 45612
   Company: SomeCo
   Departament: Accounting
   Floor: 4
   Phone: 123-555-5555
   Mobile: 123-555-5556

Record of Jane Doe
   Address: 123 Fake Street
   PostalCode:
   Company: SomeCo
   Departament:
   Floor: 4
   Phone: 123-555-5555
   Mobile: 123-555-5556

安东尼奥


你需要保护空格,但同时也要保护;=和制表符。虽然制表符可能不太容易出问题。此外,包含*?的值将会被破坏。?可以被保护,但是没有好的方法用批处理替换*,所以我不知道它该如何被保护。 - dbenham
谢谢你的帮助。我会在几天内尝试一下Aacini的代码。在这行代码中,那个C字符是什么意思:set line=!line: =Ç! <--? - gmilic
正如之前的REM行所指示的那样,它是Ascii字符#128。您可以使用任何其他字符_不出现在文件中_,但在这种情况下,请确保在六行以下使用相同的字符。 - Aacini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接