Click here to get back home

Windows paths in glob

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Windows paths in glob Dmitry 03-30-2008
Posted by Dmitry on March 30, 2008, 3:09 pm
Please log in for more thread options
OK, so there's a well-known difficulty with handling Windows-style paths in
glob: it doesn't
like backslashes, nor does it like spaces. One solution to that is to use
Unix-style paths:

glob('C:\Documents and Settings\*'); # Doesn't work
glob('C:/Documents\ and\ Settings/*'); # Works

Problem is, the rest of Perl's built-in file-handling functionality behaves the
other way around.
For instance, with -d:

-d 'C:\Documents and Settings'; # Works
-d 'C:/Documents\ and\ Settings'; # Doesn't work

Question: is there any way to use the same path string with glob and with the
rest of Perl,
without having to convert them back and forth?


Posted by John W. Krahn on March 30, 2008, 3:16 pm
Please log in for more thread options
Dmitry wrote:
> OK, so there's a well-known difficulty with handling Windows-style paths in
glob: it doesn't
> like backslashes, nor does it like spaces. One solution to that is to use
Unix-style paths:
>
> glob('C:\Documents and Settings\*'); # Doesn't work
> glob('C:/Documents\ and\ Settings/*'); # Works
>
> Problem is, the rest of Perl's built-in file-handling functionality behaves
the other way around.
> For instance, with -d:
>
> -d 'C:\Documents and Settings'; # Works
> -d 'C:/Documents\ and\ Settings'; # Doesn't work
>
> Question: is there any way to use the same path string with glob and with the
rest of Perl,
> without having to convert them back and forth?

perldoc File::DosGlob
perldoc File::Spec
perldoc File::Basename


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by Dmitry on March 31, 2008, 1:54 am
Please log in for more thread options

> Dmitry wrote:
>> OK, so there's a well-known difficulty with handling Windows-style
>> paths in glob: it doesn't like backslashes, nor does it like spaces.
>> One solution to that is to use Unix-style paths:
>>
>> glob('C:\Documents and Settings\*'); # Doesn't work
>> glob('C:/Documents\ and\ Settings/*'); # Works
>>
>> Problem is, the rest of Perl's built-in file-handling functionality
>> behaves the other way around. For instance, with -d:
>>
>> -d 'C:\Documents and Settings'; # Works
>> -d 'C:/Documents\ and\ Settings'; # Doesn't work
>>
>> Question: is there any way to use the same path string with glob and
>> with the rest of Perl, without having to convert them back and forth?
>
> perldoc File::DosGlob
> perldoc File::Spec
> perldoc File::Basename

I tried DosGlob, but when I passed it 'C:\Documents and Settings\*' it bugged
out with an
error somewhere in the module...

Posted by Martijn Lievaart on March 30, 2008, 3:27 pm
Please log in for more thread options
On Sun, 30 Mar 2008 19:09:18 +0000, Dmitry wrote:

> OK, so there's a well-known difficulty with handling Windows-style paths
> in glob: it doesn't like backslashes, nor does it like spaces. One
> solution to that is to use Unix-style paths:
>
> glob('C:\Documents and Settings\*'); # Doesn't work glob('C:/Documents\
> and\ Settings/*'); # Works
>
> Problem is, the rest of Perl's built-in file-handling functionality
> behaves the other way around. For instance, with -d:
>
> -d 'C:\Documents and Settings'; # Works -d 'C:/Documents\ and\
> Settings'; # Doesn't work
>
> Question: is there any way to use the same path string with glob and
> with the rest of Perl, without having to convert them back and forth?

I don't have Windows to test here, but I recall that using either a
forward slash '/' or a backward slash -- properly escaped -- '\' works
either way in both situations.

In the examples you gave, the versions with backslashes cannot work, the
backslashes are not escaped.

M4

Posted by Peter J. Holzer on March 30, 2008, 5:44 pm
Please log in for more thread options
> On Sun, 30 Mar 2008 19:09:18 +0000, Dmitry wrote:
>> OK, so there's a well-known difficulty with handling Windows-style paths
>> in glob: it doesn't like backslashes, nor does it like spaces. One
>> solution to that is to use Unix-style paths:
>>
>> glob('C:\Documents and Settings\*'); # Doesn't work
>> glob('C:/Documents\ and\ Settings/*'); # Works

I didn't expect that but on second thought it makes sense.


>> Problem is, the rest of Perl's built-in file-handling functionality
>> behaves the other way around. For instance, with -d:
>>
>> -d 'C:\Documents and Settings'; # Works -d 'C:/Documents\ and\
>> Settings'; # Doesn't work
>>
>> Question: is there any way to use the same path string with glob and
>> with the rest of Perl, without having to convert them back and forth?
>
> I don't have Windows to test here, but I recall that using either a
> forward slash '/' or a backward slash -- properly escaped -- '\' works
> either way in both situations.

You misunderstood the problem. The problem is that glob patterns, like
regexps are mini-languages where some characters (or sequences of
characters) have a special meaning. Just as you cannot just use any
string as a regexp and expect it to match itself (or even be a
well-formed regexp) you cannot use any filename as a glob pattern and
expect it to expand to itself. Actually, for globs the situation is
worse: While any string can be converted to a regexp matching that
string, this is not true for globs. Spaces can be escaped with a
backslash, but I didn't find any way to escape an asterisk or question
mark.

So I guess Gunnar's advice is the best: If you need to deal with
arbitrary file and directory names, avoid glob and use opendir/readdir.
Or maybe File::Find or a similar module (which uses opendir/readdir
internally).

        hp

Similar ThreadsPosted
Paths help March 12, 2006, 7:05 am
Using UNC paths and ssh with perl August 16, 2005, 3:52 am
spaces in paths August 29, 2005, 2:09 pm
Checking for safe paths March 9, 2005, 7:59 pm
Expanding tree paths July 19, 2005, 10:48 am
Getting 'system' to Process Win32 Paths Correctly July 14, 2005, 6:38 am
File::Find dies on directory paths which are too long March 13, 2007, 2:53 pm
Migrating thousands of user home, profile, apps and groups directory paths August 20, 2005, 9:51 am
glob February 23, 2005, 4:48 pm
Glob Q June 12, 2005, 12:31 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap