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を持っていると思います。

次に、空になるフィールドがないため、 each , を ,# に置き換えるだけでよく、後で最初の文字を削除します。

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 に答える