Best way for a simple wildcard search
Posted: Mon Dec 14, 2009 11:20 am
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:
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:
But this just returns a blank screen, and doesn't delete the file.
What is the easiest way to include a wildcard search?
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";
}
}
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";
}
}
What is the easiest way to include a wildcard search?