Solved

Read folder content and search for specific filenames

Posted on 2011-02-17
7
347 Views
Last Modified: 2012-06-27
Hi,


I have a php script to read a folder content:
<?php
if ($handle = opendir('attachments')) {
    while (($file = readdir($handle))!==false) {
		echo $file."<br/>";
    }
    closedir($handle);
}
?>

Open in new window

and txt files inside:
OUT.101217.143529.Maersk-Test.txt
OUT.110107.151627.Maersk-Test.txt
OUT.110217.220100.Maersk-Test.txt
IN.101217.143529.Maersk-Test.txt
IN.110107.151627.Maersk-Test.txt

What I need is:
- Recognize the latest 'OUT' file (sort by filename, then pickup latest one would be the easiest way I suppose), from files above that would be "OUT.110217.220100.Maersk-Test.txt";
- check the first number in filename, "110217" in this case (number between 'OUT.' and '.') and check if there exist 'IN' file with the same number, if so, echo 'OK', if not, echo 'MISSING DATA';
 
Example step by step:
1. recognize latest file with name beginning with 'OUT': OUT.110217.220100.Maersk-Test.txt
2. take first number from filename: 110217
3. check if there exist file with filename beginning with 'IN.110217'
4. if exists, print 'OK', if not, print 'MISSING DATA';


Thanks for any help
0
Comment
Question by:Zado
  • 4
  • 3
7 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34916882
You might be better using scandir() which does the same job as opendir/readdir but returns the files as an array of filenames. That way you can use the standard sort() routine as well

$fileArray = scandir('attachments');
arsort( $fileArray );

arsort will sort in REVERSE order so the OUT files are done first and the latest number (the biggest?) will be the first one.  Once you have the filename it is a trivial task to swap its OUT for IN and access the array

$filename = $fileArray [0];
$inFile = str_replace("OUT.", "IN.", $filename );

if ( isset( $fileArray [ $inFile ] ) )
    echo "OK";
else
    echo "MISSING DATA";

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34916895
0
 
LVL 8

Author Comment

by:Zado
ID: 34917115
Great idea, thanks, but there's something wrong in your script, no matter what happen, the script always prints 'MISSING DATA'
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34917814
Silly me, the data is in the array as VALUES not as KEYS. I should have used in_array. Replace the IF in the code fragment with

if ( in_array( $inFile, $fileArray ) )

like so


$fileArray = scandir('attachments');
arsort( $fileArray );
$filename = $fileArray[0];

$inFile = str_replace("OUT.", "IN.", $filename );

if ( in_array( $inFile, $fileArray ) )
    echo "OK";
else
    echo "MISSING DATA";

Open in new window

0
 
LVL 8

Author Comment

by:Zado
ID: 34918128
Thanks, almost done.
If filename is: OUT.110217.140029.Maersk-Test.ini
I'm interested if there exists IN.110217... in folder, I need to compare first number in filename only, not full filename, I've done some changes to your script, but don't know what to do next:

<?php
$fileArray = scandir('attachments');
arsort($fileArray);

$filename = $fileArray [0];
$subfilename = substr($filename, 4,6);

$inFile = str_replace("OUT.", "IN.", $filename );
$subinFile = substr($inFile, 3,6);

//I'm stuck here
if (in_array($subinFile,$fileArray)) { echo "OK"; }
else echo "MISSING DATA";
?>

Open in new window

0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 34918478
We need a custom comparison system so replace the in_array in the IF with this function and also please replace arsort with rsort (see code below)

function maerskCompare( $filename, & $fileArray ) {
     $found = false;
     $inFile = preg_replace('!^[\.A-Z]+([0-9]+).*$!', 'IN.$1', $filename );

     foreach( $fileArray as & $aFile )
          if ( strpos( $aFile, $inFile ) !== false ) {
               $found = true;
               break;
          }

     return $found;
}

// ...... other code .......

rsort( $fileArray );
$filename = $fileArray[0];

$inFile = str_replace("OUT.", "IN.", $filename );


if ( maerskCompare( $inFile, $fileArray ) )
    echo "OK";
else
    echo "MISSING DATA";

Open in new window



0
 
LVL 8

Author Comment

by:Zado
ID: 34918777
Nice, thanks a lot for your help.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

775 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question