Closed Thread
Results 1 to 3 of 3

Thread: Search for file and criteria

  1. #1
    atrip25 is offline Newbie
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0
    Ok, I'm trying to write a perl cgi program that lets the user input a file that they want to open and what they want to search for. So for example, they will go to a site and have two fields in a table. One to specify the file name and the other to enter their search criteria. I'm having a hard time actually figuring out how to implement.

    Ok, so I'm thinking I can use the find function to do this. What I would like to do it let the user imput the files name. I can have perl do a find and locate the file. I want to be able to store the path as a string and use it to open the file. Any suggestions on how to make this work?
    Last edited by WingedPanther; 12-17-2009 at 01:46 PM. Reason: Double post

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Red_Shadow's Avatar
    Red_Shadow is offline Learning Programmer
    Join Date
    Jan 2009
    Location
    over the rainbow
    Posts
    58
    Rep Power
    0

    Re: Search for file and criteria

    Learn Perl. If you knew the language, you'd know how simple this was.
    Seriously though, if you use <STDIN> and open(FILE, $filevar), you pretty much have it.

    One thing: Make sure people can't input "../" because then they can access documents you don't want them reaching. Make one folder for the files people are *allowed* to access, and then forbid "../" in the string. Otherwise, you have a path traversal exploit.

    Lookie here: The Web Application Security Consortium / Path Traversal

  4. #3
    phpforfun's Avatar
    phpforfun is offline Speaks fluent binary
    Join Date
    Feb 2008
    Posts
    1,232
    Blog Entries
    17
    Rep Power
    24

    Re: Search for file and criteria

    I actually just did this, its my 2nd perl script ever, its pretty easy man... You just need to do some googling.

    Just to be nice, ill provide my code

    Code:
    #!c:/perl/bin
    use File::Copy;
    use 
    Fatal qw(copy);
    &
    start;


    # BEGIN, CHOOSING THE MAIL SERVER
    sub start{
        
    system("cls");
        print 
    "What mail server?> ";
        
    chomp($svr = <>);
        print 
    "\n";
        &
    CHOOSE;
    }
    # CHOOSE THE PROTOCOL

    sub CHOOSE {
        
    system("cls");
        
        print 
    "What type of logs for $svr?\n";
        print 
    "1 - SMTP\n";
        print 
    "2 - POP\n";
        print 
    "3 - Delivery\n";
        print 
    "4 - IMAP\n";
        print 
    "> ";
        
    chomp($input = <>);

        if(
    $input == 1){
            
    $type "smtpLog";
        } 
    elsif($input == 2){
            
    $type "popRetrieval";
        } 
    elsif($input == 3){
            
    $type "delivery";
        } 
    elsif($input == 4){
            
    $type "imapLog";
        } else {
            print 
    "Please type 1 2 3 or 4\a\n";
            
    sleep(2);
            &
    CHOOSE;
        }
        &
    date;
    }
    # CHOOSE THE DATE
    sub date {
        
    system("cls");
        print 
    "What date? (EG: YYYY.DD.MM)> ";
        
    chomp($date = <>);

    # REGEX THE DATE
        
    if( $date =~ /[^d{4}.d{2}.d{2}]/){
            print 
    "Bad Date Specified\n";
            &
    DATE;
        }
        &
    crap;
    }
    sub crap {
    # SEARCH STRING
        
    system("cls");
        print 
    "String to search> ";
        
    chomp($search = <>);

    # PUT LOG PATH TOGETHER
        
    print $date "-" $type ".log\n";
    my $remote_file "\\\\".$svr."\\e\$\\SmarterMail\\Logs\\".$date."-".$type.".log";
    # CHECK IF LOG EXISTS
        
    if(! -e $remote_file ) {
         print 
    "Can't find ".$remote_file." (Maybe too far back?)\n";
        exit;
    }

    # OPEN THE LOG FILE
    open (FILE$remote_file);

    # CREATE THE RESULTS FILE
    open (RESULTS">results.txt");

    # RESULTS FILE OPENING TEXT
    print RESULTS "Search for " .$search" in " .$remote_file"\n";

    print 
    "Opened ".$remote_file." for reading..\n";

    # set blank session _search, so it can be used later
    $session_search ""

    $count 0;
    $tmp_count 0;
    while (<
    FILE>) {
        
    chomp;
        if(
    $type eq "delivery"){                         # DELIVERY LOG CHOMP
            
    ($date$session$rest) = split(" ");
        } 
    elsif($type eq "smtpLog") {                    # SMTP LOG CHOMP
            
    ($date$ip$session$rest) = split(" ");
        } 
    elsif($type eq "popRetrieval") {                # POP LOG CHOMP
            
    ($date$ip$session$rest) = split(" ");
        } 
    elsif($type eq "imapLog") {                    # IMAP LOG CHOMP
            
    ($date$ip$session$rest) = split(" ");
        } else {                                        
    # NULL CHOMP    
            
    print "Unable to continue, no log chomp available\n";
            
    sleep(2);
            exit;
        }

        
    $line $_;
        if ((
    $line =~ m/$search/i) || ($session eq $session_search)) {
            if(
    $session_search eq $session){
                print 
    RESULTS $line"\n";
            } else {
                
    $session_search $session;
                print 
    RESULTS $line"\n";
            }
        }
        
    # KEEP LOG COUNT AND UPDATE TECH
        
    $count++;
        
    $tmp_count++;
        if(
    $tmp_count == 5000){
            print 
    "Searched " .$count" lines..\n";
            
    $tmp_count 0;
        }
    }

    close (RESULTS);

    close (FILE);

    print 
    "\nSearch Complete!\n";

    # DONE!
    exec 'results.txt';

    # THE END  
    }
    print 
    "The End\n"
    Checkout my new forum! http://adminreference.com/

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Java file search
    By Anemone in forum Java Help
    Replies: 1
    Last Post: 12-07-2010, 09:35 AM
  2. Search TXT file with button click
    By kevinsabres in forum Visual Basic Programming
    Replies: 0
    Last Post: 05-05-2010, 11:50 AM
  3. How can i search in a txt file?
    By lil-fino in forum PHP Development
    Replies: 5
    Last Post: 04-22-2010, 09:35 PM
  4. search and replace over a file
    By dirdamalah0 in forum PHP Development
    Replies: 1
    Last Post: 06-21-2008, 06:26 AM
  5. handling text file and search a string using VB
    By pbosek in forum Visual Basic Programming
    Replies: 1
    Last Post: 02-25-2008, 07:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts