我从命令行合并了许多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
任何人都知道如何解决这个问题?
答案 0 :(得分:0)
下一个脚本显示了一些合并(实际上是追加)场景:
del all.csv&(for %G in ( *.csv ) do @>>"%~G" echo()© *.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>