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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl does not recognize file type 1

Status
Not open for further replies.

irene01

Technical User
Joined
Dec 27, 2006
Messages
5
Location
US
My perl program opens dos directory and tests all files
using -f. There are 5 files in the directory. Somehow the program recognizes only 3. For 2 others any file test operator does not return any value. Here is a code -

{
($dir,$title)=@_;
opendir (DIR,$dir) || die "cannot open: $!";
@dirfiles =readdir(DIR);
close (DIR);
print " \n";
print "$title Directory Content ...\n";
print "--------------------------------\n";
foreach (@dirfiles)
{
$name=$_;
if ( -d $name)
{ next;
}
$len= length $name;
$spaces=" "x(5-$len);
if (-f $name)
{
$date_string = ctime(stat($name)->mtime);

print "$name $spaces $date_string\n";
}
else
{
$a1=(-A $name);
print "$a1\n";
print "????$name\n";
}
}
}



Does anybody know why perl does not recognize some files ?
 
please show whats in @dirfiles.


Code:
($dir,$title)=@_;
opendir (DIR,$dir) || die "cannot open: $!";
@dirfiles =readdir(DIR);
[b]print "$_\n" for @dirfiles;[/b]
close (DIR);


please use the code tags to display your perl code. See the link below: "Process TGML".

- Kevin, perl coder unexceptional!
 
It looks that I found solution by adding chdir($dir) to the program, but still do not understand why without chdir
some files are recognized, while some are not.

Here is the content of the directory -

Volume in drive C has no label.
Volume Serial Number is A026-93C7

Directory of C:\scripts\test\test1

12/28/2006 09:59 AM <DIR> .
12/28/2006 09:59 AM <DIR> ..
12/26/2006 03:57 PM 383 a1.pl
12/26/2006 10:19 AM 741 a3.pl
12/26/2006 10:47 AM 721 a8.pl
12/26/2006 03:57 PM 383 a9.pl
12/26/2006 05:39 PM 250 b1.pl
12/26/2006 05:42 PM 245 b1.txt
6 File(s) 2,723 bytes
2 Dir(s) 1,359,282,176 bytes free

I am running the following perl program which is suppoused to print the content of the directory with the timestamp. If the program does not recognize the type of some file, then it prints the file name with "???" at the beginning.


use File::stat;
use Time::localtime;
use strict;
#use warnings;
my $dir = "c:\\scripts\\test\\test1";
my ($name) = {};
my ($days_keep) = {};
my ($date_string) = {};
my ($len) = {};
my ($spaces)={};
my @dirfiles;
my $a1 = {};
opendir (DIR,$dir) || die "cannot open: $!";
#chdir($dir);
@dirfiles =readdir(DIR);
close (DIR);
print " \n";
print "Directory Content ...\n";
print "--------------------------------\n";
foreach (@dirfiles)
{
$name=$_;
if ( -d $name)
{ next;
}
$len= length $name;
$spaces=" "x(5-$len);
if (-f $name)
{
$date_string = ctime(stat($name)->mtime);

print "$name $spaces $date_string\n";
}
else
{
$a1=(-A $name);
print "$a1\n";
print "????$name\n";
}
}
----------------------

Here is the output -

Directory Content ...
--------------------------------
a1.pl Tue Dec 26 15:57:41 2006
a3.pl Tue Dec 26 10:19:43 2006

????a8.pl

????a9.pl
b1.pl Tue Dec 26 17:39:48 2006
b1.txt Tue Dec 26 17:42:41 2006

-----------------
Now I commented out chdir ($dir) statement (line 14)

the result as follows -

Directory Content ...
--------------------------------
a1.pl Tue Dec 26 15:57:41 2006
a3.pl Tue Dec 26 10:19:43 2006
a8.pl Tue Dec 26 10:47:09 2006
a9.pl Tue Dec 26 15:57:41 2006
b1.pl Tue Dec 26 17:39:48 2006
b1.txt Tue Dec 26 17:42:41 2006

This shows that now my script recognizes all files in the directory.

What is even more strange, that if now I commented out chdir
again, the script will recognize all files.

I am new to PERL. Is there any explanation of this ?
I did not find it in docs.

Thanks,
Irene.




 
try like this:

Code:
chdir($dir) or die "Can't chdir to $dir: $!":
opendir (DIR,'.') or die "cannot open: $!";
@dirfiles = readdir(DIR);
close(DIR);

please use the code tags from now on for posting your perl code.



- Kevin, perl coder unexceptional!
 
This works. Thanks for your help.
Does this mean that before opening directory
I need always do chdir. I was looking in the docs - did not find anything about this.

Thanks again.
 
if you do it how I showed using '.' in the opendir function then you should chdir first otherwise you open whatever the current working directory is.

You don't need to chdir to do what you are doing but if you don't you probably need to use the full dir path in your file test operations:

Code:
$dir = 'path/to/files';
opendir (DIR,$dir) || die "cannot open: $!";
@dirfiles =readdir(DIR);
close (DIR);
foreach $name (@dirfiles)
{
if ( -d "[b]$dir/$name[/b]")
{
..... 
}

obviously if you chdir after you open the dir it seems to mess things up. I never did that before so we both learned somthing new.

- Kevin, perl coder unexceptional!
 
The orignal problem happened when I was using the whole path - c:\scripts\test
 
It works now. Thank you very much for your help.

Irene.
 
you're welcome [smile]

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top