Why aren't filenames being lowercased?

P

Peter Bailey

This is a simple script that I'm just using for something bigger in the
near future, kind of a test. I'm looking in a directory of TIFF files,
lower-casing the filenames, and then listing them with PDF named
counterparts. The script works fine until about 3/4 of the way down the
list and, all of a sudden, it displays the TIFF files in uppercase. If I
go into the directory itself, those "upper-case" names are actually
lower-case as filenames. Why is it doing this?
Thanks,
Peter

Dir.chdir("L:/tiff/cdtiff/00000")
Dir.glob("*.tif").each do |tiffile|
File.rename(tiffile, tiffile.downcase)
pdffile = File.basename(tiffile, ".tif") + ".pdf"
puts "#{tiffile} #{pdffile}"
end

gives me:
...
va997.tif va997.pdf
va998.tif va998.pdf
WI679.TIF WI679.TIF.pdf
WI680.TIF WI680.TIF.pdf
WI681.TIF WI681.TIF.pdf
...
 
F

Florian Gross

Dir.chdir("L:/tiff/cdtiff/00000")
Dir.glob("*.tif").each do |tiffile|
new_name = tiffile.downcase
File.rename(tiffile, new_name)
pdffile = File.basename(new_name, ".tif") + ".pdf"
puts "#{new_name} #{pdffile}"

Basically, you were still using the old (outdated) name.
gives me:
...
va997.tif va997.pdf
va998.tif va998.pdf
WI679.TIF WI679.TIF.pdf
WI680.TIF WI680.TIF.pdf
WI681.TIF WI681.TIF.pdf

I think your va* files were already in lowercase when the script was
running.
 
P

Peter Bailey

Florian said:
new_name = tiffile.downcase
File.rename(tiffile, new_name)
pdffile = File.basename(new_name, ".tif") + ".pdf"
puts "#{new_name} #{pdffile}"

Basically, you were still using the old (outdated) name.


I think your va* files were already in lowercase when the script was
running.

Well, all of the filenames should be lower-cased, because of my
"File.rename" in my script. And, like I said, all the files that show up
upper-case are really lower-case as files.
 
C

ChrisH

See inline comments
Dir.chdir("L:/tiff/cdtiff/00000")
Dir.glob("*.tif").each do |tiffile|
From the docs look like it creates an array of matched filke names
then
iterates, placing current filename in tiffile...
File.rename(tiffile, tiffile.downcase)
Rename the file to be lowercase, value in tiffile(and in the array
created by Dir.glob) is unchanged...
pdffile = File.basename(tiffile, ".tif") + ".pdf"
I think base name is case sensitive, so ".tif" does not match '.TIF'
so .pdf is appended instead
of replacing '.TIF'
puts "#{tiffile} #{pdffile}"
end

When you go look at the directory the files are in lowercase because
your script has made them so.
Have you tried re-running on the dir of lower-case names?

Cheers
Chris
 
P

Peter Bailey

ChrisH said:
See inline comments

then
iterates, placing current filename in tiffile...

Rename the file to be lowercase, value in tiffile(and in the array
created by Dir.glob) is unchanged...

I think base name is case sensitive, so ".tif" does not match '.TIF'
so .pdf is appended instead
of replacing '.TIF'

When you go look at the directory the files are in lowercase because
your script has made them so.
Have you tried re-running on the dir of lower-case names?

Cheers
Chris

I don't understand. Yes, I've made them lower-case, and then, I'm just
listing them. I'm a Windows guy and this whole case business is new to
me, but, I'm learning to have to deal with it. That's why I lower-cased
all the files to start with. So, if they're lowercase, why would they
display as upper-case?
 
J

Jano Svitok

I don't understand. Yes, I've made them lower-case, and then, I'm just
listing them. I'm a Windows guy and this whole case business is new to
me, but, I'm learning to have to deal with it. That's why I lower-cased
all the files to start with. So, if they're lowercase, why would they
display as upper-case?

1. You have to work with the new filename. I.e. after you rename the
file, set the new file name as well, otherwise you'll be working with
the old name.

File.rename(tiffile, tiffile.downcase)
tiffile = tiffile.downcase # <<< THIS IS IMPORTANT!!!

pdffile = ...

- OR -

newtifile = tiffile.downcase
if newtiffile != tiffile
File.rename(tiffile, newtiffile)
tiffile = newtiffile
end

pdffile = ... tiffile...

2. Sometimes windows shows uppercase filenames in lowercase -- (I'm
guessing that)
when on FAT && the filename is in 8.3 format (=has no lfn) -- you can
check with full dir
 
P

Peter Bailey

2. Sometimes windows shows uppercase filenames in lowercase -- (I'm
guessing that)
when on FAT && the filename is in 8.3 format (=has no lfn) -- you can
check with full dir

Thanks. I guess I thought that the File.rename was enough, but,
obviously, it wasn't. Kind of disconcerting to me. But, I got it. I took
one of your hints, above, and did a full lower-casing in a separate
loop, instead of in the same loop. So, now, it's working. Thank you to
all!
 

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

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top