Avatar of Jazzy 1012
Jazzy 1012
 asked on

Combine all files in one txt

How do I combine all my files named "389476_845786y.edi_5" (Random numbers but the beginning 3 and the _# at the end is stable, using php code, in a document called "combined.txt"
PHP

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Ray Paseur

Read the files with file_get_contents() or file().  The file contents will be strings or arrays.  Append the files to one another.  If using arrays, you can use implode() to rebuild the strings.  Write the resulting strings with file_put_contents().
Jazzy 1012

ASKER
so i do
implode $myfiles =>'combined.txt' ?
Julian Hansen

Try this  - change wild card to match input files and $output path.
<?php
$output = 'combined.txt';
$files = glob("*.edi_5");

foreach($files as $f) {
    file_put_contents($output, file_get_contents($f), FILE_APPEND);
}

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

so i do
implode $myfiles =>'combined.txt' ?
Not exactly.  Whenever you're unsure about how a PHP function works, you can look it up on PHP.NET.  Example:
http://php.net/manual/en/function.implode.php

PHP implode() is used to turn an array into a string, with optional "glue" character(s).  It would create the string you would later write with file_put_contents().  The name of the output file would be used in file_put_contents(), not in implode().
Jazzy 1012

ASKER
But they dont have edi_5 at the end, many of them have geh2_4 or h343_4 , there just random
Ray Paseur

But they dont have edi_5 at the end...
These are the kinds of "details" that we really, really need to know about if we are going to help you!  

You can provide us with a good-sized sample of known endings.  Or you can provide us with a written set of rules that define the endings.  Rules might be something like "three to five lower-case letters and numbers, followed by a single underscore, followed by a single digit."

The reason this is important is because glob() or regex matching uses written rules to find the file names that match your needs.  Without well-defined rules, we will miss important files or include unwanted files, and neither of those outcomes is acceptable.

You might also want to think about putting all the files you want into their own directory.  Then you could just say "process all files in this directory, no matter what their names."  Just a thought...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

... or you can read the code, try to understand what is going on - posting back here for clarification and then modify the code to suite your particular circumstances. By working through the code and understanding it

a) You will learn something new
b) You will be able to maintain and extend the code without having to rely on external input.

Rather than trying to find an Expert to give you the exact answer - get into the code samples posted and understand what they are doing - then modify them to suit your particular requirements.