2016-11-18 48 views
0

我從命令行合併了許多CSV文件。但是,在Excel中打開生成的文件時,每個CSV的第一行會附加到前一個CSV的最後一行。我嘗試過其他合併方式,但同樣的事情發生。命令行CSV合併錯誤

例如: 從命令行我運行:的CSV#1

copy *.csv all.csv 

行56是A,B,C

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

合併CSV將是A,b,c1,2,3

任何人都有一個線索,如何解決這一問題?

回答

0

下一個腳本顯示了一些合併(事實上,附加)場景:

  • 簡單的COPY:批准描述線串聯,如果在一個文件中最後一行缺少結尾即CRLF標準線 ;
  • loop COPY:修復了上述問題,但可能會在輸出文件中添加一些空行;
  • loop type:與loop相同COPY;
  • loop ECHO:可能是最好的解決方案可以處理CSV頭文件(如果有的話);
  • 追加CRLF提前簡單的COPY前:同環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結尾。

腳本

@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>