我有以下问题。我正在从数据库中读取字段。这些字段并非都是强制性的。因此,并非所有这些都被填写。我遇到的问题是批处理(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>
始终对齐?
答案 0 :(得分:3)
第一个问题是您的示例文本不适合您的代码 您的代码用逗号分隔字符串,但您的示例仅使用换行符。
我假设你有一张CSV。
然后你只需要用#替换每个,因为那时没有字段是空的,后来删除了第一个字符。
Set line=#!line:,=,#!
答案 1 :(得分: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
答案 2 :(得分: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
安东尼奥