Forum: Ruby File.basename problems

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2007-07-25 14:21
Why is it that File.basename works for me for one single file, but, it
doesn't work for an array of files. Here's an example of what worked for
me for one file; then, it didn't work for multiple files.

Thanks,
Peter


file    = "va998.tif"
pdffile = File.basename(file, ".tif") + ".pdf"
puts "#{file}   #{pdffile}"

   yields:
   va998.tif   va998.pdf


Dir.glob("*.tif").each do |tiffile|
pdffile = File.basename(tiffile, ".tif") + ".pdf"
puts "#{tiffile}   #{pdffile}"
end

   yields:

   va992.tif   va992.tif.pdf
   va993.tif   va993.tif.pdf
   va994.tif   va994.tif.pdf
   ...
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-07-26 00:46
(Received via mailing list)
On 7/25/07, Peter Bailey <pbailey@bna.com> wrote:
> puts "#{file}   #{pdffile}"
>    yields:
>
>    va992.tif   va992.tif.pdf
>    va993.tif   va993.tif.pdf
>    va994.tif   va994.tif.pdf
>    ...
> --

I could not reproduce the error, just after touching 1.tif and 2.tif I
ran

505/5 > touch 1.tif
robert@PC:~/tmp/x 14:31:10
506/6 > touch 2.tif
robert@PC:~/tmp/x 14:31:14
507/7 > irb
irb(main):001:0> Dir.glob("*.tif").each do |tiffile|
irb(main):002:1* pdffile = File.basename(tiffile, ".tif") + ".pdf"
irb(main):003:1> puts "#{tiffile}   #{pdffile}"
irb(main):004:1> end
1.tif   1.pdf
2.tif   2.pdf
=> ["1.tif", "2.tif"]
irb(main):005:0>
???
Really strange
I got:
508/8 > ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-linux]
and you?

Robert
6d9bf78ca49a017e9e3e6b0357b6c59e?d=identicon&s=25 Peter Hickman (Guest)
on 2007-07-26 00:46
(Received via mailing list)
This works for me on:

OSX 10.4.10 : ruby 1.8.5 (2006-12-25 patchlevel 12) [powerpc-darwin]
Linux 2.6.10 : ruby 1.8.2 (2005-04-11) [i386-linux]
Windows XP : ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2007-07-26 00:46
(Received via mailing list)
On 7/25/07, Peter Bailey <pbailey@bna.com> wrote:
> puts "#{file}   #{pdffile}"
>
>    yields:
>    va998.tif   va998.pdf
>
>
> Dir.glob("*.tif").each do |tiffile|
> pdffile = File.basename(tiffile, ".tif") + ".pdf"
> puts "#{tiffile}   #{pdffile}"
> end

Works for me - what ruby version and OS? Try

p Dir.glob("*.tif")

p Dir.glob("*.tif").map {|f| File.basename(f, ".tif")}

and see if either one looks visibly funny.

martin
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2007-07-26 14:26
Here I'm using Ruby 1.8.5 on Windows Server 2003.
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2007-07-26 14:27
Peter Hickman wrote:
> This works for me on:
>
> OSX 10.4.10 : ruby 1.8.5 (2006-12-25 patchlevel 12) [powerpc-darwin]
> Linux 2.6.10 : ruby 1.8.2 (2005-04-11) [i386-linux]
> Windows XP : ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

I'm using Ruby 1.8.5 on Windows Server 2003.
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2007-07-26 14:31
> Works for me - what ruby version and OS? Try
>
> p Dir.glob("*.tif")
>
> p Dir.glob("*.tif").map {|f| File.basename(f, ".tif")}
>
> and see if either one looks visibly funny.
>
> martin

Both lines yield the same TIFF files. The second one shows the same TIFF
files, too, with the extension still there.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-07-26 15:14
(Received via mailing list)
On 7/26/07, Peter Bailey <pbailey@bna.com> wrote:
> Both lines yield the same TIFF files. The second one shows the same TIFF
> files, too, with the extension still there.
> --
> Posted via http://www.ruby-forum.com/.
>
>
Seems it is time to upgrade to 1.8.6, although I am not sure at all
that this will fix your problem.
Maybe there are some control characters at the end of the filenames of
the original tiff file?
No idea how to check this on Windows, but in Ruby you could check it
like this:

Dir.glob("*.tif").each do |tiffile|
########################
puts "I got you" unless /tif$/ === tiffile
########################
pdffile = File.basename(tiffile, ".tif") + ".pdf"

puts "#{tiffile}   #{pdffile}"
end

hmm does not seem possible, but try anyway :(

Cheers
Robert
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2007-07-26 16:15
> Dir.glob("*.tif").each do |tiffile|
> ########################
> puts "I got you" unless /tif$/ === tiffile
> ########################
> pdffile = File.basename(tiffile, ".tif") + ".pdf"
>
> puts "#{tiffile}   #{pdffile}"
> end
>
> hmm does not seem possible, but try anyway :(
>
> Cheers
> Robert

Thanks, Robert. I did upgrade to 1.8.6. But, using your suggestion here,
I get the same. Using my code above, I get the same, too.
...
I got you
VA996.TIF   VA996.TIF.pdf
end
puts
I got you
VA997.TIF   VA997.TIF.pdf
end
puts
I got you
VA998.TIF   VA998.TIF.pdf
end
...
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-07-26 16:59
(Received via mailing list)
On 7/26/07, Peter Bailey <pbailey@bna.com> wrote:
> > hmm does not seem possible, but try anyway :(
> >
> > Cheers
> > Robert
>
> Thanks, Robert. I did upgrade to 1.8.6.
That is a good thing anyway,
> puts
> I got you
> VA998.TIF   VA998.TIF.pdf
> end
> ...
> --
> Posted via http://www.ruby-forum.com/.
>
>
what about case? In your case I think that Glob pulls them in
uppercase and you check for lowercase...

Robert
Ec07fbda510a142f5331ffd903b07820?d=identicon&s=25 Gordon Thiesfeld (Guest)
on 2007-07-26 17:20
(Received via mailing list)
It seems to matter that your extensions are upper-cased:

>> Dir['*.tif']
=> ["1_lower.tif", "1_upper.TIF", "2_lower.tif", "2_upper.TIF"]

Running Roberts code gives me this:

1_lower.tif   1_lower.pdf
I got you
1_upper.TIF   1_upper.TIF.pdf
2_lower.tif   2_lower.pdf
I got you
2_upper.TIF   2_upper.TIF.pdf

Very interesting.
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (Guest)
on 2007-07-26 23:12
(Received via mailing list)
Hi,

At Thu, 26 Jul 2007 23:56:46 +0900,
Robert Dober wrote in [ruby-talk:261980]:
> what about case? In your case I think that Glob pulls them in
> uppercase and you check for lowercase...

Try File.basename(tiffile, ".*")
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2007-07-27 16:34
Gordon Thiesfeld wrote:
> It seems to matter that your extensions are upper-cased:
>
>>> Dir['*.tif']
> => ["1_lower.tif", "1_upper.TIF", "2_lower.tif", "2_upper.TIF"]
>
> Running Roberts code gives me this:
>
> 1_lower.tif   1_lower.pdf
> I got you
> 1_upper.TIF   1_upper.TIF.pdf
> 2_lower.tif   2_lower.pdf
> I got you
> 2_upper.TIF   2_upper.TIF.pdf
>
> Very interesting.

Yes! Thank you all! I did a File.rename to downcase and now it works
fine. Sorry, I'm a Windows guy. Case has never meant much to me.

-Peter
This topic is locked and can not be replied to.