Page 1 of 1

Best way for a simple wildcard search

Posted: Mon Dec 14, 2009 11:20 am
by IGGt
I have a system that outputs csv files in the format of reportname_datetime.csv (e.g. CustReportType1_20091412170923.csv, CustReportType2_20091412165923.csv), and I need a way to check if a copy exists before I create a new one.

The code I have thus far is:

Code: Select all

 
//Check for original file
    $myFile = "ReportFiles\\CustReportType1_*.csv";
    if  (file_exists($myFile)) {
        unlink($myFile);
        echo "The file $myFile has been Deleted";
        } else {
        echo "The file $myFile does not exist";
        }
    }
 
This all works fine, except for the wildcard part (*). If I type the name exactly it works, unfortunately, I wont know the full name, because it is genereated when the file is created.

I tried using:

Code: Select all

 
foreach (glob("CustReportType1_*.csv") as $myFile) {
    if  (file_exists($myFile)) {
        unlink($myFile);
        echo "The file $myFile has been Deleted";
        } else {
        echo "The file $myFile does not exist";
        }
    }
 
But this just returns a blank screen, and doesn't delete the file.

What is the easiest way to include a wildcard search?

Re: Best way for a simple wildcard search

Posted: Mon Dec 14, 2009 12:35 pm
by pickle
So when you pass the wildcard, you want all matches to be deleted?

Your glob() should work, but you need to be sure you're passing it the right argument. In your non-wildcard example, you've preceded the filename with "ReportFiles\\", but you didn't do that in the glob() example.

Re: Best way for a simple wildcard search

Posted: Tue Dec 15, 2009 2:59 am
by IGGt
That's right, so in the example it would delete everything that starts with 'custReportType1_' and is a csv file. (so it would delete CustReportType1_20091215085332.csv, but not 'CustReportType2_20091215085400.csv'.

I have checked my code and have actually included the location in the script, I just forgot to write it in the example.

<pasted from my test file>

Code: Select all

 
<?php
//print (glob("ReportFiles\Custreport_*.csv"));
 
foreach (glob("ReportFiles\\Custreport_*.csv") as $myFile) {
    if  (file_exists($myFile)) {
        unlink($myFile);
        echo "The file $myFile has been Deleted";
        } else {
        echo "The file $myFile does not exist";
        }
    }
?>
 
As you can see I also tried to print the result first (to see what it was picking up), but instead of getting the correct result (ReportFiles\CustReport_20091214140656.csv) it prints the word 'Array'.

Unfortunately, though it still doesn't work, the file doesn't get deleted.
Do I need to do anythin up front to use Glob(), or is it built in? I am using PHP 5.3.0.

Re: Best way for a simple wildcard search

Posted: Tue Dec 15, 2009 4:48 am
by IGGt
OK,

I've fixed one problem but found another. The problem was (rather embarrisingly) , the difference between Custreport and CustReport!

However, now that part of it works, the else statement isn't working, the file has been deleted, but when I run it again, I get a blank screen, and not 'The file does not exist'.

n.b. I changed the code to take out the $myFile from that line, as it was rather pointless.

Re: Best way for a simple wildcard search

Posted: Tue Dec 15, 2009 9:58 am
by pickle
If the file doesn't exist, glob() won't find it either.

Re: Best way for a simple wildcard search

Posted: Tue Dec 15, 2009 10:11 am
by IGGt
But surely therefore the

Code: Select all

 
if  (file_exists($myFile)) {
        ...
         } else {
         echo "The file does not exist";
         }
 
should work and say 'the file does not exist'