Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 20th, 2012, 03:26 PM
machina machina is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 machina User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 49 sec
Reputation Power: 0
List files in a directory into array and print

I would like to list all *.rpt files in a directory into an array and then convert each file name into a link. The $rpts parameter is sourced from an html page with three links: Billing, GL, Inventory. I have presented test code here for review. When you replace $rpts with, say, 'billing', one would expect a list of files to display but nothing happens -?! Any help would be greatly appreciated!


#!/usr/bin/perl

#use strict;
use CGI qw(:standard);

#Three types of web reports: Billing, GL, Inventory
#my $rpts = param('reports');

my $dir = "/production/reports/$rpts";

opendir(DIR, $dir);

while (my @file = readdir(DIR)) {

# We only want files
next unless (-f "$dir/@file");

# Use a regular expression to find files ending in .rpt
next unless (@file =~ m/\.rpt$/);

print "$file(@file)\n";
}

closedir(DIR);

exit 0;

Reply With Quote
  #2  
Old December 20th, 2012, 10:21 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 188 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 5 m 8 sec
Reputation Power: 41
Try it like this:
Code:
my $file_name_search  =  "dir_path/*.rpt";
my @file_names_found;

# Search for files and load array of qualified file names to zip.
@file_names_found  =  glob("$file_name_search");

# Print names of files found
if ( ! @file_names_found ) {
  print "No files were found.\n";
} else {
  foreach $fnf ( @file_names_found ) {
    print "$fnf\n";
  }
}

Reply With Quote
  #3  
Old December 21st, 2012, 01:15 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 502 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 18 h 51 m 13 sec
Reputation Power: 385
Quote:
Originally Posted by machina

#!/usr/bin/perl

#use strict;
use CGI qw(:standard);

#Three types of web reports: Billing, GL, Inventory
#my $rpts = param('reports');

my $dir = "/production/reports/$rpts";
# ...


This would not compile if you had uses the "use strict;" and "use wartnings;" pragmas because the $repts variable is not defined.

Code:
print "$file(@file)\n";

also does not make much sense to me.

You could try this:

Perl Code:
Original - Perl Code
  1. my @file_list = glob ("/production/reports/*.rpt");
  2. @file_list = grep {-f $_} @file_list;
  3. print "@file_list", "\n";

Reply With Quote
  #4  
Old December 21st, 2012, 08:31 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,645 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 21 h 31 m 24 sec
Reputation Power: 1170
Rather than using 2 (or more) statements to get the file list, I'd use a single grep statement.

Code:
my $search_pattern   = '/production/reports/*.rpt';
my @file_list        = grep { -f } <$search_pattern>;

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > List files in a directory into array and print

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap