in reply to check if it is a file or directory

I think that you're making things too hard on yourself. Just keep it simple. I tried this, and I double-checked. It's easier, and it is correct:
#!/usr/bin/perl use strict; use warnings; chdir('/path/to/oldFolder'); my @files = <*>; foreach my $file (@files) { if ( -f $file ) { print $file . " file \n"; } if ( -d $file ) { print $file . " directory \n"; } }

Replies are listed 'Best First'.
Re^2: check if it is a file or directory
by toolic (Bishop) on Feb 27, 2010 at 01:52 UTC
    ... but your solution does not do what the OP wants:
    • Only print directory names (yours also prints file names).
    • Do not read all the names into an array at once -- just operate on each name, one at a time (yours stuffs all names into an array, then it loops through the array).
      But changing dir maybe a better solution for a more compact code. For example:
      $oldFolder='C:\Documents and Settings\user\Desktop'; opendir OF, $oldFolder or die "\nCannot open dir $old1Folder: $!\n"; chdir $oldFolder; while ( my $name=readdir OF) { next if $name =~ /^\./ or -f $name; # not interesting . o .. (or hidden files) neither in plain files print $name; } closedir OF;
      now -f test in oldFolder.
        I incorporated elements of your script into this:
        #!/usr/bin/perl use strict; use warnings; use Path::Extended::Dir; my $dir = Path::Extended::Dir->new('/root/Desktop'); $dir->open; chdir($dir); while( my $entry = $dir->read($dir) ) { next if $entry =~ /^\./ or -f $entry; print $entry, "\n"; } $dir->close;
        I decided to use Path::Extended::Dir to try to get better control.