Jump to content
bschultz

Read Through Directory Names and Matching Based on Needle

Recommended Posts

Posted (edited)

I'm trying to loop through a directory for subdirectory names.  If a subdirectory matches needle and haystack...loop through that directory and proceed.

Here's the code

$this_show_date = media_shooter_date ("Saturday", "Saturday");
	$path = "/showdownloads/MediaShooter/MediaShooterDrop/";
	$dir = new DirectoryIterator("/showdownloads/MediaShooter/MediaShooterDrop/");
foreach ($dir as $fileinfo) {
	echo "$fileinfo\n";
	    if ($fileinfo->isDir() && !$fileinfo->isDot()) {
	if (strpos($fileinfo->getFilename(), "Years of Hits") !== false && strpos($fileinfo->getFilename(), "{$this_show_date}") !== false && strpos($fileinfo->getFilename(), 'revised')  !== false) {
        $final_directory = $fileinfo->getFilename(); // does this match ALL of the following: Years of Hits, the date returned from $this_show_date AND contains the word REVISED
    }
	elseif (strpos($fileinfo->getFilename(), "Years of Hits") !== false && strpos($fileinfo->getFilename(), "{$this_show_date}") !== false && strpos($fileinfo->getFilename(), 'revised')  !== true) {
        $final_directory = $fileinfo->getFilename(); // does this match ALL of the following: Years of Hits, the date returned from $this_show_date AND DOES NOTcontain the word REVISED
    }
	}
}
	echo $final_directory; exit; // once $final_directory is set, the code will proceed
	



By echoing the $filename, I get this:

Pulse Best 07.16.18 3 for KKBJ-FM only
Sonrise (CHR) SUNDAY BEFORE 3PM 07.14.18 6 for KKBJ-FM only
25 Years of Hits SATURDAY 1 07.21.18 1 for WMIS-FM only
Virtual News Network MF 07.16.18 3 for WBJI-FM only
SSI RMWorldTravel Minute 07.16.18 4 for KKBJ-AM only
Rick Jackson's Cntry Clsscs (3hr) SATURDAY 07.14.18 3 for KPMI-AM only
Monsters of Rock 3 (Sun) 07.15.18 1 for WMIS-FM only
JT John Tesh Weekday 07.16.18 8 for KKBJ-FM only
Live In Concert ROCK (Sunday) AFTER 3PM 07.15.18 4 for WMIS-FM only
Slow Jams Sunday Nite pop 07.15.18 5 for KKBJ-FM only
.
SSI Chuck Woolery Minute 07.16.18 for KPMI-AM only
Racing Rocks! (Crnt) SATURDAY 07.14.18 3 for WMIS-FM only
25 Years of Hits SATURDAY WITH REVISED HOUR 2 SEGMENT 1 07.21.18 1 for WMIS-FM only
Red Cup Country 5 07.14.18 1 for WBJI-FM only
..
House of Hair 3 SATURDAY 07.14.18 2 for WMIS-FM only
PHP Notice:  Undefined variable: final_directory in /cron/shows/25-years.php on line 62



This should match 25 Years of Hits SATURDAY 1 07.21.18 1 for WMIS-FM only and 25 Years of Hits SATURDAY WITH REVISED HOUR 2 SEGMENT 1 07.21.18 1 for WMIS-FM only...but doesn't.

Any ideas why?

Thanks!

Edited by bschultz

Share this post


Link to post
Share on other sites

strpos is case-sensitive. You can use stripos if that's a problem.

Share this post


Link to post
Share on other sites

Changing the code to replace strpos with stripos still didn't match anything.

Share this post


Link to post
Share on other sites

Have you confirmed $this_show_date has the value you think it does? What's your code now?

Share this post


Link to post
Share on other sites
Posted (edited)

I have confirmed the date is correct...

Here's the code:

$this_show_date = media_shooter_date ("Saturday", "Saturday");
echo "This show date = $this_show_date\n";

/*  // This is the old code...that does NOT check for the date

$dir = "/showdownloads/MediaShooter/MediaShooterDrop/*Years\ of\ Hits*";
foreach(glob($dir) as $file)
{
$files = glob("$file/*.*");
   foreach($files as $audio){
       $file_to_go = str_replace($file,"",$audio);
  copy($audio, '/showdownloads/25Years/' . $file_to_go);

   }
}

*/


$path = "/showdownloads/MediaShooter/MediaShooterDrop/";

$dir = new DirectoryIterator("/showdownloads/MediaShooter/MediaShooterDrop/");
foreach ($dir as $fileinfo) {

//echo "$fileinfo\n";

    if ($fileinfo->isDir() && !$fileinfo->isDot()) {

if (stripos($fileinfo->getFilename(), "Years of Hits") !== false && stripos($fileinfo->getFilename(), "{$this_show_date}") !== false && stripos($fileinfo->getFilename(), 'revised')  !== false) {
        $final_directory = $fileinfo->getFilename();
    }

elseif (stripos($fileinfo->getFilename(), "Years of Hits") !== false && stripos($fileinfo->getFilename(), "{$this_show_date}") !== false && stripos($fileinfo->getFilename(), 'revised')  === false) {
        $final_directory = $fileinfo->getFilename();
    }

}
}

echo $final_directory; exit;
$files = glob("/showdownloads/MediaShooter/MediaShooterDrop/$final_directory/*.*");
   foreach($files as $audio){
       $file_to_go = str_replace($file,"",$audio);
  copy($audio, '/showdownloads/25Years/' . $file_to_go);

   }

 

Edited by bschultz

Share this post


Link to post
Share on other sites

The date is the only thing I can't see, so I think it's the part that's wrong. What is its exact value?

Share this post


Link to post
Share on other sites

This show date = 07.21.18

Share this post


Link to post
Share on other sites

Here's the function...

 

function media_shooter_date ($show, $date_of_directory)  // show is daily or weekend...date of directory is the date to expect from the media shooter directory
{
$todays_dow = date('l');  // Sunday, Monday...etc

if ($show == "daily" && $todays_dow == "Sunday") { $media_shooter_date_diff = "+1 day"; }
if ($show == "daily" && $todays_dow == "Monday") { $media_shooter_date_diff = "+0 day"; }
if ($show == "daily" && $todays_dow == "Tuesday") { $media_shooter_date_diff = "-1 day"; }
if ($show == "daily" && $todays_dow == "Wednesday") { $media_shooter_date_diff = "-2 days"; }
if ($show == "daily" && $todays_dow == "Thursday") { $media_shooter_date_diff = "-3 days"; }
if ($show == "daily" && $todays_dow == "Friday") { $media_shooter_date_diff = "-4 days"; }
if ($show == "daily" && $todays_dow == "Saturday") { $media_shooter_date_diff = "-5 days"; }

if ($show == "Saturday" && $todays_dow == "Sunday") { $media_shooter_date_diff = "+6 days"; }
if ($show == "Saturday" && $todays_dow == "Monday") { $media_shooter_date_diff = "+5 days"; }
if ($show == "Saturday" && $todays_dow == "Tuesday") { $media_shooter_date_diff = "+4 days"; }
if ($show == "Saturday" && $todays_dow == "Wednesday") { $media_shooter_date_diff = "+3 days"; }
if ($show == "Saturday" && $todays_dow == "Thursday") { $media_shooter_date_diff = "+2 days"; }
if ($show == "Saturday" && $todays_dow == "Friday") { $media_shooter_date_diff = "+1 day"; }
if ($show == "Saturday" && $todays_dow == "Saturday") { $media_shooter_date_diff = "+0 day"; }

if ($show == "Sunday" && $todays_dow == "Sunday") { $media_shooter_date_diff = "+0 day"; }
if ($show == "Sunday" && $todays_dow == "Monday") { $media_shooter_date_diff = "+6 days"; }
if ($show == "Sunday" && $todays_dow == "Tuesday") { $media_shooter_date_diff = "+5 days"; }
if ($show == "Sunday" && $todays_dow == "Wednesday") { $media_shooter_date_diff = "+4 days"; }
if ($show == "Sunday" && $todays_dow == "Thursday") { $media_shooter_date_diff = "+3 days"; }
if ($show == "Sunday" && $todays_dow == "Friday") { $media_shooter_date_diff = "+2 day"; }
if ($show == "Sunday" && $todays_dow == "Saturday") { $media_shooter_date_diff = "+1 day"; }


return date('m.d.y', strtotime("$media_shooter_date_diff")). "\n";




}

 

Share this post


Link to post
Share on other sites

Look carefully.

return date('m.d.y', strtotime("$media_shooter_date_diff")). "\n";

 

Share this post


Link to post
Share on other sites

New line!  Damn!  Thank you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.