通过批处理合并csv文件 [英] Merging csv-files via batch

查看:124
本文介绍了通过批处理合并csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将来自不同来源的很多小型csv文件合并为单独的较大的csv文件

通常我使用:

(.)中/r E:\ MyFolder %% d的

 复制"%% d \ *.csv""%% d \ merge.txt"/B) 

这可以很好地工作,但是对于某些类型的csv文件,在末尾缺少新行,导致该行无法正常工作.我可以使用:

对于(E:\ MyFolder \ case1 \ *.csv)中的%% x,做更多的"%% x">> E:\ MyFolder \ case1 \ merge.txt

但是,显然,它不会递归运行,仅适用于顶层文件夹.我尝试将相同的for循环包装为其他语句,但仍然无法正常工作.

您能帮我递归工作吗?

解决方案

以下代码应为您工作:

  @echo offrem//在此处定义常量:设置"_ROOT = E:\ MyFolder"设置"_FILE = merge.csv"设置"_MASK = *.csv"rem//解析目标文件路径以供以后比较:为(%_ROOT%\%_ FILE%")中的%% C做(rem//写入目标文件:>"%%〜fC"(rem//切换到目标文件的父目录:按下"%%〜dpC".&&(rem//遍历所有匹配的文件:为/R %% F在(%_MASK%")中做(rem//排除目标文件:如果/I不是"%%〜F" =="%%〜fC"(rem//如果需要,返回文件内容并以换行符结尾:更多"%%〜F"))弹出))) 

当输入的CSV文件包含少于64K的行/行(每行少于64K字符/字节)时,此功能有效.

对于具有64K行/行或更多行的输入CSV文件,将更多"%%〜F" 替换为<假定每行/行少于4K个字符/字节,"%%〜F"找到/V" .

I need to merge a lot small csv-files from different source into separate bigger csv-files

Usually I use:

for /r E:\MyFolder %%d in (.) do ( 
    copy "%%d\*.csv" "%%d\merge.txt" /B
)

This works fine, but for a certain type of csv file, where a new line at the end missing where this won't work correctly. I can use :

for %%x in (E:\MyFolder\case1\*.csv) do more "%%x" >>E:\MyFolder\case1\merge.txt

But it won't run recursively, only for the top folder, obviously. I tried wrapping the same for loop as in the other statement around it but it still won't work.

Can you help me getting it to work recursively?

解决方案

The following code should work for you:

@echo off
rem // Define constants here:
set "_ROOT=E:\MyFolder"
set "_FILE=merge.csv"
set "_MASK=*.csv"
rem // Resolve target file path for later comparison:
for %%C in ("%_ROOT%\%_FILE%") do (
    rem // Write to target file:
    > "%%~fC" (
        rem // Change into parent directory of target file:
        pushd "%%~dpC." && (
            rem // Loop through all matching files:
            for /R %%F in ("%_MASK%") do (
                rem // Exclude target file:
                if /I not "%%~F"=="%%~fC" (
                    rem // Return file content with final line-break if needed:
                    more "%%~F"
                )
            )
            popd
        )
    )
)

This works when the input CSV files contains less than 64K lines/rows each of which is shorter than 64K characters/bytes.

For input CSV files with 64K lines/rows or more, replace more "%%~F" by < "%%~F" find /V "", given that each of the lines/rows is shorter than 4K characters/bytes.

这篇关于通过批处理合并csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆