Beginner wrote:
>
> On 17 Oct 2006 at 13:16, Perl beginners wrote:
>>
>> Hi All,
>>
>> I am knocking my head against a wall trying to work with directories
>> with spaces.
>>
>> I need to translate some file paths like the one below:
>>
>> /data/scanning/phil/edits/gary/finished/STI 9-10-06/E0102.tif
>>
>> into
>> /var/www/phil/pix/E0102.jpg
>>
>> (I need the path to the tif to make the jpeg).
>>
>> If I glob the files in the parent directory and then File::Basename I
>> end up with 'STI' as the filename and the rest of the program fails.
>>
>> foreach my $d (@dirs) {
>> print STDERR "reading files from $d.\n";
>> my @tif = glob("$d/*tif");
>> foreach my $t (@tif) {
>> my $base = basename("$t");
>> (my $j = $base) =~ s/tif$/jpg/;
>> print STDERR "Found $base $j $t\n";;
>> push(@names,$n);
>> }
>> }
>>
>> Is there some quoting scheme I should be using? Or do I have to use a
>> regex to grab the filename from the end of the string?
>
> I opted for the readdir() function instead. Worked a treat and was
> less code.
>
> foreach my $d (@dirs) {
> print STDERR "reading directory $d\n";
> opendir(DIR,$d) || die "Can't open $d: $!\n";
> @names = grep {! /^\./ && -f "$d/$_" && "$d/$_" =~ /(tiff|tif)$/}
readdir(DIR);
> }
>
Hi Dermot
(Than you for bottom-posting: it makes a thread so much more readable)
Your initial problem wasn't with basename, it was with glob(), as you would have
seen if you'd examined the contents of @tif. Without delimiters, glob sees its
parameter as a list of two items, the file
/data/scanning/phil/edits/gary/finished/STI (which doesn't exist, but that's
fine because it's not wildcarded) and 9-10-06/*tif (which also doesn't exist,
but because it's wildcarded glob looks for matches and finds none, so the
returned list is just the first 'file' called STI. If you write:
my @tif = glob("'$d/*tif'");
then the whole file glob is treated as a single entity and everything works
correctly.
However, having fixed this your second post shows that you also want *.tiff
files, which you could do by
my @tif = (glob("'$d/*tif'"), glob("'$d/*tiff'"));
or by using readdir and regexes as you did. May I tidy this up a little? If the
grep { ! /^\./ } is there to remove the '.' and '..' pseudo-directories then
these are already excluded by -f, so you could write:
@names = grep { -f and /\.tiff?$/ } readdir DIR;
which is a little clearer I think.
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>