Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Perl: globbing filenames with spaces in them

Perl: globbing filenames with spaces in them
Thread Tools
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
May 29, 2002, 04:41 PM
 
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 &quot;$_/*&quot;;</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
Reply With Quote
May 30, 2002, 11:03 AM
 
</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(&quot;*&quot;

my $filename;
foreach $filename (@filenames)
{
print &quot;$filename\n&quot;;
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Hope that helps.

-JAPH
     
seb2  (op)
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
May 30, 2002, 03:04 PM
 
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?
     
seb2  (op)
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
May 31, 2002, 11:27 AM
 
ok, i found it. -- no more need to help.

thanks anyway.
     
Senior User
Join Date: Sep 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Jun 1, 2002, 06:50 AM
 
why not do a

opendir
@list_of_files = readdir;
closedir
     
seb2  (op)
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
Jun 1, 2002, 08:03 AM
 
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.
     
   
Thread Tools
Forum Links
Forum 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 On
Top
Privacy Policy
All times are GMT -5. The time now is 01:16 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2