Re: Problem with glob and filenames containing '[' and ']'
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxx
- Date: 27 Sep 2006 13:57:26 GMT
David Squire <David.Squire@xxxxxxxxxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
Hi folks,
I'm having trouble using glob to find filenames that contain '[' and
']', even though I am escaping those meta-characters. Here is an example
script and output:
I don't know what goes wrong for you. It works for me as expected
(after replacing /damocles/documents/ENH1260/2006/2/Short assignment/
with something that exists on my box).
----
#!/usr/bin/perl
use strict;
use warnings;
use CGI::Deurl;
for my $EncodedFile (
'/damocles/documents/ENH1260/2006/2/Short
assignment/20331975_week9%5B1%5D.txt',
'/damocles/documents/ENH1260/2006/2/Short
assignment/20331975_week9.txt',
) {
my $OriginalFileBase = deurlstr($EncodedFile);
$OriginalFileBase =~ s/\.[^.]+$//; # trim extension
$OriginalFileBase =~ s/([\[\]{}?*~\ ,'`"])/\\$1/g; # escape
characters that are meta in glob;
You can use quotemeta() instead of your s///. That quotes a little more
(most visibly "/"), but that doesn't hurt.
Anno
[remainder left for reference]
print "\$OriginalFileBase = $OriginalFileBase\n";.
my @CandidateOrigFiles = glob ("$OriginalFileBase*");
print "\@CandidateOrigFiles:\n", join "\n", @CandidateOrigFiles;
print "\n###########################################################\n";
}
----
Output:
Sep 27 - 9:31pm % ./test.pl
<ENTER THE CGI QUERY. End with CTRL+D>
$OriginalFileBase = /damocles/documents/ENH1260/2006/2/Short\
assignment/20331975_week9\[1\]
@CandidateOrigFiles:
###########################################################
$OriginalFileBase = /damocles/documents/ENH1260/2006/2/Short\
assignment/20331975_week9
@CandidateOrigFiles:
/damocles/documents/ENH1260/2006/2/Short
assignment/20331975_week9%5B1%5D.txt
/damocles/documents/ENH1260/2006/2/Short
assignment/20331975_week9%5B1%5D.txt.webbed
/damocles/documents/ENH1260/2006/2/Short assignment/20331975_week9[1].doc
###########################################################
----
As you can see, the first iteration of the for loop produces no matches.
I have included the second, shortened filename, example to demonstrate
that the file I want really does exist. Likewise, at the bash prompt I
can do:
Sep 27 - 9:31pm % ls /damocles/documents/ENH1260/2006/2/Short\
assignment/20331975_week9\[1\]*
/damocles/documents/ENH1260/2006/2/Short assignment/20331975_week9[1].doc
I am at a loss...
DS
- Follow-Ups:
- Re: Problem with glob and filenames containing '[' and ']'
- From: David Squire
- Re: Problem with glob and filenames containing '[' and ']'
- References:
- Problem with glob and filenames containing '[' and ']'
- From: David Squire
- Problem with glob and filenames containing '[' and ']'
- Prev by Date: glob problem: escaped space seems to be significant too (was Re: Problem with glob and filenames containing '[' and ']')
- Next by Date: Re: Problem with glob and filenames containing '[' and ']'
- Previous by thread: Re: glob problem: escaped space seems to be significant too (was Re: Problem with glob and filenames containing '[' and ']')
- Next by thread: Re: Problem with glob and filenames containing '[' and ']'
- Index(es):
Relevant Pages
|