in reply to I go crazy with windows filenames with spaces!
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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: I go crazy with windows filenames with spaces!
by Anonymous Monk on Jan 20, 2010 at 03:29 UTC | |
by Marshall (Canon) on Jan 20, 2010 at 04:54 UTC | |
by ikegami (Patriarch) on Jan 20, 2010 at 16:02 UTC | |
by Anonymous Monk on Jan 20, 2010 at 09:49 UTC |