Frozen Crayon
Frozen Crayon

Reputation: 5400

Merge several text files into one

I have a directory of several thousand text files. Each text file has a single line. To merge them I used copy * aggregate.txt in the command prompt in Windows but the lines have been copied without any spacing between them.

So how do I combine the files such that lines are separated? Insert a newline character each time a file is copied?

If there's a way to do in command-prompt or using windows batch program or I could do it by writing a c++ program if you could tell me how to read all files in a directory one by one.

Upvotes: 0

Views: 740

Answers (2)

foxidrive
foxidrive

Reputation: 41234

This may take a while to start as thousands of files are involved Change the d:\folder and place the batch file somewhere else that is not in d:\folder

@echo off    
pushd "d:\folder"
for /f "delims=" %%a in ('dir /b /a-d ') do (
type "%%a" >>aggregate.txt
echo.>>aggregate.txt
)
popd

Or this which should start straight away:

@echo off
pushd "d:\folder"
dir /b /a-d >"c:\file.tmp"
for /f "delims=" %%a in (c:\file.tmp) do (
type "%%a" >>aggregate.txt
echo.>>aggregate.txt
)
del "c:\file.tmp"
popd

Upvotes: 1

Magoo
Magoo

Reputation: 80013

@ECHO OFF
SETLOCAL
(
FOR %%i IN (*) DO if /i not "%%i"=="aggregate.txt" TYPE "%%i"&ECHO(
)>aggregate.txt

Personally, I'd use this...

Upvotes: 0

Related Questions