globbing mess

M

MSG

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...
 
M

Matt Garrish

MSG said:
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
 
I

it_says_BALLS_on_your forehead

Matt said:
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?
 
D

Dr.Ruud

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.
 
M

Matt Garrish

Dr.Ruud said:
Matt Garrish:


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
 
D

Dr.Ruud

Matt Garrish:
it_says_BALLS_on_your forehead:

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 "."
 
T

Tad McClellan

MSG said:
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.
 
M

Matt Garrish

Dr.Ruud said:
Matt Garrish:

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
 
D

Dr.Ruud

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/".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top