Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Windows paths in glob

Reply
Thread Tools

Windows paths in glob

 
 
szr
Guest
Posts: n/a
 
      03-31-2008
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{\n}, join(qq{\n}, @d), qq{\n};"

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{\n}, join(qq{\n}, @d), qq{\n};'

/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


 
Reply With Quote
 
 
 
 
Bart Lateur
Guest
Posts: n/a
 
      03-31-2008
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.
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      03-31-2008

Quoth Bart Lateur <(E-Mail Removed)>:
>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
glob.glob output Hitesh Python 6 03-13-2007 03:45 PM
glob.glob unicode bug or feature Elbert Lev Python 5 08-02-2004 12:09 AM
Question about glob.glob <--newbie Sean Berry Python 3 05-04-2004 05:34 PM
RE: Bug in glob.glob for files w/o extentions in Windows Tim Peters Python 1 12-01-2003 09:22 AM
Bug in glob.glob for files w/o extentions in Windows Georgy Pruss Python 15 12-01-2003 04:04 AM



Advertisments