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 182739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:59:34+00:00 2026-05-11T14:59:34+00:00

I am using Perl readdir to get file listing, however, the directory contains more

  • 0

I am using Perl readdir to get file listing, however, the directory contains more than 250,000 files and this results long time (longer than 4 minutes) to perform readdir and uses over 80MB of RAM. As this was intended to be a recurring job every 5 minutes, this lag time will not be acceptable.

More info: Another job will fill the directory (once per day) being scanned. This Perl script is responsible for processing the files. A file count is specified for each script iteration, currently 1000 per run. The Perl script is to run every 5 min and process (if applicable) up to 1000 files. File count limit intended to allow down stream processing to keep up as Perl pushes data into database which triggers complex workflow.

Is there another way to obtain filenames from directory, ideally limited to 1000 (set by variable) which would greatly increase speed of this script?

  • 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. 2026-05-11T14:59:34+00:00Added an answer on May 11, 2026 at 2:59 pm

    What exactly do you mean when you say readdir is taking minutes and 80 MB? Can you show that specific line of code? Are you using readdir in scalar or list context?

    Are you doing something like this:

    foreach my $file ( readdir($dir) ) {     #do stuff here } 

    If that’s the case, you are reading the entire directory listing into memory. No wonder it takes a long time and a lot of memory.

    The rest of this post assumes that this is the problem, if you are not using readdir in list context, ignore the rest of the post.

    The fix for this is to use a while loop and use readdir in a scalar context.

    while (      defined( my $file = readdir $dir )  ) {      # do stuff.  } 

    Now you only read one item at a time. You can add a counter to keep track of how many files you process, too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Perl's module File::Find to traverse across a directory. This directory is an
I am using Perl stat() function to get the size of directory and its
I had a question using Perl's readdir(). I want to gather all the files
I am using Perl to load some 'macro' files. These macros can, however, be
For some reason Perl keeps caching the directory entries I'm trying to read using
I am using Perl to print some data read from one file to another.
I am using Perl to read UTF-16LE files in Windows 7. If I read
I'm using perl cron, and I want to make a rule like this run
Duplicate data removal using Perl called within via a batch file within Windows A
I am using Perl's DBD::ODBC to connect to an Oracle database. However, an issue

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.