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
Solved

Read folder content and search for specific filenames

Posted on 2011-02-17
7
355 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
youtube blocking politics 4 56
PHP and JQuery Syntax question 4 26
Test if unique ID is in log file 5 22
check mysql insert 12 27
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

828 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