File.basename problems

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

On 7/25/07, Peter B. [email protected] 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

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]

On 7/25/07, Peter B. [email protected] 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

Here I’m using Ruby 1.8.5 on Windows Server 2003.

Peter H. 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.

On 7/26/07, Peter B. [email protected] 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 :frowning:

Cheers
Robert

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 :frowning:

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

On 7/26/07, Peter B. [email protected] wrote:

hmm does not seem possible, but try anyway :frowning:

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

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.

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.

Gordon T. 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

Hi,

At Thu, 26 Jul 2007 23:56:46 +0900,
Robert D. 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, “.*”)