获取csv文件的动态列的批处理脚本问题?

时间:2014-01-15 09:46:00

标签: windows batch-file

这是我用来从csv导出数据并写入.trn扩展名文件的脚本。代码执行某些csv文件数据的权利,我的意思是说如果列没有索引A到Z,代码执行正确,但是当Z列意味着AA之后,AB ect数据出错。你可以在这里看到的代码:

批处理脚本

@echo off
   set "line1=^%%a,cf,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,,,,,,,,,,,,,"
   set "line2=^%%a,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,qbconid,,,^%%b,,,,,,,,,"
   .......................................................................................
   .......................................................................................
   set "line26=^%%a,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,txtstate,,,^%%z,,,,,,,,,"
   set "line27=^%%a,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,pmtper,,,^%%aa,,,,,,,,,"
   set "line28=^%%a,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,minfee,^%%ab,,,,,,,,,"
(for /F "tokens=1-28 skip=1 delims=," %%a in ('type "NewPortfolios.csv"') do ( 
        echo %line1%
        echo %line2%
        ............
        ............
        echo %line26%
    echo %line27%
    echo %line28%

    )) >"file.trn"

csv文件数据为:

 A               B             C               AA           AB
$portcode   $qbconId    $name     ....... $pmtper   #minfee
asingh12    123456789   Ajay Singh....... ajay           123

输出为:

asingh12,cf,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,,,,,,,,,,,,,
asingh12,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,qbconid,,,123456789,,,,,,,,,
...................................................................................
asingh12,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,pmtper,,,asingh12a,,,,,,,,,
asingh12,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,minfee,asingh12b,,,,,,,,,

在上面两行中,您可以看到列值出错。 正如你可以看到CSV数据,最后2列的值应该是ajay和123,但它是asingh12a和asingh12b。这些是a和b的值。它应该来自AA和AB。

有人可以帮助我。?

                            **EDITED HERE**

嗨Aacini - 希望你做得好。我再次面临.csv文件转换为.txt文件的问题。现在这次从.csv获取带有引号(“)的数据,这就是批处理脚本无法正常运行的原因。请查找.csv格式为.txt格式的示例数据 -  “_portcode”, “_ qbconId”, “_名”, “_地址”, “_城市”, “_状态”, “_拉链”, “_ bmeth”, “_ ffee”, “_ brak1”, “_ rate0”, “_ brak2”,“_率1 ”, “_ brak3”, “_率2”, “_ brak4”, “_率3”, “_ brak5”, “_ rate4”, “_ rate5”, “_冰川”, “_ custact”, “_保管”, “_ qbmgrid”, “_ refby” “_txstate”, “_ pmtper”, “_ minfee”

“schorira”,“001c000000WtrL1AAJ”,“9232 Collegeview Cir”,“Bloomington”,“MN”,“55437”,“T”,,“1000000.0”,“1.25”,,,,,,,, ,“1.0”,“Robert M. Schofield IRA Rollover”,“Schwab”,“JTB”,“Patrick Stephens”,“MN”,“Quarterly”,“0”。以前在.csv数据中没有行情。请帮帮我。如果您想从我这边获得更多说明,请告诉我。我提前感谢您的帮助。

嗨Acini,

直到现在我们正在等你的回复。请帮帮我。

1 个答案:

答案 0 :(得分:2)

编辑:我修复了一些细节,现在应该正确运行。

编辑#2 :修复了细微错误,添加了一个示例

@echo off
set "letter=abcdefghijklmnopqrstuvwxyz"
   set "line1=!a!,cf,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,,,,,,,,,,,,,"
   set "line2=!a!,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,qbconid,,,!b!,,,,,,,,,"

   set "line26=!a!,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,txtstate,,,!z!,,,,,,,,,"
   set "line27=!a!,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,pmtper,,,!aa!,,,,,,,,,"
   set "line28=!a!,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,minfee,!ab!,,,,,,,,,"
setlocal EnableDelayedExpansion
(for /F "usebackq skip=1 delims=" %%a in ("NewPortfolios.csv") do (
   set i1=0
   set "line=%%a"
   for %%b in ("!line:,=" "!") do for /F "tokens=1,2" %%i in ("!i1! !i2!") do (
      if %%i lss 26 (
         set var=!letter:~%%i,1!
      ) else (
         set var=a!letter:~%%j,1!
      )
      set "!var!=%%~b!
      set /A i1+=1, i2=i1-26
   )
   echo %line1%
   echo %line2%

   echo %line26%
   echo %line27%
   echo %line28%
)) > file.trn

以前的批处理程序最多可输出52列(a..z aa..az),但增加此范围非常容易。使用此输入数据:

 A,        B,       C,        D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z, AA,    AB
$portcode,$qbconId,$name,     D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,$pmtper,#minfee
asingh12,123456789,Ajay Singh,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,ajay,123

输出是:

asingh12,cf,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,,,,,,,,,,,,,
asingh12,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,qbconid,,,123456789,,,,,,,,,
asingh12,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,txtstate,,,Z,,,,,,,,,
asingh12,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,pmtper,,,ajay,,,,,,,,,
asingh12,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,minfee,123,,,,,,,,,