Solved

Combine all files in one txt

Posted on 2016-09-06
9
41 Views
Last Modified: 2016-09-07
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"
0
Comment
Question by:Jasmine Ikhreishi
  • 4
  • 3
  • 2
9 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41786082
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
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41786101
so i do
implode $myfiles =>'combined.txt' ?
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41786134
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 41786135
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 41786138
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41786141
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
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41787617
But they dont have edi_5 at the end, many of them have geh2_4 or h343_4 , there just random
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41787639
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
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41787659
... 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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now