Anmol
Anmol

Reputation: 29

-f command in Perl not working on my system

I have windows 7, I am trying to open a directory and print all filenames in the directory but the -f command is not working on my system. The program is as follows:

enter code here

$directory = '\Users';
opendir(DIR, $directory) or die $!;
while ($file = readdir(DIR)){
    if(-f $file){
        print "$file\n";
    }
}
closedir(DIR);

can anybody tell me what's wrong? Thanks in Advance.

Upvotes: 1

Views: 138

Answers (2)

Yi Zhao
Yi Zhao

Reputation: 6784

You should test with the full path name. $file only contains the file name.

if ( -f "$directory\\$file" ){
    print "$file\n";
}

Upvotes: 1

chrsblck
chrsblck

Reputation: 4088

You're missing the path to the file.

It should be something like this:

if(-f "$directory/$file") {

Upvotes: 3

Related Questions