in reply to I go crazy with windows filenames with spaces!

I don't understand what your second code is supposed to do.

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
    Just a note wrt Windows paths: perl does the right thing because Windows does the right thing with forward-slashes as directory separators. It's just that almost none of Windows userland does.

    The only example I can think of for a tool doing the right thing off hand is chdir in cmd.exe: cd /d c:/windows.

      I said that: Windows Perl will do the "right thing". That's a bit different than "Windows does the right thing". Perl has an amazing capacity to "un-screw" Windows quirks.

        Anonymonk is right. Perl doesn't do anything to the slashes. Windows accepts both forward and backward slashes as path seps.

        Individual command line tools and applications might not handle forward slashes, and some require that you quote the path if it contain a forward slash (to distinguish it from command line options). But Windows itself doesn't care.

        It's a matter of Perl using Windows at the API level, not about making special cases.