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 31, 2008, 1:58 am
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

Spaces are a more serious problem than slashes. But anyway, the examples work,
because I used single quotes. BTW, current core glob seems to ignore
backslashes
altogether, unless they escape something other than a backslash.

Posted by Gunnar Hjalmarsson on March 30, 2008, 4:05 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?

A long time ago I decided to use opendir() and readdir() instead of
glob(). It may not be as 'elegant', but it works flawlessly without
escaping spaces.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by Ben Morrow on March 30, 2008, 6:48 pm
Please log in for more thread options

>
> A long time ago I decided to use opendir() and readdir() instead of
> glob(). It may not be as 'elegant', but it works flawlessly without
> escaping spaces.

To save Uri the trouble of pointing it out :), File::Slurp now has a
read_dir function.

Ben


Posted by Uri Guttman on March 30, 2008, 7:20 pm
Please log in for more thread options

>>
>> A long time ago I decided to use opendir() and readdir() instead of
>> glob(). It may not be as 'elegant', but it works flawlessly without
>> escaping spaces.

BM> To save Uri the trouble of pointing it out :), File::Slurp now has a
BM> read_dir function.

it has always had a read_dir function! its advantages are a simpler API
(no need for a handle, opendir, closedir calls) and it filters out . and
.. for you. a minor disadvantage (and very minor IMO) is that it can't
iterate in scalar mode so you get one dir entry at a time. that would
only matter if your dir was enormous and i mean very big.

future plans include passing in a regex or code ref to filter for
you. yeah, you can use grep on the output but it is slightly shorter
that way.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Posted by Dmitry on March 31, 2008, 2:09 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?
>
> A long time ago I decided to use opendir() and readdir() instead of
> glob(). It may not be as 'elegant', but it works flawlessly without
> escaping spaces.
>

OK, thanks. I guess if I wanted to process wildcards in the file name, I would
pass them
through grep?

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