3

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

举个例子:
有问题的字段如下:(例子)

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>始终对齐,即使该空间中的信息为空白?

4

3 回答 3

3

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

我假设你有一个 CSV。

然后您只需将每个 , 替换为 ,# 因为这样就没有字段为空,然后删除第一个字符。

Set line=#!line:,=,#!
于 2012-11-13T23:33:29.880 回答
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
于 2012-11-13T23:33:57.343 回答
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

安东尼奥

于 2012-11-14T02:44:45.620 回答