File.basename problems

P

Peter Bailey

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
...
 
R

Robert Dober

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

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
 
M

Martin DeMello

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

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
 
P

Peter Hickman

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]
 
P

Peter Bailey

Peter said:
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.
 
P

Peter Bailey

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.
 
R

Robert Dober

Both lines yield the same TIFF files. The second one shows the same TIFF
files, too, with the extension still there.
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
 
P

Peter Bailey

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
...
 
R

Robert Dober

Thanks, Robert. I did upgrade to 1.8.6. That is a good thing anyway,
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
...
what about case? In your case I think that Glob pulls them in
uppercase and you check for lowercase...

Robert
 
G

Gordon Thiesfeld

It seems to matter that your extensions are upper-cased:
=> ["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.
 
N

Nobuyoshi Nakada

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, ".*")
 
P

Peter Bailey

Gordon said:
It seems to matter that your extensions are upper-cased:
=> ["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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top