 |
 |
Perl: globbing filenames with spaces in them
|
 |
|
 |
|
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status:
Offline
|
|
hi everybody,
first of all, i did rtfm and i searched the net for quite some time. i did find related thing, but no solution.
what i need is this: something simple to get the complete paths to all files in a directory. the name of the directory is in a variable.
what i'm doing now is this:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">push @files, glob "$_/*";</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">this works fine if the directory name in $_ doesn't contain a space. but if it does... no go.
any clues?
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2001
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by seb2:
<strong>
what i need is this: something simple to get the complete paths to all files in a directory. the name of the directory is in a variable.
any clues?</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">This will list all the files in your current directory, including with spaces:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">#!/usr/bin/perl -w
use strict;
my @filenames = glob("*"  ;
my $filename;
foreach $filename (@filenames)
{
print "$filename\n";
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Hope that helps.
-JAPH
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status:
Offline
|
|
first of all, thanks.
i should have mentioned that the line above is only a part of a script that is being called from another application and also hands it a path from which it recursively traverses all subdirectories.
it resolves paths to directories just fine as long as the name of the directory doesn't have a space in its name. -- say: the script partially works.
i've come so far to find out that a space in the filename makes glob take this as two different arguments -- which of course is not what i want.
on several sites, i read that i should use "use File::Glob ':glob';" or "use File::Glob qw(:globally :case);" and that this would override the built-in glob and make it work.
but somehow, i can't get this to work. any other ideas?
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status:
Offline
|
|
ok, i found it. -- no more need to help.
thanks anyway.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Sep 2000
Location: Boston, MA
Status:
Offline
|
|
why not do a
opendir
@list_of_files = readdir;
closedir
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status:
Offline
|
|
thanks, howardm4!
your suggestion is pretty much what i'm doing.
to be exact, it's this:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;"> opendir TMPDIR, $_;
while (my $fle = readdir TMPDIR)
{
next if $fle =~ /^\.\.?$/;
# this is where i do stuff with the file
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">using a glob would have been nicer, though, because
a) it returns complete paths -- which is what i need in my context
b) it doesn't return "." and ".."
but -- whatever -- it now works.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|