arunss created this script but has not updated my request since then can anyone help me with this mode to his already good script ?

See Below for that I need to add

<?php
$songs = file('songs.txt');
var_dump($songs);
echo "<br /><br /><h3>result</h3>";
$filtered_songs	=	array_unique($songs);
var_dump($filtered_songs);
if(sizeof($filtered_songs)>0)
{
	$content	=	"";
	foreach($filtered_songs as $song)
	{
		$content	=	$content.$song;
	}

}

$myFile = "newfile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $content);
fclose($fh);

?>

arunss TY for all your help that is EXACTLY what I needed now all I need is the same idea but I need a seperate script that will run thru each hour of the day

EX:
1am 2am 3am 4am 4am 6am 7am 8am 9am 10am 11am 12pm ... 12am

and take each hours file ( say for ex 10am.txt ) and add all the songs in each file to one file for that day ... ( ex playlistsun.txt for sunday , or playlistmon.txt for monday ETC ETC ...

so in effect it needs to open each hours file and append it to one file in order from 12am to 11pm for each day based on the servers day and time ... and dont worry about telling it when to run I want to do this once a night thru a cron job ...

Thank you so much if you can help ... or anyone else ...

please have it name the file playlistsun.txt or playlistmon.txt based on the day it is on the server ...

Thanks
Midnite

Recommended Answers

All 10 Replies

First of all, sorry for the delay. I was busy with my office work.
This code will create new files for each hour(1am.txt,2am.txt...) and place in folders having the name of current date( eg :2010_01_29).
You can read all files in that folder later.

<?php
$songs = file('songs.txt');
//var_dump($songs);
//echo "<br /><br /><h3>result</h3>";
$filtered_songs	=	array_unique($songs);
//var_dump($filtered_songs);
if(sizeof($filtered_songs)>0)
{
	$content	=	"";
	foreach($filtered_songs as $song)
	{
		$content	=	$content.$song;
	}

}
//Get Current date
$date	=	date("Y_m_d",time());

//Create a new folder for todays date (eg: 10_01_21)
createFolder($date);

//Get Current time
$time_in_hour	=	date("ga",time());
//Create new file name
$new_file_name	=	$time_in_hour.".txt";


$myFile = $date.'/'.$new_file_name;
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $content);
fclose($fh);


function createFolder($folderpath)
{
	if(!is_dir($folderpath))
		{
			mkdir($folderpath,0777);
		}
		
}
?>

This does what you said but what I want the script to do is go to a folder ( ex playlists ) and add the contents of each hour to one file with that days name ...

Ex -

Files in playlists folder are

12 am 1am 2am 3am 4am 4am 6am 7am 8am 9am 10am 11am 12pm ... 11pm

what I want is the script to open each hour in that folder and append it to one file named ( that day - Monday, Tuesday ETC ) but in order of the hours starting at midnight and ending at the 11pm hour

12am
SONG
SONG
SONG
1am
SONG
SONG
SONG
2am
SONG
SONG
SONG

ETC ETC ETC

until

11pm
SONG
SONG
SONG

IF you need me to be more specific please let me know ...

Thanks again for all your help

Midnite

i ran the script and for some reson altho you made it run thru unigue it still grabbed 2 identical songs ... instead of being unigue ... I had the first 7 songs in the txt file all the same for testing but as mentioned it didnt UNIQUE the file ?

Is something missing ?

Please advise ?

This is a shell script basically that does what I am asking you to help me with in PHP if you could

#!/bin/sh
# Midnight Script Cleans up 24hr playlists and appends them
# to ONE file (ex: PLAYLIST.Sun)
#
# Then copies new Playlist file over top of currently appended
# files (execpt 12am.txt)
#
#
# Written:	01/03/2010
# By:		Dave Parks
#
# Updated:	01/03/2010
# By:		Dave Parks
#
#
#

DAY=`date +%a`
DAY1="$DAY".txt
DIR0="playlists"
DIR1="dailyplaylists"
DIR2="generic"

rm "$DIR1"/Playlist_"$DAY1"

for i in `cat ./list`
do
cat "$DIR0"/"$i" | uniq  >> "$DIR1"/Playlist_"$DAY1"
done


for i in `cat ./list | grep -v 12amold.txt`
do
cp "$DIR2"/"$i" "$DIR0"/"$i"
done


exit

This code will read files having name like 1am.txt 2am.txt 3am.txt and append to a new file (eg: playlistFriday.txt). I dont know whether it meet your requirement,

<?php
//Your directory name (or todays directory)

$dir = "2010_01_29";
//Read all files from this directory , this will return as an array
$songs	=	ReadDirectory($dir);

//Pass the resultant array for creating file 
createFileOfToday($dir,$songs);

function ReadDirectory($dir_name='')
{
	//Read all files from this directory having name like 1am.txt 2am.txt 3am.txt
	if(is_dir($dir_name))
	{
		$arr_result		=	array();
		$file_suffix	=	"am.txt";
		for($k=1;	$k<=2;	$k++)
		{
			for($i=1;	$i<=12;	$i++)
			{
				$file_name	=	$dir_name."/".$i.$file_suffix;
				if(file_exists($file_name))
				{
					$songs = file($file_name);
					$filtered_songs	=	array_unique($songs);
					$arr_result		=	array_merge($arr_result	,$filtered_songs);
				}
			}
			$file_suffix	=	"pm.txt";
		}
	}
	$arr_result	=	array_unique($arr_result);
	return $arr_result;
}

function createFileOfToday($dir_name,$songs)
{
	if(sizeof($songs)>0)
	{
		$content	=	"";
		foreach($songs as $song)
		{
			$content	=	$content.$song."\r\n";
		}
		$day	=	date("l",time());//get day lowercase 'L' as used for getting current day, 
		$new_file_name	=	$dir_name."/playlist".$day.".txt";
		$fh = fopen($new_file_name, 'a') or die("can't open file");
		fwrite($fh, $content);
		fclose($fh);
		echo "New file created as $new_file_name";
	}
	
}

?>

Thanks again for your help ....

There are only 2 issues with your current script and then it is perfect

1) I worked it out so there is no line breaks between song names

EX:
your script has it doing this

1am

love is good

king of pain

I now have it doing this

1am
love is good
kingofpain

but what I need to do is after each our put in the line break to space out the hours lists seperately

Ex:

1am
love is good
king of pain

2am
charlie loves you
your the best

instead of
1am
love is good
king of pain
2am
charlie loves you
your the best

which is what it does now with no space between the hours


2) The other issue is this ... it starts at 1am ( when I would like it to start at 12am then do 1am,2am,3am,4am,5am,6am ... then after it gets to `11am the next one would have to be 12pm then 1pm,2pm,3pm all the way till it gets to the 11pm hour ( in other words a full 24hrs starting at 12am and ending at 11pm ... meaning the first file it reads into the new filw would have to be 12am then 1am then 2am etc etc then after it reads in 11am it would then need to switch to 12pm then 1pm then 2pm then 3pm till is finished at 11pm ...
EX
12am
CONTENT
1am
CONTENT
2am
CONTENT
3am
CONTENT
4am
CONTENT
5am
CONTENT
6am
CONTENT
7am
CONTENT
8am
CONTENT
9am
CONTENT
10am
CONTENT
11am
CONTENT
12PM
CONTENT
1PM
CONTENT
2PM
CONTENT
3PM
CONTENT
4PM
CONTENT
5PM
CONTENT
6PM
CONTENT
7PM
CONTENT
8PM
CONTENT
9PM
CONTENT
10PM
CONTENT
11PM
CONTENT

If you could adjust your script to make those 2 fixes what you have created is PERFECT and I am most greatful

Thanks so very much for your help
Midnite

Hi midnight,
Could you send me the sample files like 1am.txt, 2am.txt with valid data?

How would I do that ?

Please let me know ?

mail me

How would I mail you ... where ? how do I do that ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.