Re: Problem with glob and filenames containing '[' and ']'



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
.



Relevant Pages

  • Problem with glob and filenames containing [ and ]
    ... I'm having trouble using glob to find filenames that contain '[' and ... script and output: ... characters that are meta in glob; ... @CandidateOrigFiles: ...
    (comp.lang.perl.misc)
  • Re: Problem with glob and filenames containing [ and ]
    ... I'm having trouble using glob to find filenames that contain '[' and ... even though I am escaping those meta-characters. ... I think that's going to confuse glob big-time. ...
    (comp.lang.perl.misc)
  • Re: tclhttpd with utf-8
    ... it changed the data format from the non utf-8 format (that I was ... translation binary suppose to skip encoding translation? ... The problem is most likely the [glob] command, ... the filenames that cause trouble, then run your tclsh script with the ...
    (comp.lang.tcl)
  • Dumb glob question
    ... I've run into an issue with glob and matching filenames with brackets '' ...
    (comp.lang.python)
  • Re: A globbing question
    ... I'm trying to figure out how to "glob" lists instead of the current ... This fragment will print all the filenames started with 'a' in the ...
    (comp.lang.perl.misc)