On 17 Oct 2006 at 15:09, Rob Dixon wrote: > 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
Damm my cover is blown!! Hi Rob, > my @tif = glob("'$d/*tif'"); I am pretty sure I had tied this, it looks like it from my post. > 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. You are right, that reads much clearer and I should have employed a little more brain-power with the -f switch too. Thanx for the help, much appeciated. Dp. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>