What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Searching text files

Pages: (2): « First « 1 [ 2 ] Last »
Searching text files
Author: Message:
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
32 / Male / Flag
Joined: Jul 2006
O.P. RE: Searching text files
Thanks WDZ :)
05-03-2009 05:45 PM
Profile E-Mail PM Find Quote Report
stoshrocket
Senior Member
****

Avatar
formerly methos

Posts: 748
Reputation: 31
34 / Male / Flag
Joined: Aug 2005
RE: Searching text files
quote:
Originally posted by blessedguy
quote:
Originally posted by methos
It'd be quite easy in PHP...
How? =O
Not nearly as easy as WDZ's method above, but just for completion and curiosity...
php code:
$needle = 'needle'; //value to search in the text files
$dir = './dir/'; //dir to folder containing text files

$files = scandir($dir);
$arr_count = count($files);
$i = 0;
$cur_file = current($files);

while ($i<$arr_count){
    $ext = substr($cur_file, strrpos($cur_file,'.') + 1);
    if($ext == 'txt'){
        $line_array = file($dir.$cur_file);
        $line_count = count(file($dir.$cur_file));
        $n = 1;
        $cur_line = current($line_array);
        while ($n <= $line_count){
            if(strpos($cur_line,$needle) !== FALSE){
                $line_quote = str_replace($needle,"<b>".$needle."</b>",$cur_line);
                echo"Found value in $dir"."$cur_file <br /> $n - $line_quote";
                exit;
            }
            $n++;
            $cur_line = next($line_array);
        }
    }
    $i++;
    $cur_file = next($files);
}

Creates an array of the contents of ./dir then progressively checks if each value is a text file, if so, each line of this text file is read into an array, which is then progressively searched for the search value $needle. When $needle is found, it stops searching and the path to the file is echo'd, along with the line number and quote of the line with $needle in bold. Can be easily modified to create a file results.txt with this info in, and to search through all files etc.

:happy:

This post was edited on 05-03-2009 at 07:28 PM by stoshrocket.
formerly methos
05-03-2009 07:27 PM
Profile PM Web Find Quote Report
-dt-
Scripting Contest Winner
*****

Avatar
;o

Posts: 1819
Reputation: 74
36 / Male / Flag
Joined: Mar 2004
RE: Searching text files
quote:
Originally posted by methos
quote:
Originally posted by blessedguy
quote:
Originally posted by methos
It'd be quite easy in PHP...
How? =O
Not nearly as easy as WDZ's method above, but just for completion and curiosity...
php code:
$needle = 'needle'; //value to search in the text files
$dir = './dir/'; //dir to folder containing text files

$files = scandir($dir);
$arr_count = count($files);
$i = 0;
$cur_file = current($files);

while ($i<$arr_count){
    $ext = substr($cur_file, strrpos($cur_file,'.') + 1);
    if($ext == 'txt'){
        $line_array = file($dir.$cur_file);
        $line_count = count(file($dir.$cur_file));
        $n = 1;
        $cur_line = current($line_array);
        while ($n <= $line_count){
            if(strpos($cur_line,$needle) !== FALSE){
                $line_quote = str_replace($needle,"<b>".$needle."</b>",$cur_line);
                echo"Found value in $dir"."$cur_file <br /> $n - $line_quote";
                exit;
            }
            $n++;
            $cur_line = next($line_array);
        }
    }
    $i++;
    $cur_file = next($files);
}

Creates an array of the contents of ./dir then progressively checks if each value is a text file, if so, each line of this text file is read into an array, which is then progressively searched for the search value $needle. When $needle is found, it stops searching and the path to the file is echo'd, along with the line number and quote of the line with $needle in bold. Can be easily modified to create a file results.txt with this info in, and to search through all files etc.

:happy:
its even easier than that...

php code:
<?php

$dir = "D:/";
$toFind = "xxx";

foreach(new DirectoryIterator($dir) as $file){
    if(!$file->isDir()){
        $ext = trim(strrchr($file, '.'), ".");
        if($ext == "txt"){   
            $fp = fopen($file->getPathname(), 'r');
            while($fp && !feof($fp)){
                $line = stream_get_line($fp, 2048, "\n");
                if(strpos($line, $toFind) !== false){
                    echo $file . "\n";
                }
            }
            fclose($fp);
        }
    }
}




?>
[Image: dt2.0v2.png]      Happy Birthday, WDZ
05-04-2009 12:15 AM
Profile PM Web Find Quote Report
stoshrocket
Senior Member
****

Avatar
formerly methos

Posts: 748
Reputation: 31
34 / Male / Flag
Joined: Aug 2005
RE: Searching text files
quote:
Originally posted by -dt-

its even easier than that...

php code:
<?php

$dir = "D:/";
$toFind = "xxx";

foreach(new DirectoryIterator($dir) as $file){
    if(!$file->isDir()){
        $ext = trim(strrchr($file, '.'), ".");
        if($ext == "txt"){   
            $fp = fopen($file->getPathname(), 'r');
            while($fp && !feof($fp)){
                $line = stream_get_line($fp, 2048, "\n");
                if(strpos($line, $toFind) !== false){
                    echo $file . "\n";
                }
            }
            fclose($fp);
        }
    }
}
?>

Very elegant, I never thought of using foreach to work through each file or even stream_to_get_line! The more you use the more you learn!

This post was edited on 05-04-2009 at 12:35 AM by stoshrocket.
formerly methos
05-04-2009 12:35 AM
Profile PM Web Find Quote Report
Pages: (2): « First « 1 [ 2 ] Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On