批处理命令合并csv行
问题描述:
我的问题陈述.我有2个csv文件.我想将两个文件的前两行合并为一个.我运行我的代码,似乎我的第二个标头丢失了,数据追加到了第三行.
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.
文件A:
A B
1 2
文件B:
C D
3 4
预期结果:
A B C D
1 2 3 4
我的代码:
@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
失败结果:
a,b
1,2
3,4
答
要合并两个文件中的列,您可以并行读取两个文件
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%