Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > globbing mess

Reply
Thread Tools

globbing mess

 
 
MSG
Guest
Posts: n/a
 
      01-14-2006
I can't seem to get a grasp on globbing when a directory has a white
space in it.
Here is my code snippet on Win32:

my $dir = 'c:/program files';
my @files = glob "$dir/*";
foreach (@files) { print $_, "\n" };

the output is just:
c:/program
instead of a list of files/directories.

I found two ways to get around the problem:
one is to add a backslash in front of the white space like this
my $dir = 'c:/program\ files';
the other is to double on quotes like this:
my $dir = '"c:/program files"';

But I am wondering what is the escaping/quoting rule with globbing.

One other thing seems odd: It doesn't matter if there is a trailing
slash or not:
both $dir='c:/program\ files' and $dir='c:/program\ files/' work. I
would think that
c:/program files// would mess up the code...

 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      01-14-2006

"MSG" <> wrote in message
news: oups.com...
>I can't seem to get a grasp on globbing when a directory has a white
> space in it.
> Here is my code snippet on Win32:
>
> my $dir = 'c:/program files';
> my @files = glob "$dir/*";


my @files = glob "'$dir/*'";

Should do the trick. Be aware that you're not just going to get files, but
directories as well. If you just want files try *.* instead.

Matt


 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      01-15-2006

Matt Garrish wrote:
> "MSG" <> wrote in message
> news: oups.com...
> >I can't seem to get a grasp on globbing when a directory has a white
> > space in it.
> > Here is my code snippet on Win32:
> >
> > my $dir = 'c:/program files';
> > my @files = glob "$dir/*";

>
> my @files = glob "'$dir/*'";
>
> Should do the trick. Be aware that you're not just going to get files, but
> directories as well. If you just want files try *.* instead.


what about extensionless files?

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      01-15-2006
Matt Garrish:

> my @files = glob "'$dir/*'";
>
> Should do the trick. Be aware that you're not just going to get
> files, but directories as well. If you just want files try *.*
> instead.


The presence or absence of a dot in the name, is not the distiguishing
feature between files and directories.

#!/usr/bin/perl

use strict; use warnings;
use File::Glob ':glob';

{ local $\ = $/;
for (bsd_glob '*', GLOB_MARK | GLOB_NOSORT | GLOB_ERR) {
print if m(/$);
}
}


I don't know yet why

while (glob '*') { print }

works and

while (bsd_glob '*') { print }

doesn't.

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      01-15-2006

"it_says_BALLS_on_your forehead" <> wrote in message
news: ups.com...
>
> Matt Garrish wrote:
>> "MSG" <> wrote in message
>> news: oups.com...
>> >I can't seem to get a grasp on globbing when a directory has a white
>> > space in it.
>> > Here is my code snippet on Win32:
>> >
>> > my $dir = 'c:/program files';
>> > my @files = glob "$dir/*";

>>
>> my @files = glob "'$dir/*'";
>>
>> Should do the trick. Be aware that you're not just going to get files,
>> but
>> directories as well. If you just want files try *.* instead.

>
> what about extensionless files?
>


You're right, even if they are incredibly rare on Windows...

Matt


 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      01-15-2006

"Dr.Ruud" <rvtol+> wrote in message
news:...
> Matt Garrish:
>
>> my @files = glob "'$dir/*'";
>>
>> Should do the trick. Be aware that you're not just going to get
>> files, but directories as well. If you just want files try *.*
>> instead.

>
> The presence or absence of a dot in the name, is not the distiguishing
> feature between files and directories.
>


I know, but I was going to eat dinner and not really thinking things
though... : )

Matt


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      01-15-2006
Matt Garrish:
> it_says_BALLS_on_your forehead:
>> Matt Garrish:


>>> my @files = glob "'$dir/*'";
>>>
>>> Should do the trick. Be aware that you're not just going to get
>>> files, but
>>> directories as well. If you just want files try *.* instead.

>>
>> what about extensionless files?

>
> You're right, even if they are incredibly rare on Windows...


That must be a local thing. I have a Windows-2000 system here with many
files that have no dot in the name.

dir c:\ /s/a-d/b | find /v "."

(skips directory names with a "." too, but already returns plenty)


And many directory names with an embedded dot too, even as the first
character.

dir c:\ /s/ad/b | find "."

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-15-2006
MSG <> wrote:

> I can't seem to get a grasp on globbing when a directory has a white
> space in it.



Using readdir/grep is an alternative to globbing.


> my @files = glob "$dir/*";


# untested
opendir DIR, $dir or die "could not open '$dir' $!";
my @files = grep /^[^.]/, readdir DIR;
closedir DIR;


(but you may need to remember to paste the $dir part onto the
front of each array element.
)


> But I am wondering what is the escaping/quoting rule with globbing.



The docs say glob() uses csh rules.


> One other thing seems odd: It doesn't matter if there is a trailing
> slash or not:
> both $dir='c:/program\ files' and $dir='c:/program\ files/' work. I
> would think that
> c:/program files// would mess up the code...



If you tell us why you think that would mess up the code, then
we might be able to clear up whatever misunderstanding you have
that led to that conclusion.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      01-15-2006

"Dr.Ruud" <rvtol+> wrote in message
news:...
> Matt Garrish:
>> it_says_BALLS_on_your forehead:
>>> Matt Garrish:

>
>>>> my @files = glob "'$dir/*'";
>>>>
>>>> Should do the trick. Be aware that you're not just going to get
>>>> files, but
>>>> directories as well. If you just want files try *.* instead.
>>>
>>> what about extensionless files?

>>
>> You're right, even if they are incredibly rare on Windows...

>
> That must be a local thing. I have a Windows-2000 system here with many
> files that have no dot in the name.
>


Less than 2% of all files is not common (see File-Find question I posted),
especially when removing uninstall and some application data files from the
list reduces that number to a fraction of 1%. I'm curious what Windows files
you have that don't have extensions?

Matt


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      01-15-2006
Matt Garrish:

> [extension-less files on a Windows-system]
> Less than 2% of all files is not common (see File-Find question I
> posted), especially when removing uninstall and some application data
> files from the list reduces that number to a fraction of 1%. I'm
> curious what Windows files you have that don't have extensions?



s/common/uncommon/ ?

On that Win2000-system I see all sort of files without a dot, like:
- WordPerfect-5 documents, from an age that it was the only application
in the world (for certain users)
- data files of an MSDOS bookkeeping program
- many system files like c:/CFGSAFE/QCINIT/NT*/hlm*
- c:/Documents and Settings/Administrator/Application
Data/Microsoft/CryptnetUrlCache/Content/*
- c:/Perl/*/*
- c:/Program Files/Common Files/InstallShield/Driver/8/Intel 32/ID
- c:/Program Files/WinSCP3/licence
- c:/RECYCLER/S-1-5-21-839522115-1383384898-1343024091-500/INFO2
- c:/WINNT/ShellIconCache
- c:/WINNT/*/reg*
- c:/WINNT/repair/*
- c:/WINNT/system32/drivers/etc/*
so mostly system files.

On this system (with 201500 files) there are 4200 such files, of which
almost 3000 belonging to cygwin.
So without the cygwin files it is about 0.5% netto.

There are 6320 directories, of which 1920 with a dot in the name, of
which about 850 of cygwin or with version numbers, so netto about 17%.
Most system or application directories, but also many that are
user-made, for example from website names like "www.example.com/".

--
Affijn, Ruud

"Gewoon is een tijger."

 
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
globbing source for C Owner C Programming 7 05-16-2011 03:14 AM
Globbing files by their creation date tkpmep@hotmail.com Python 3 01-17-2007 04:31 PM
File Copying With Globbing Daz C++ 3 05-03-2006 11:08 AM
globbing multiple wildcards utabintarbo@gmail.com Python 5 05-07-2005 10:40 PM
Case-insensitive globbing Thomas Philips Python 4 06-03-2004 08:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57