windows批处理文件在一个文件夹中按列组合csv [英] windows batch file combine csv in a folder by column
问题描述
我想创建一个Windows批处理文件,将一个包含CSV文件夹的文件夹合并为一个CSV文件,但按列而不是结束。
I would like to create a windows batch file to combine a folder full of CSV's into one CSV file but by column, not at the end.
file1.csv
A, B
1, 2
3, 4
5, 6
...
fileN.csv
C, D
1, 2
3, 4
5, 6
results.csv
A, B, C, D
1, 2, 1, 2
3, 4, 3, 4
5, 6, 5, 6
我看到的所有示例都将2个文件与定义的名称组合,或者一个接一个复制一个简单的副本。我找不到如何复制列!
All of the examples I have seen combine 2 files with a defined name, or a simple copy one after the other. I cannot find how to copy by column!
推荐答案
我修改了我在merge-several-csv-file-side-by-side-using -batch-file 主题,以合并通过通配符选择的多个文件(而不是参数中给出的文件)。这个解决方案可以处理可变数量的文件,但只有最多8个文件可以合并到另一个;如果有超过8个文件,将在mergedFiles文件夹中创建多达8个文件的多个合并文件。如果您要从超过8个文件生成一个文件,则必须使用mergedFiles文件夹中的文件重复此过程。
I modified the solution I posted at merge-several-csv-file-side-by-side-using-batch-file topic in order to merge several files selected via a wild-card (instead of files given in the parameters). This solution can process a "variable number of files", but just a maximum of 8 files can be merged into another one; if there are more than 8 files, several "merged files" will be created with up to 8 files each in "mergedFiles" folder. If you want to generate one file from more than 8 files, you must repeat the process with the files in "mergedFiles" folder.
@echo off
setlocal EnableDelayedExpansion
rem MergeFiles2.bat: Merge several files horizontally, version 2
rem Antonio Perez Ayala
rem Get the list of files to merge into "file" array
set "num=0"
for %%a in (*.csv) do (
set /A num+=1
set "file[!num!]=%%a"
)
rem Merge files from the "file" array
set /A merge=0, last=0
if not exist mergedFiles\ md mergedFiles
:nextMerge
set /A merge+=1, first=last+1, last+=8
if %last% gtr %num% set last=%num%
rem Assemble the lists of redirections and SET /P commands for this merge
set "file1=!file[%first%]!"
echo/
echo The following files:
echo - %file1%
set /A first+=1
set "redirs="
set "commands="
set handle=2
for /L %%i in (%first%,1,%last%) do (
echo - !file[%%i]!
set /A handle+=1
set "redirs=!redirs! !handle!<"!file[%%i]!" "
set "commands=!commands! & set /P "line=^^!line^^!, " ^<^&!handle!"
)
if defined commands set "commands=!commands:~3!"
echo will be merged into: mergedFiles\file%merge%.csv
rem First file in this merge is read with FOR /F command
rem The rest of files are read via standard handles, starting at # 3
%redirs% (
for /F "usebackq delims=" %%a in ("%file1%") do (
rem Get a line from first file
set "line=%%a"
rem Write-Read lines from all files, excepting the last one
%commands%
rem Write the line from last file
echo !line!
)
) > mergedFiles\file%merge%.csv
if %last% lss %num% goto nextMerge
这篇关于windows批处理文件在一个文件夹中按列组合csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!