CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Searching Directories for Files.

 



adamjazz1
Novice

Jun 19, 2009, 11:48 AM

Post #1 of 7 (906 views)
Searching Directories for Files. Can't Post

Hello everyone, I am new to perl and I have a quick question.

I have a .txt file with about 159 addresses for directories on my server. I would like to search each of these dirs for a specific file.

I am confused about how I go about this.
my text file is set up like this
/####/####/###/###/###/###/##/#####

Can I open the .txt file and pass the information to readdir?
Then check if my file is there.

Thanks for your help. Code doesnt need to be complete, but if you could point me in the right direction.


1arryb
User

Jun 19, 2009, 12:26 PM

Post #2 of 7 (904 views)
Re: [adamjazz1] Searching Directories for Files. [In reply to] Can't Post

Hi adam,

I'm assuming your input file contains L/Unix-style paths, one per line in the file.

If the internal structure of your directories is flat (i.e., no sudirectories):

1. Get the input file and the file to match on from the command line.
2. Open the file.
3. Outer loop: Read each line of the file, one at a time.
4. Middle loop: get all the files in the directory. The easiest would be to use 'for my $dirent glob("$dir/*") { ... }'.
5. Inner loop: Test each dirent against the match file 'e.g., 'if (basename($dirent) eq $matchFile) { ... }').

However, if your directories have substructure (i.e., internal subdirectories, you'd probably do better using File::Find in lieu of 4) and 5).

Cheers,

Larry


adamjazz1
Novice

Jun 19, 2009, 12:55 PM

Post #3 of 7 (902 views)
Re: [1arryb] Searching Directories for Files. [In reply to] Can't Post

Thanks for your reply 1arryb,

Question about #1: how do I get the files to match from the command line? Would it be something like this?

if ($#ARGV != *$filename*) {
print "no match\n";
exit;

Code
 
I can open the file correctly(I think), but how do I read the .txt one line at a time? Here is my open code.

$dir="/****/****/***/***/***.txt";
open (DIR1, $dir);
$direct= <DIR1>;

Code
 
Thanks for your help!


adamjazz1
Novice

Jun 19, 2009, 1:27 PM

Post #4 of 7 (898 views)
Re: [adamjazz1] Searching Directories for Files. [In reply to] Can't Post

I figured it out!

Code
#!/usr/bin/env perl 
#
if ($#ARGV != 0) {
print "What Filename?\n";
exit;
}
$filename = $ARGV[0];


$dir="/****/****/*****/****/*****.txt";
open (DIR1, $dir);

my @lines=();
my $line=();
my $file=();

while ($direct = <DIR1>) {

@lines = `cd $direct ls -l | grep $filename`;
foreach $line (@lines) {
$lines =~ /\s+(\S+)$/;
$file =$1;
print "Found $file in $direct";
}
}
close DIR1;



1arryb
User

Jun 19, 2009, 1:31 PM

Post #5 of 7 (898 views)
Re: [adamjazz1] Searching Directories for Files. [In reply to] Can't Post

Hi adam,

I don't much care for opendir...readdir. Maybe someone else will help if you want to go that route. I, on the other had, suggest:

Code
#!/usr/bin/perl 

# Script to get a list of directories from the given file and
# search them for a file whose name exactly matches
# the 2nd program argument.

use strict;
use warnings;

use File::Find;

my $inputFile = $ARGV[0];
my $matchName = $ARGV[1];
die "usage: $0 <input-file>" unless $inputFile and $matchName;

open (my $in, "<", $inputFile) or die "Can't open $inputFile for reading";
while (my $dir = <$in>) {
chomp($dir);
unless (-d $dir ) {
warn "$dir does not exist. Skipping...";
next;
}
# $_ has the base filename, $File::Find::name has the full pathname of the current file.
# Note: You could easily have the sub {} populate a list
# of matching filenames for post processing if you need
# to do more than just print the names.
find( sub {print $File::Find::name, "\n" if $_ eq $matchName}, ($dir));
}
close $in


Cheers,

Larry


adamjazz1
Novice

Jun 26, 2009, 1:30 PM

Post #6 of 7 (875 views)
Re: [1arryb] Searching Directories for Files. [In reply to] Can't Post

Hi 1arryb,

Thanks for your reply!

Your code works very well, thank you. Could you explain a couple things to me?




Code
die "usage: $0 <input-file>" unless $inputFile and $matchName;


What does the $0 represent here? Does

Code
unless $inputFile and $matchName;

simply mean, that these inputs exist?

Also,

Code
 chomp($dir);

what does the chomp function actually do?

Thank you!


1arryb
User

Jun 26, 2009, 1:39 PM

Post #7 of 7 (874 views)
Re: [adamjazz1] Searching Directories for Files. [In reply to] Can't Post

Hi adam,

1. $0 is the program name, as typed on the command line.

2. 'unless $inputFile and $matchName' means these variables must have a "true" value. true in Perl means not 0 and not the empty string (or undef).

3. chomp($arg) means trim $arg of the line termination as defined in the perl global '$/'. For L/Unix, this is '\n', for Windows, '\r\n';

Cheers,

Larry

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives