Solved

Read folder content and search for specific filenames

Posted on 2011-02-17
7
364 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
Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Suggested Solutions

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

738 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