Concatenate .txt files into one file using cmd prompt

血红的双手。 提交于 2019-12-12 03:44:43

问题


I have 367 .txt files that I need to concatenate into one big .txt file through the windows cmd prompt. Each file has the name and date on it, e.g.,

andx.20150401.000000.txt, andx.20150402.000000.txt, ..., andx.20160401.000000.txt

Inside each file I have 7 columns for time (in julians) and 6 other measurable variables. I need a continuous file for all 367 and I wanted to do this through the cmd prompt.


回答1:


I suppose you could do this as one command if the directory is clear of old files that might match the pattern.

TYPE andx.*.txt 2>NUL >>sum_file.txt

Alternatively, this could be built into a .bat script that produces the .txt files in your previous question. I doubt that you would want to type all this on the command line. Please note, this is not tested.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "SUM_FILE=xxxxx.txt"
IF EXIST "%SUM_FILE%" (DEL "%SUM_FILE%")

FOR %%i IN (*.cdf) DO (
    andx "%%~i" -o TXT atmos_pressure temp_mean ^
        rh_mean wspd_vec_mean wdir_vec_mean ^
        org_precip_rate_mean
    SET EXITCODE=!ERRORLEVEL!
    IF !EXITCODE! NEQ 0 (
        ECHO ERORR: andx failed processing "%%~i"
        ECHO ERORR: exitcode is !EXITCODE!
        GOTO TheEnd
    ) ELSE (
        FOR /F "usebackq skip=1 tokens=*" %%s IN (`TYPE "%%~ni.txt"`) DO (
            ECHO>>"%SUM_FILE%" %%s
        )
    )
)

:TheEnd
EXIT /B %EXITCODE%


来源:https://stackoverflow.com/questions/36695472/concatenate-txt-files-into-one-file-using-cmd-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!