Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8103735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:38:16+00:00 2026-06-05T23:38:16+00:00

So my program is supposed to recursively go through a directory and then for

  • 0

So my program is supposed to recursively go through a directory and then for each file in the directory, open up the file and search for the words “error” “fail” and “failed.” Then it should write the instances where these words occur, as well as the rest of the characters on the line after those words, out to a file designated in the command prompt. I have been having some trouble making sure the program performs the search on the files that are found in the directory. Right now it does recurse through the directory and even creates a file to write out to, however, it does not seem to be searching through the files found in the recursing. Here is my code:

#!/usr/local/bin/perl

use warnings;
use strict;
use File::Find;


my $argument2 = $ARGV[0];
my $dir = "c:/program/Scripts/Directory1"; #directory to search through

open FILE, ">>$argument2" or die $!;   #file to write out 
my $unsuccessful = 0;
my @errors = ();
my @failures= ();
my @failures2 = ();
my @name = ();
my @file;
my $file;

my $filename;

opendir(DIR, $dir) or die $!;

while($file = readdir(DIR)){
next if($file =~ m/^\./);



foreach(0..$#file){
    print $_;
    open(FILELIST, '<', $_);    

    while(<FILELIST>){

    if (/Unsuccessful/i){
        $unsuccessful = 1;
    }
    if(/ERROR/ ){
        push(@errors, "ERROR in line $.\n");
        print  "\t\tERROR in line $.:$1\n" if (/Error\s+(.+)/);

    }
    if(/fail/i ){
        push(@failures, "ERROR in line $.\n");
        print FILE "ERROR in line $.:$1\n" if (/fail\s+(.+)/);

    }
    if(/failed/i ){
        push(@failures2, "ERROR in line $.\n");
        print FILE "ERROR in line $.:$1\n" if (/failed\s+(.+)/);

    }

    if ($unsuccessful){

    }

    }
    close FILELIST;


    }
}

closedir(DIR);
close FILE;

So, to clarify, my problem is that the search contained in the “while()” loop does not seem to be executing on the files found in the directory recursively. Any comments/suggestions/help that you can give on why this may be happening would be very helpful. I am new to Perl so some sample code would also just help me understand what you are trying to say. Thank you very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:38:17+00:00Added an answer on June 5, 2026 at 11:38 pm

    Typically, when I want to do something on recursive files, I start with find2perl . -print which generates the boilerplate for me with the wanted function from which I can modify to do whatever I want.

    For example

    # Traverse desired filesystems
    File::Find::find({wanted => \&wanted}, '.');
    exit;
    sub wanted {
        return unless -f $File::Find::name;
        return unless -R $File::Find::name;
        open (F,"<",$File::Find::name) or warn("Error opening $File::Find::name : $!\n");
    
        while(<F>) {
            if(m/error/) { print; }
            if(m/fail/) { print; }
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program is supposed to open a txt file, record the answers from two
The program is supposed to receive an input through cin, tokenize it, and then
The program is supposed to take a file, say data.dat, filled with a list
PLease help me out here. The program is supposed to recursively find out the
My program is supposed to read an integer n from the user and then
The program is supposed to be a live search using php and javascript... where
This program is supposed to run through a while loop twice. The first time
Supposing I have a File f that represents a directory, then f.delete() will only
The program is supposed to accept telnet connections on port 8888 and then send
The program is supposed to find a file and return whether it exists on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.