合并没有标题的CSV文件

时间:2014-01-23 20:41:19

标签: batch-file csv merge cmd

我进行了广泛的搜索,发现了我认为是我的问题的解决方案,它正在合并CSV文件,而不是每次都重复标题。它看起来很有效,只是它只将文件夹中的第一个文件复制到目标文件中。我认为它无法打开文件,因为它们的名称中有空格。我已经被告知我可能只需要在某个地方加上引号,但我不确定它们会去哪里。提前谢谢。

@ECHO OFF
SET first=y
SET newfile=new.csv
for %%F in (*.csv) do IF NOT %%F==%newfile% (
  if defined first (
    COPY /y "%%F" %newfile% >nul
    set "first="
  ) else (
    FOR /f "skip=1delims=" %%i IN (%%F) DO >> %newfile% ECHO %%i
  )
)

5 个答案:

答案 0 :(得分:2)

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem configure paths
    set "source=*.csv"
    set "target=newfile.csv"

    rem remove output file if needed
    if exist "%target%" del "%target%" >nul 2>nul

    rem search for header row
    set "headerRow="
    for %%f in ("%source%") do (
        <"%%~ff" ( for /l %%a in (1 1 10) do if not defined headerRow set /p "headerRow=" )
        if defined headerRow goto haveHeader
    )
:haveHeader
    if not defined headerRow (
        echo ERROR: impossible to get header row. 
        goto endProcess
    )

    rem output header to header file to use as filter.
    rem header is cut to avoid findstr limitations on search strings
    set "headerFile=%temp%\%~nx0_headerFile.tmp"
    setlocal enableextensions enabledelayedexpansion
    > "%headerFile%" echo(!headerRow:~0,125!
    endlocal


    rem search for input files with matching headers to join to final file
    for /f "tokens=*" %%f in ('findstr /m /b /l /g:"%headerFile%" "%source%"') do (
        if not exist "%target%" (

                rem first file is directly copied
                copy "%%~f" "%target%" /y > nul 2>nul

            ) else (

                rem next files are filtered to exclude the header row
                findstr /v /b /l /g:"%headerFile%" "%%~f" >> "%target%"
        )
        echo ... [%%~ff] joined to %target%
    )

    rem remove the temporary header file
    del "%headerFile%" >nul 2>nul

:endProcess
    endlocal

答案 1 :(得分:1)

@echo off &setlocal disableDelayedExpansion
set "NewFile=new.csv"

>"%NewFile%" cd .
for /f "tokens=1*delims=:" %%a in ('dir /b /a-d /od *.csv ^|findstr /nvx "%NewFile%"') do (
    if %%a equ 1 (
        copy /b "%%~b" "%NewFile%" >nul
    ) else (
        for /f "skip=1delims=" %%c in ('type "%%~b"') do >>"%NewFile%" echo(%%c
    )
)

<子> sed for Windows

答案 2 :(得分:1)

这是另一种选择。

@echo off
set "newfile=new.txt"
del "%newfile%" 2>nul
for %%a in (*.csv) do (
  if not exist "%newfile%" (type "%%a" > "%newfile%") else (more +1 "%%a" >> "%newfile%")
)
ren "%newfile%" "new.csv"

答案 3 :(得分:0)

我认为接近尾声的行开始&#34; FOR / f&#34;混淆了它应该是:

@ECHO OFF
SET first=y
SET newfile=new.csv
for %%F in (*.csv) do IF NOT %%F==%newfile% (
  if "%first%"=="y" (
    COPY /y "%%F" %newfile% >nul
    set "first="
  ) else (
    FOR /f "skip=1delims=" %%i IN ("%%F") DO ECHO %%i >> %newfile%
  )
)

答案 4 :(得分:0)

@ECHO OFF
SET first=y
SET "newfile=new.txt"
del new.csv 2>nul >nul
for %%F in (*.csv) do (
  if defined first (
    COPY /y "%%F" %newfile% >nul
    set "first="
  ) else (
    FOR /f "usebackqskip=1delims=" %%i IN ("%%F") DO >> %newfile% ECHO %%i
  )
)
ren %newfile% new.csv

set "var=value"语法可确保批处理行上的任何尾随空格不包含在分配给var的值中。

第一步是删除new.csv文件 - 2>nul >nuldel重定向消息和错误消息,以便命令完全无声 - 文件是否存在。

接下来,您无需检查new.csv是否被选为%%F,因为它刚刚被删除(如果它确实存在),输出现在为new.txt(文件名并不重要 - 实际上,我很想称之为new.vsc。关键是不是 .csv所以for不需要检查一下)

除了第一个文件(copy比读取和回显更快)之外,文件的名称(在%% F中)被读入%%i,因为它需要"quoted" 1}}(告诉CMD空格不是分隔符)您需要将usebackq添加到for/f控件。

最后,将您的文件重命名为所需的新名称。

这应解决问题。