Batch file 批量筛选令牌

Batch file 批量筛选令牌,batch-file,Batch File,我有以下问题。我正在从数据库中读取字段。这些字段并非都是必填字段。因此,并不是所有的都填写了。我遇到的问题是批处理(ms dos)和令牌功能 让我给你举个例子: 所涉及的字段如下:(示例) 当我运行此代码时: 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 F

我有以下问题。我正在从数据库中读取字段。这些字段并非都是必填字段。因此,并不是所有的都填写了。我遇到的问题是批处理(ms dos)和令牌功能

让我给你举个例子:
所涉及的字段如下:(示例)

当我运行此代码时:

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
%%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>
一切都很好。我能正确显示所有的回声。但是如果缺少这些字段中的任何一个,例如:

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
名字:约翰
姓:史密斯
地址:假街123号
邮政编码:
公司:SomeCo
部门:
楼层:4
电话:123-555-5555
手机号码:123-555-5556
我的输出如下所示:

%%a= John
%%b= Smith
%%c= 123 Fake Street
%%d= 45612
%%e= SomeCo
%%f= Accounting
%%g= 4
%%h= 123-555-5555
%%i= 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>
%%a=John
%%b=史密斯
%%c=123假街
%%d=SomeCo
%%e=4
%%f=123-555-5555
%%g=123-555-5556
%%h=
%%i=
你可以看到这是如何导致沮丧的。

我的问题是:如何确保所有
%%
始终对齐,即使该空间中的信息为空?

第一个问题是示例文本与代码不匹配。
代码以逗号分隔字符串,但示例仅使用换行符

我假设你有一个CSV

然后只需将每个字段替换为,#,因为这样就不会有任何字段为空,然后删除第一个字符

Set line=#!line:,=,#!

另一种语言,比如带有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

下面的批处理文件将jeb和William陈述的想法收集到一个真正有效的完整程序中。此程序不受文件中字段数量的限制,也不受使用选项时所需的缺少字段的位置的限制。相反,它使用描述文件字段的变量名列表,以便程序加载变量中的值(不适用于令牌)。这样,只需更改变量列表,就可以很容易地更改字段的数量、特定字段的位置或文件中的任何其他修改

@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

Antonio

你能发布实际的文件吗?在我看来好像不匹配。batch命令表示一个逗号分隔的文件。虽然没有william bettridge radford的答案那么全面,但总体思路相同,而且更快+1表示正确的方向(因为批量问题通常被低估)。您可以保护空间,但也应该保护
=
和选项卡。虽然tab不太可能成为问题。此外,包含
*
的值也将损坏。
可以被保护,但是没有好的方法用批处理替换
*
,所以我不知道如何保护它。谢谢你的帮助。我会在几天后试用你的代码。设置行=!中的C字符是什么!行:=玟!如前一行所示,它是Ascii字符#128。您可以使用文件中未显示的任何其他字符,但在这种情况下,请确保在+1下面的六行中使用相同的字符,但如果使用延迟展开删除下划线,则解决方案会更好(更安全)。在循环中执行所有操作而无需调用的速度也显著加快:
set“arg1=%%a”&set“arg1=!arg1:~0,-1!”