Windows batch - concatenate multiple text files into one

前端 未结 9 577
醉梦人生
醉梦人生 2021-01-31 13:53

I need to create a script, which concatenates multiple text files into one. I know it\'s simple to use

type *.txt > merged.txt

But the require

相关标签:
9条回答
  • 2021-01-31 14:53

    Try this:

    @echo off
    set yyyy=%date:~6,4%
    set mm=%date:~3,2%
    set dd=%date:~0,2%
    
    set /p temp= "Enter the name of text file: "
    FOR /F "tokens=* delims=" %%x in (texto1.txt, texto2.txt, texto3.txt) DO echo %%x >> day_%temp%.txt
    

    This code ask you to set the name of the file after "day_" where you can input the date. If you want to name your file like the actual date you can do this:

    FOR /F "tokens=* delims=" %%x in (texto1.txt, texto2.txt, texto3.txt) DO echo %%x >> day_%yyyy%-%mm%-%dd%.txt
    
    0 讨论(0)
  • 2021-01-31 14:54

    At its most basic, concatenating files from a batch file is done with 'copy'.

    copy file1.txt + file2.txt + file3.txt concattedfile.txt
    
    0 讨论(0)
  • 2021-01-31 14:58

    I am reiterating some of the other points already made, but including a 3rd example that helps when you have files across folders that you want to concatenate.

    Example 1 (files in the same folder):

    copy file1.txt+file2.txt+file3.txt file123.txt
    

    Example 2 (files in same folder):

    type *.txt > combined.txt
    

    Example 3 (files exist across multiple folders, assumes newfileoutput.txt doesn't exist):

    for /D %f in (folderName) DO type %f/filename.txt >> .\newfileoutput.txt
    
    0 讨论(0)
提交回复
热议问题