Avatar of E=mc2
Flag for Canada asked on

Join text files

I have several text files which need to be joined together.  They all contain data.
I simply want to join the contents of each file in an orderly way, one after another and so forth, and then I want to form 1 file.

For example, there are 4 .txt files, at C:\WorkingFolder\...

They all end with .. 1.txt,  ..2.txt,  ...3.txt and ...4.txt  (for instance Order1.txt, Order2.txt, Order3.txt and Order4.txt.  I want to join them all into 1 .txt file called OrdersComb.txt.
The order of the joining of the contents does not have to follow the 1,2,3,4 but that would be fine if it followed that logical pattern.
Microsoft Legacy OSWindows OSMicrosoft DOS

Avatar of undefined
Last Comment

8/22/2022 - Mon
Bill Prew

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ram Balachandran

yes, if you wish you can also use vbscript also. In the following link you can merge multiple txt files from specific folder to one txt file. It will merge all data to one txt file [OrdersComb.txt]  from the path C:\workingfolder

Const ForReading = 1 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objOutputFile = objFSO.CreateTextFile("OrdersComb.txt") 
strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
Set FileList = objWMIService.ExecQuery _ 
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\workingfolder''} Where " _ 
        & "ResultClass = CIM_DataFile") 
For Each objFile In FileList 
    Set objTextFile = objFSO.OpenTextFile(objFile.Name, ForReading)  
    strText = objTextFile.ReadAll 
    objOutputFile.WriteLine strText 

Open in new window

Steve Knight

I'd go with copy command suggested by Bill from batch unless you need something like a specific order etc.  If needed then get the output of the dir command into the order you want, e.g.

e.g. date order:  dir /od  dir /o-d
or even something like using a sort by 5th char in name
dir /b /o-d | sort /+5

then you can read whatever list of filenames you make, and pipe them all into a new file with a one liner batch:

@echo off
(for /f "tokens=*" %%a in ('dir /b /o-d /a-d Order*.txt') do type "%%~a") > outputfile.txt

Lastly, when joining the files.
I want a blank line to separate the data.   Is this possible?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Steve Knight

Don't know which ones you have tried or talking about but try this or variation as required on the dir command options.  It is only the filenames that is processed by the for command so there is no problem with any control characters like ><&() etc. messing anything up in the data like if we processed them line by line.


@echo off
(for /f "tokens=*" %%a in ('dir /b /o-d /a-d Order*.txt') do (
  type "%%~a"
) > outputfile.txt
Bill Prew

I think this would work in a small BAT file:

@echo off
set Blank="%TEMP%\_lf.txt"
copy file1.txt+%Blank%+file2.txt+%Blank%+file3.txt+%Blank%+file4.txt orderscomb.txt
del %Blank%

Open in new window

Steve Knight

True Bill, messier than nice loop though :-)
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bill Prew

Yes, really depends on how many files involved...

Steve Knight

any particular reason why you gave bill a B grade for answering your question?

It should have been an A grade.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes