PerlingTheUK has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am using Tk for an application on Win32 and wanted to use the most windowish looking chooseDirectory dialog to select a directory. This returns a directory of the sort:

C:/Documents and Settings/UserX/Desktop/WORK/data/var - 21/dat
In a next step I need to get all xml files in that directory. Which is the point where I struggle.

Globbing for these values returns an arrat that is split at the spaces and terminates before the -:

@files = glob( $path . "/*.xml ); print join ( "\n", @files ); <code> This returns: <code> C:/Documents and Settings/UserX/Desktop/WORK/data/var

I have also tried to alter the path to a Dos standard.

$path =~ s/\//\\\\/g; $path =~ s/\ /\\\ /g;
And then globbed without results.

And I also tried to use File::DosGlob.

Both of the last efforts did return an empty array from the glob function. Does anyone know what the bast way to tackle this problem is?


Cheers,
PerlingTheUK

Replies are listed 'Best First'.
Re: Tk::chooseDirectory and glob problem
by ikegami (Patriarch) on Jun 25, 2005 at 08:00 UTC

    Adding quotes works:

    $path = "C:/Documents and Settings/Administrator"; @files = glob(qq{"$path/*.xml"}); $\ = $, = "\n"; print @files; # C:/Documents and Settings/Administrator/file.xml

    or do the globbing yourself:

    $path = "C:/Documents and Settings/Administrator"; local *DIR; opendir(DIR, "C:/Documents and Settings/Administrator"); @files = map { "$path/$_" } grep { /\.xml$/i } readdir(DIR); $\ = $, = "\n"; print @files; # C:/Documents and Settings/Administrator/file.xml

    Note that the path must be prepended when using readdir.

Re: Tk::chooseDirectory and glob problem
by Zaxo (Archbishop) on Jun 25, 2005 at 07:59 UTC

    It looks like spaces in paths are giving you grief. Try applying quotemeta to them. That is a tricky corner for cross-platform code.

    After Compline,
    Zaxo

      Works fine. Thank you.

      Cheers,
      PerlingTheUK