Test for directory

V

VbScripter

I swear I am not a newbie

I am trying to write something simple that needs to test and then
recurse into subdirs.

I have tried 10 differet ways, and all I can get is . and .. in any
given directory.

This needs to work on RH8.0 and or HP-UX 11.11

#!/usr/bin/perl
# looks through dirs
print "Please enter in a dir name ";
$dir=<>;
chomp($dir);

opendir(DIR,$dir) or die "$dir does not exist or I can't open
it\n\n";
@files= readdir(DIR);
closedir(DIR);
for $file(@files){
chomp($file);
my $ftype="";
($device,$inode,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
$blksize,$blocks) = stat($file);

$ftype = ($mode & 0170000)>>12;
# if($ftype!=8){
print "$file = $ftype and mode = $mode\n";
# }
}
I of cours tried if ( -d $dir ) but it only returns . and ..

Please help..
Thanks in advance
 
P

Purl Gurl

VbScripter wrote:

(snipped)
I am trying to write something simple that needs to test and then
recurse into subdirs.
I have tried 10 differet ways, and all I can get is . and .. in any
given directory.


Your method has some problems, which will be commented.
Not all problems are commented, just those needed to
help you get started.

Here is a link to a very generic directory listing utility
which does more than you need. Nonetheless, this is an example
script intended for visitors to take apart and put back together
to perform whatever task is needed.

Do read this page and pay extra attention to coding. You will
learn a bit if you invest a little time and effort.

http://www.purlgurl.net/~purlgurl/perl/direct/dir_list.html

print "Please enter in a dir name ";
$dir=<>;

Your visitors need to enter a full directory path or you
need to provide a directory path to be used.

Entering "test" will not open this directory path:

c:/apache/users/test

opendir(DIR,$dir) or die "$dir does not exist or I can't open
@files= readdir(DIR);

This is ok for reading a single directory. This will
not recurse into child directories.

(snipped) $blksize,$blocks) = stat($file);

Add error checking and you will discover what is wrong.

....stat($file) or die "STAT FAILED: $!";

What information is provided by your $file variable?
You are only providing a filename but no directory
path to your file, yes?

Run your code and read your error message.

stat ("$dir/$file") or die...


Study my example script via that link above. This example
I provide is for people like you to modify and use to
meet your needs. Taking it apart, modifying it, breaking
it, fixing it, all will teach you a lot.

Very simple coding once you learn a bit. Do so and you
will have your script running in no time.


Purl Gurl
 
J

Jürgen Exner

VbScripter said:
I swear I am not a newbie

I am trying to write something simple that needs to test and then
recurse into subdirs.

I have tried 10 differet ways, and all I can get is . and .. in any
given directory.

Was File::Find among those 10 methods?

jue
 
N

nobull

I swear I am not a newbie

So why are you posting to a long-time non-existant newsgroup?
... readdir(DIR) ...

There is a mistake that most people make the first time they use
readdir(). For this reason no less that half the reference manual
entry for readdir() is devoted to warning you about it.

Even if you don't think you are a newbie you should check the
descriptions of the functions in the reference manuals.

(Oh, and as has been pointed out you probably wanted File::Find
anyhow).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top