Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching for specific files in directory 1

Status
Not open for further replies.

megabyte214

Programmer
Nov 7, 2001
40
US
Is there a way to get all the files in a directory with an extension of .txt?
I used opendir to get all the directories and I want to do something like this to get all the files with extension .txt. Thanks in advance.
 
I figured it out.

Code:
use File::Find;
find(\&wanted, 'blosxom');

sub wanted{
#print substr($_, -3)."\n" ;
	if (substr($_,-3  )eq "txt"){

		-f $_ && print "$File::Find::name\n";
#print substr($_,length -4)."\n";
	}#END if
}

I want to add these files to a list box. I have been looking for examples and I haven't found anything. Thanks in advance for any help on this matter.
 
You could have also done (using file globbing)
Code:
my @files = <*.txt>;
Or, for files in a different directory,
Code:
my @files = <$dir/*.txt>;
Although, [tt]File::Find[/tt] is a good module to know, especially for more complicated search requirements.

jaa
 
Thanks! This code works on the UNIX command line, but I get a &quot;Internal Error or Misconfiguration&quot; error when I try and run it on the web. My permissions are set on this file at 755. Any suggestions would be great. Thanks in advance.

Code:
#!/usr/local/bin/perl -w

use CGI;
use strict;

use File::Find;
my $r = new CGI; # make a new CGI object 

find(\&wanted, 'blosxom');
my $i = 0;
my @names;
my $myLength;
my $size;
sub wanted{

	if (substr($_,-3  )eq &quot;txt&quot;){

		push(@names, $File::Find::name);

	}#END if
$myLength = $size=@names;

}#end wanted 

print $r->header('text/html'),
print $r->start_html(-title=>&quot;Add a new File&quot;);
print $r->startform;
	print &quot;<P>List of Text Files &quot;,
        $r->popup_menu('files',@names);

 $r->endform;           
print  $r->end_html();
 
I see two errors.

One, This line should have a semicolon instead of a comma
Code:
print $r->header('text/html'),
(or remove 'print' from the line that follows it).

Two, popup_menu() takes a reference to an array rather than the array itself
[tt]
$r->popup_menu('files', \@names);
[/tt]

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top