batch command merge csv rows

2019-08-28 21:27发布

My problem statement. I have 2 csv files. I want to merge first 2 rows of both files together become one.I run my code it seems like my second header is missing and the data append to 3rd row.

File A:

A B 
1 2

File B:

C D
3 4

Expected result:

A B C D
1 2 3 4

My code:

@echo off
cd /d "C:\my csv directory"
set first=true
setlocal enabledelayedexpansion
(for %%x in (*.csv) do (
if !first!==true (
type "%%x"
echo.
set first=false
) ELSE (
more +1 "%%x"
)
))> c:\destination\newfile.csv

Fail result:

a,b
1,2
3,4

1条回答
小情绪 Triste *
2楼-- · 2019-08-28 21:49

To merge columns from two files you can read two files in paralllel

:: Merge2Files.cmd FileA FileB
@Echo Off & SetLocal EnableDelayedExpansion
Set "FileA=FileA.txt"
Set "FileB=Fileb.txt"
<%FileB% (For /f "delims=" %%A in (%FileA%) Do (
    Set "B="&Set /P "B="
    Echo:%%A,!B!
)) >New-%FileA%
查看更多
登录 后发表回答