Cmd行CSV合并错误

时间:2016-11-18 16:45:41

标签: excel csv command-line merge

我从命令行合并了许多CSV文件。但是,在Excel中打开生成的文件时,每个CSV的第一行将附加到上一个CSV的最后一行。我尝试过其他合并手段,但同样的事情发生了。

例如: 从命令行我运行:

copy *.csv all.csv 

CSV#1的第56行是a,b,c

CSV#2的第1行是1,2,3

合并的CSV将是a,b,c1,2,3

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

下一个脚本显示了一些合并(实际上是追加)场景:

  • 简单COPY :如果文件中的最后一行缺少标准行结尾,即 CR LF ,则批准所描述的行连接;
  • 循环COPY :修复了上述问题,但可能会在输出文件中添加一些空行;
  • 循环类型:与循环COPY相同;
  • 循环ECHO :可能最好的解决方案允许处理例如CSV标头正确(如果存在);
  • 在简单COPY之前预先附加 CR LF :与循环COPY 相同,但非常简单,可以写成oneliner
    • del all.csv&(for %G in ( *.csv ) do @>>"%~G" echo()&copy *.csv all.csv /B

请注意,没有结尾copy *.csv all.csv开关的/B可以将 Ctrl + Z 字符添加到all.csv端。< / p>

<强>脚本

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion

    REM change the current directory to a test folder 
pushd d:\bat\files\folder

    REM create sample input files (optional) 
call :createSampleInputFiles

echo(
echo --- simple COPY:
2>NUL del all.csv
>NUL copy *.csv all.csv /B
    REM (debugging, optional) show output file
findstr "^" all.csv

echo(
echo --- loop COPY:
2>NUL del all.csv
    REM create empty output file: necessary
>NUL copy NUL all.csv
for %%G in (*.csv) do (
  if NOT "%%~G"=="all.csv" (
    >NUL copy all.csv + %%G all.csv /B
        REM add an empty line unconditionally
    >>all.csv echo(
  )
)
findstr "^" all.csv

echo(
echo --- loop TYPE: same as --- loop COPY
2>NUL del all.csv
    REM create empty output file: possible but not necessary
rem >NUL copy NUL all.csv
for %%G in (*.csv) do (
  if NOT "%%~G"=="all.csv" (
    >>all.csv type %%G
        REM add an empty line unconditionally
    >>all.csv echo(
  )
)
rem findstr "^" all.csv

echo(
echo --- loop ECHO:
2>NUL del all.csv
>all.csv (
  for /F "delims=" %%G in ('dir /b *.csv') do (
    if NOT "%%~G"=="all.csv" (
      for /F "usebackq tokens=*" %%g in ( "%%~G" ) do (
            REM ToDo: (optional) if not 1st file then skip copying CSV header  
        echo(%%g
      )
    )
  )
)
findstr "^" all.csv

    REM for completeness - add <CR><LF> in advance, then simple COPY:
echo(
echo --- add ^<CR^>^<LF^> before simple COPY: same as --- loop COPY 
2>NUL del all.csv
for %%G in ( *.csv ) do >>"%%~G" echo(
>NUL copy *.csv all.csv /B
rem findstr "^" all.csv

    REM change directory back to the path/folder most recently stored by PUSHD
popd

ENDLOCAL
goto :eof

:createSampleInputFiles
      REM create sample input files 
  2>NUL del *.csv
  for %%g in ( x y z ) do (
    >i%%g.csv (
          REM write CSV header (optional)
      rem echo(Ca,Cb,Cc
      for %%G in ( 1 2 ) do (
        echo(a%%g%%G,b%%g%%G,c%%g%%G
      )
      if "%%g"=="x" (
            REM last line with trailing <CR><LF>
        echo(a%%gL,b%%gL,c%%gL
      ) else (
            REM last line without trailing <CR><LF>
        <NUL set /P "=a%%gL,b%%gL,c%%gL"
      )
    )
  )
      REM show sample input files
  rem type *.csv
goto :eof

<强>输出

d:\bat> D:\bat\SO\40682173.bat

--- simple COPY:
ax1,bx1,cx1
ax2,bx2,cx2
axL,bxL,cxL
ay1,by1,cy1
ay2,by2,cy2
ayL,byL,cyLaz1,bz1,cz1
az2,bz2,cz2
azL,bzL,czL
--- loop COPY:
ax1,bx1,cx1
ax2,bx2,cx2
axL,bxL,cxL

ay1,by1,cy1
ay2,by2,cy2
ayL,byL,cyL
az1,bz1,cz1
az2,bz2,cz2
azL,bzL,czL

--- loop TYPE: same as --- loop COPY

--- loop ECHO:
ax1,bx1,cx1
ax2,bx2,cx2
axL,bxL,cxL
ay1,by1,cy1
ay2,by2,cy2
ayL,byL,cyL
az1,bz1,cz1
az2,bz2,cz2
azL,bzL,czL

--- add <CR><LF> before simple COPY: same as --- loop COPY

d:\bat>