Jump to content

Merging text files


Rifts

Recommended Posts

Hey!

 

I have a bunch of text files with one line of text ex. SHS9GW34

 

I'm trying to write a little php script on my local machine that will take all the files and put all the different codes in the same files in this kind of format:

 

RGDGIDSG453

EOHBDSBN453

ERGD4535355

etc

etc

 

 

Thanks

Link to comment
Share on other sites

You are not providing any information in your post that would pin down what is needed. Where these files are located (are they all in one folder or spread out over 1000's of folders that are inside of a common folder) and what are their names (do they have a common naming format, do they have a common extension?)

Link to comment
Share on other sites

sorry they are all in the same directory as my index.php page.

 

they are all .txt files and the names are pretty random but if there is a way just to use ANY file that ends it .txt that would work best.

 

 

also this is the error im currently getting:

 

Fatal error: Call to undefined function file_write_contents() in C:\wamp\www\test\index.php on line 7

 

using this code:

<?php
$x = array(
    file_get_contents('file1.txt'),
    file_get_contents('file2.txt'),
    file_get_contents('file3.txt')
);
file_write_contents('final_file.txt', implode("\n", $x));
?>

 

i have the three files in the directory as index currectly and line 7 is

file_write_contents('final_file.txt', implode("\n", $x));

 

 

Link to comment
Share on other sites

sorry they are all in the same directory as my index.php page.

 

they are all .txt files and the names are pretty random but if there is a way just to use ANY file that ends it .txt that would work best.

 

 

also this is the error im currently getting:

 

Fatal error: Call to undefined function file_write_contents() in C:\wamp\www\test\index.php on line 7

 

using this code:

<?php
$x = array(
    file_get_contents('file1.txt'),
    file_get_contents('file2.txt'),
    file_get_contents('file3.txt')
);
file_write_contents('final_file.txt', implode("\n", $x));
?>

 

i have the three files in the directory as index currectly and line 7 is

file_write_contents('final_file.txt', implode("\n", $x));

 

Yeah, because there is no such function, and there is no reason to build and implode() and array:

 

$data = '';
foreach(glob('*.txt') as $file) {
   $data .= file_get_contents($file);
}
file_put_contents('newfile.txt', $data);

Link to comment
Share on other sites

put all the different codes in the same files in this kind of format:

 

1) If there is a possibility that there are duplicate codes and you don't want the duplicates, you may want to look at array_unique()

 

2) I'd be careful naming the file 'newfile.txt' -- If you run the script a second time, the glob() call will include this file and you will double your data.  Either give it a different extension, or delete it (unlink()) before the call to glob(), or specifically exclude it within the loop.

 

Link to comment
Share on other sites

I definitely just noticed my error there, it would be file_put_contents, my bad.

Ok try this

<?php
$newfile = 'result.txt';
$files = glob('C:\path\to\files\*.txt');
foreach($files as $file) file_put_contents($newfile, file_get_contents($file) . "\n", FILE_APPEND);
?>

 

EDIT:

Just noticed that AbraCadaver posted essentially the same code, and his would be better than mine, seeing as how his incurs only one filewrite, and mine incurs multiple. The more you know.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.