Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl - A way to get only the first (.txt) filename from another directory without loading them all?
    text
    copied!<p>I have a directory that holds ~5000 2,400 sized .txt files.</p> <p>I just want one filename from that directory; order does not matter.</p> <p>The file will be processed and deleted.</p> <p>This is not the scripts working directory.</p> <p>The intention is:</p> <ul> <li>to open that file,</li> <li>read it,</li> <li>do some stuff,</li> <li>unlink it and then</li> <li>loop to the next file.</li> </ul> <p>My crude attempt does not check for only .txt files and also has to get all ~5000 filenames just for one filename. I am also possibly calling too many modules?</p> <p>The <code>Verify_Empty</code> sub was intended to validate that there is a directory and there are files in it but, my attempts are failing so, here I am seeking assistance. </p> <pre><code>#!/usr/bin/perl -w use strict; use warnings; use CGI; use CGI ':standard'; print CGI::header(); use CGI::Carp qw(fatalsToBrowser warningsToBrowser); ### use vars qw(@Files $TheFile $PathToFile); my $ListFolder = CGI::param('openthisfolder'); Get_File($ListFolder); ### sub Get_File{ $ListFolder = shift; unless (Verify_Empty($ListFolder)) { opendir(DIR,$ListFolder); @Files = grep { $_ ne '.' &amp;&amp; $_ ne '..' } readdir(DIR); closedir(DIR); foreach(@Files){ $TheFile = $_; } #### This is where I go off to process and unlink file (sub not here) #### $PathToFile = $ListFolder.'/'.$TheFile; OpenFileReadPrepare($PathToFile); #### After unlinked, the OpenFileReadPrepare sub loops back to this script. } else { print qq~No more files to process~; exit; } exit; } #### sub Verify_Empty { $ListFolder = shift; opendir(DIR, $ListFolder) or die "Not a directory"; return scalar(grep { $_ ne "." &amp;&amp; $_ ne ".." } readdir(DIR)) == 0; closedir(DIR); } </code></pre> <p>Obviously I am very new at this. This method seems quite "hungry"? Seems like a lot to grab one filename and process it! Guidance would be great!</p> <p><strong>EDIT -Latest Attempt</strong></p> <pre><code>my $dir = '..'; my @files = glob "$dir/*.txt"; for (0..$#files){ $files[$_] =~ s/\.txt$//; } my $PathAndFile =$files[0].'.txt'; print qq~$PathAndFile~; </code></pre> <p>This "works" but, it still gets all the filenames. None of the examples here, so far, have worked for me. I guess I will live with this for today until I figure it out. Perhaps I will revisit and see if anyone came up with anything better.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload