Solved

How to write a PERL program to read a file with path so that it can file to see if certain files exists ?

Posted on 2002-05-22
6
126 Views
Last Modified: 2010-03-05
Hi experts,

Say now a file called "my_paths.txt".
This file contains some paths
for e.g.

#### part of my_paths.txt ####
my_lib = /fs27/abc.mmm/mylib/
her_cat = /afs/common/hercat/
###############################

These directories are of dirrecent paths.
And in these directories, there are some files.

Now the main purpose of the PERL script is to
(1) read the "my_path.txt" file
(2) look into each path listed in the "my_path.txt"
(3) look for certain file name in each of the path

So, this PERL script will initially prompt 2 inputs
- file names to find (for e.g. happy*.*)
- file name that contains paths (for e.g. my_paths.txt")

So, say in the directory of /fs27/abc.mmm/mylib/ , it contains 4 files, i.e. "happygal.log", "apple.txt", "rem.pl" and "happy23.a"
And in the directory of /afs/common/hercat/, it contains 3 files, i.e. "aaa.zz", "happy77.aaa" and "xxx.m".

Then, by running the script, it will print out 3 file names, i.e. happygal.log , happy23.a and happy77.aaa


Pls kind help to provide this script in full.
Thanks.

0
Comment
Question by:carol_loveme
  • 4
  • 2
6 Comments
 

Author Comment

by:carol_loveme
ID: 7026370
For finding the file names, some frens of mine suggest that I can use something like :-

@file_names = glob ("$input");
print "@file_names \n";

And also for path name matching, probably I can use

/\/w+\b*\/

One thing to mention here is that the path will definitely start with either "/afs/" or "/fs##" where ## can be numbers ranging from 1 to 99.

But the path can be  having 3 or more subdirectory levels,
for e.g.

/afs/sub1/sub2/sub3
/fs33/sub1/sub2/sub3/sub4/sub5/sub6

Pls kindly help to provide a full executable script for this problem.

Thanks.
0
 
LVL 4

Expert Comment

by:dda
ID: 7026441
Try this, I think it is self-explanatory:

#!/usr/bin/perl -w

use strict;

print "Enter file names to find: ";
my $mask = <STDIN>; chomp $mask;

print "Enter file name that contains paths: ";
my $list = <STDIN>; chomp $list;

open LIST, $list or die "Can't open $list file: $!\n";
while (my $path = <LIST>) {
    chomp $path;
    my $fullpath = "$path/$mask";
    print "Looking for [$mask] in [$path]\n";
    foreach (glob $fullpath) {
        print "\tfound: $_\n";
    }
}
close LIST;

exit;
0
 

Author Comment

by:carol_loveme
ID: 7028616
Hi, Sorry, my mistake on this :-

Actually the file "my_path.txt" would contain something like :-
my_lib=/fs27/abc.mmm/mylib/
her_cat=/afs/common/hercat/

in which there will be NO SPACE between "my_lib" and "=" and "/fs27/abc.mmm/mylib" and so on.

So, is there anyway to make the script to actually get the part "/fs27/abc.mmm/mylib" while ignoring "my_lib=" ?

Thanks.
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 

Author Comment

by:carol_loveme
ID: 7028619
with the :-
"my_lib=/fs27/abc.mmm/mylib/"
all connected as one line without space in between
the script will take the whole line as one,
hence failed to perform finding correctly.
0
 
LVL 4

Accepted Solution

by:
dda earned 300 total points
ID: 7028961
It's simple:

#!/usr/bin/perl -w

use strict;

print "Enter file names to find: ";
my $mask = <STDIN>; chomp $mask;

print "Enter file name that contains paths: ";
my $list = <STDIN>; chomp $list;

open LIST, $list or die "Can't open $list file: $!\n";
while (my $tmp = <LIST>) {
    chomp $tmp;
    my ($alias, $path) = split /=/, $tmp;
    my $fullpath = "$path/$mask";
    print "Looking for [$mask] in [$alias ($path)]\n";
    foreach (glob $fullpath) {
        print "\tfound: $_\n";
    }
}
close LIST;

exit;
0
 

Author Comment

by:carol_loveme
ID: 7040593
Thanks, dda. =)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question