Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Join text files

Posted on 2013-12-03
11
Medium Priority
?
407 Views
Last Modified: 2013-12-07
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.
0
Comment
Question by:100questions
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 57

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 39694434
The simplest way is just:

copy file1.txt+file2.txt+file3.txt+file4.txt orderscomb.txt

you may also be able to use a wildcard if all the files match a patter, like:

copy file?.txt orderscomb.txt

~bp
0
 
LVL 14

Expert Comment

by:Ram Balachandran
ID: 39694613
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 
    objTextFile.Close 
    objOutputFile.WriteLine strText 
Next 
 
objOutputFile.Close

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39694743
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
etc.
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
0
Plug and play, no additional software required!

The ATEN UE3310 USB3.1 Gen1 Extender Cable allows users to extend the distance between the computer and USB devices up to 10 m (33 ft). The UE3310 is a high-quality, cost-effective solution for professional environments such as hospitals, factories and business facilities.

 

Author Comment

by:100questions
ID: 39695138
Lastly, when joining the files.
I want a blank line to separate the data.   Is this possible?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39695333
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.

Steve

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

Expert Comment

by:Bill Prew
ID: 39695556
I think this would work in a small BAT file:

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

Open in new window

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39695720
True Bill, messier than nice loop though :-)
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 39695833
Yes, really depends on how many files involved...

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39702741
any particular reason why you gave bill a B grade for answering your question?
0
 

Author Comment

by:100questions
ID: 39703102
It should have been an A grade.
0

Featured Post

Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 Creator Update has just been released and I have it working very well on my laptop. Read below for issues, fixes and ideas.
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question