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 Joe Smith on March 31, 2008, 3:52 am
Please log in for more thread options
Dmitry wrote:

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

Yes, after converting wildcard characters into regex characters, of course.

Posted by szr on March 31, 2008, 3:16 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?

I find, just as in geenral under Win32, putting double quotes around the
path gets around problems like this:

C:\>perl -e "my @d = glob('"""C:/Documents and Settings"""/*'); print
qq, join(qq, @d), qq;"

C:/Documents and Settings/Administrator
C:/Documents and Settings/All Users
[...]

*** Note that """, when used in a double quoted string, under the
cmd.exe shell yields a literal ", so the glob statement is effectively:

glob('"C:/Documents and Settings"/*');

*** This is only because the command was run from the command line; in
an actual script you would of course use a normal double quote around
the path (just like in the linux examples below.)


And this works for tests like -d as well:

C:\>perl -e "print int (-d """C:/Documents and Settings""")"
1
C:\>perl -e "print int (-d """C:/123Documents and Settings""")"
0


And this form works under linux as well:

$ perl -e 'my @d = glob(q{"/mnt/samba/win_hd/Documents and
Settings"/*}); print qq, join(qq, @d), qq;'

/mnt/samba/win_hd/Documents and Settings/Administrator
/mnt/samba/win_hd/Documents and Settings/All Users

$ perl -e 'print int (-d "/mnt/samba/win_hd/Documents and Settings")'
1
$ perl -e 'print int (-d "/mnt/samba/win_hd/123Documents and Settings")'
0

This was tested under ActivePerl 5.6.1 and 5.8.7, and under linux using
5.10.0, 5.8.8, and 5.6.1.


So if you want to do it in a way that works on most platforms (at the
very least windows and *nix),

1) Use a forward slash, not a back slash, as a path delimiter.
I.E., C:/path to/somewhere/file.ext, and

2) Surround the path with quotes.
I.E., "C:/path to/somewhere/a long filename.ext", or
"C:/path to/somewhere"/file.ext, or
"C:/Documents and Settings/"

and you should be fine.

Hope this helps.

--
szr



Posted by Bart Lateur on March 31, 2008, 4:38 pm
Please log in for more thread options
Dmitry wrote:

>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?

Is a simple conversion acceptable?

If you put double quotes aroudn the path *in* the string for glob, then
it'll work.

($\, $,) = ("\n", "\t");
chdir 'c:/temp';
foreach('C:/Documents and Settings', 'C:\Documents and Settings') {
print $_, glob(qq("$_")), -d $_ || 0;
}

Result:

C:/Documents and Settings        C:/Documents and Settings        1
C:\Documents and Settings        C:./Documents and Settings        1

Well, ok... the response of glob to a backslash *is* weird. But at
least, it seems to work.

--
        Bart.

Posted by Ben Morrow on March 31, 2008, 4:57 pm
Please log in for more thread options

>
> Result:
>
> C:/Documents and Settings        C:/Documents and Settings        1
> C:\Documents and Settings        C:./Documents and Settings        1
>
> Well, ok... the response of glob to a backslash *is* weird. But at
> least, it seems to work.

Not just weird: wrong. Win32 has a notion of 'current directory on a
given drive'; C:./Documents and Settings is a path relative to the
current directory on drive C:.

Ben


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