The most portable way to get the "files" in a directory is to just open that directory and use grep{} to filter the names that you want. See below. Note that readdir does not give full paths, you have to prepend the directory to the $_ variable.
Use '/', forward slash instead of '\' for file names. Windows Perl will do the "right thing". There is no need for '\\' and "backslash" unless some pathname has to go to the Windows shell.
Updated: to show map{} function better.
#!/usr/bin/perl -w use strict; my $user = 'SOME_USER'; my $directory = "c:/documents and settings/$user/"; opendir (MYDIR, $directory) || die "unable to open $directory"; my @file_paths = map {"$directory/$_"} grep{ -f "$directory/$_" } readdir MYDIR; # The below means the same thing. But I think that the # above grep/map filter combo is easier to understand. #my @file_paths = map { -f "$directory/$_" # ? "$directory/$_" # : () # } readdir MYDIR; # # return"()" from a map{} to return "nothing" # to get all the files (directories are files).... # my @file_paths = map{ "$directory/$_"} readdir MYDIR; foreach my $file (@file_paths) { print "$file\n"; }
In reply to Re: I go crazy with windows filenames with spaces!
by Marshall
in thread I go crazy with windows filenames with spaces!
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |