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"
LVL 1
Jazzy 1012Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
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().
1
Jazzy 1012Author Commented:
so i do
implode $myfiles =>'combined.txt' ?
0
Julian HansenCommented:
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

0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Ray PaseurCommented:
This should help you get started.
<?php // demo/temp_jasmine_5.php
/**
 * https://www.experts-exchange.com/questions/28967933/Combine-all-files-in-one-txt.html
 * Generate some files named like "389476_845786y.edi_5"
 */
error_reporting(E_ALL);
echo '<pre>';


// GENERATE THE TEST FILES
$names =
[ "389_ABC.edi_5"
, "389_DEF.edi_5"
, "389_GHI.edi_5"
]
;
$alpha = str_split('ABCDEFG');
foreach ($names as $name)
{
    shuffle($alpha);
    $string = implode(NULL, $alpha) . $name;
    file_put_contents('storage' . DIRECTORY_SEPARATOR . $name, $string);
}


// SHOW HOW TO COMBINE THEM
$iterator = new FilesystemIterator('storage', FilesystemIterator::SKIP_DOTS + FilesystemIterator::CURRENT_AS_PATHNAME);

// USE A REGULAR EXPRESSION TO MATCH THE DESIRED FILE NAMES
$regex
= '#'          // REGEX DELIMITER
. DIRECTORY_SEPARATOR
. '389'        // STRING LITERAL "the beginning 3"
. '.*?'        // ANYTHING OR NOTHING
. 'edi_'       // STRING LITERAL
. '[\d]'       // CHARACTER CLASS FOR DIGITS
. '{1}'        // EXACTLY ONE DIGIT
. '$'          // AT THE END OF THE STRING
. '#'          // REGEX DELIMITER
;

// GATHER THE LIST OF THE DESIRABLE FILES
$desirable = [];
foreach ($iterator as $key => $filepath)
{
    if (preg_match($regex, $filepath))
    {
        $desirable[] = $filepath;
    }
}

// OPTIONAL: SORT THE FILE NAMES?  MAYBE?
sort($desirable);

// COLLECT THE DATA FROM THE DESIRABLE FILES
$collected = NULL;
foreach ($desirable as $name)
{
    $collected .= PHP_EOL . file_get_contents($name);
}
$collected = trim($collected);

// WRITE THE COMBINED FILE DATA (a document called "combined.txt" )
file_put_contents('storage' . DIRECTORY_SEPARATOR . 'combined.txt', $collected);

// SHOW A LINK SO WE CAN SEE THE COMBINED DATA
$link = '<a target="_blank" href="' . 'storage' . DIRECTORY_SEPARATOR . 'combined.txt' . '">SEE THE FILE</a>' . PHP_EOL;
echo $link;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Mod to ensure file is empty
<?php
$output = 'combined.txt';
$files = glob("*.edi_5");
unlink($output);
foreach($files as $f) {
    file_put_contents($output, file_get_contents($f), FILE_APPEND);
}

Open in new window

0
Ray PaseurCommented:
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().
0
Jazzy 1012Author Commented:
But they dont have edi_5 at the end, many of them have geh2_4 or h343_4 , there just random
0
Ray PaseurCommented:
This expert suggested creating a Gigs project.
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...
1
Julian HansenCommented:
... 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.