# Why aren't filenames being lowercased?

**URL:** <https://www.ruby-forum.com/t/why-arent-filenames-being-lowercased/110959>\
**Category:** Ruby\
**Created:** [August 1, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/why-arent-filenames-being-lowercased/110959 "2007-08-01T14:25:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 1, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/why-arent-filenames-being-lowercased/110959/1 "2007-08-01T14:25:13Z")

</div>

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  
…

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 1, 2007, 2:49pm UTC](https://www.ruby-forum.com/t/why-arent-filenames-being-lowercased/110959/2 "2007-08-01T14:49:23Z")

</div>

> 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}”  
> end

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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 1, 2007, 2:55pm UTC](https://www.ruby-forum.com/t/why-arent-filenames-being-lowercased/110959/3 "2007-08-01T14:55:05Z")

</div>

Florian G. wrote:

> > 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}”  
> > end
> 
> 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.

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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 1, 2007, 3:02pm UTC](https://www.ruby-forum.com/t/why-arent-filenames-being-lowercased/110959/4 "2007-08-01T15:02:25Z")

</div>

See inline comments  
On Aug 1, 8:25 am, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
…

> 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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 1, 2007, 3:36pm UTC](https://www.ruby-forum.com/t/why-arent-filenames-being-lowercased/110959/5 "2007-08-01T15:36:31Z")

</div>

On 8/1/07, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > File.rename(tiffile, tiffile.downcase)
> 
> 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…

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 1, 2007, 3:51pm UTC](https://www.ruby-forum.com/t/why-arent-filenames-being-lowercased/110959/6 "2007-08-01T15:51:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 1, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/why-arent-filenames-being-lowercased/110959/7 "2007-08-01T15:12:44Z")

</div>

ChrisH wrote:

> See inline comments  
> On Aug 1, 8:25 am, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> …
> 
> > 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

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?
