[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

File.basename problems

Peter Bailey

7/25/2007 12:22:00 PM

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
...
--
Posted via http://www.ruby-....

12 Answers

Robert Dober

7/25/2007 12:35:00 PM

0

On 7/25/07, Peter Bailey <pbailey@bna.com> wrote:
> 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
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Martin DeMello

7/25/2007 12:35:00 PM

0

On 7/25/07, Peter Bailey <pbailey@bna.com> wrote:
> 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

Peter Hickman

7/25/2007 1:01:00 PM

0

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]

Peter Bailey

7/26/2007 12:27:00 PM

0


Here I'm using Ruby 1.8.5 on Windows Server 2003.
--
Posted via http://www.ruby-....

Peter Bailey

7/26/2007 12:27:00 PM

0

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.
--
Posted via http://www.ruby-....

Peter Bailey

7/26/2007 12:31:00 PM

0

> 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.
--
Posted via http://www.ruby-....

Robert Dober

7/26/2007 1:13:00 PM

0

On 7/26/07, Peter Bailey <pbailey@bna.com> wrote:
> > 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.
> --
> Posted via http://www.ruby-....
>
>
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
--
[...]as simple as possible, but no simpler.
-- Attributed to Albert Einstein

Peter Bailey

7/26/2007 2:15:00 PM

0


> 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
...
--
Posted via http://www.ruby-....

Robert Dober

7/26/2007 2:57:00 PM

0

On 7/26/07, Peter Bailey <pbailey@bna.com> wrote:
>
> > 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.
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
> ...
> --
> Posted via http://www.ruby-....
>
>
what about case? In your case I think that Glob pulls them in
uppercase and you check for lowercase...

Robert

--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein

Gordon Thiesfeld

7/26/2007 3:15:00 PM

0

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.