?
Solved

Read folder content and search for specific filenames

Posted on 2011-02-17
7
Medium Priority
?
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
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 2000 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

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

765 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