File renaming question

P

Peter Bailey

Hi,
I just want to do a simple case-changing operation, if needed. The file
renaming scheme below works for me, but, if any files in my directory
are already lowercase, then, the script fails. How can I accommodate
files that may or may not be lowercase to begin with and just make them
lowercase, whatever they originally were. It seems to me that the "!" is
what's demanding that my original files here be upper-case, but, I can't
seem to get the thing to work without that "!."

Thanks,
Peter


require 'FileUtils'

Dir.chdir("C:/bitmaps/notif")

Dir.glob("*.pdf").each do |file|
File.rename("#{file}", "#{file.downcase!}")
FileUtils.mv(file, "c:/bitmaps/notif/scratch")
end
...
 
S

Stefano Crocco

Alle mercoled=C3=AC 11 aprile 2007, Peter Bailey ha scritto:
Hi,
I just want to do a simple case-changing operation, if needed. The file
renaming scheme below works for me, but, if any files in my directory
are already lowercase, then, the script fails. How can I accommodate
files that may or may not be lowercase to begin with and just make them
lowercase, whatever they originally were. It seems to me that the "!" is
what's demanding that my original files here be upper-case, but, I can't
seem to get the thing to work without that "!."

Thanks,
Peter


require 'FileUtils'

Dir.chdir("C:/bitmaps/notif")

Dir.glob("*.pdf").each do |file|
File.rename("#{file}", "#{file.downcase!}")
FileUtils.mv(file, "c:/bitmaps/notif/scratch")
end
...

You're right, what is making your program fail is the !. The problem is tha=
t=20
String#downcase! returns nil when the string is already downcase, so the ca=
ll=20
to rename becomes:

=46ile.rename("#{file}", "")

(nil.to_s returns an empty string), and obviously fails. So here, you need =
to=20
use downcase, which always returns the string with all characters downcased=
=2E=20
You don't specify what doesn't work when you use downcase instead of=20
downcase!, but looking at your code, I think that it fails on the call to m=
v.=20
If I'm right, the reason is that downcase doesn't alter its receiver (in th=
is=20
case, file), so, when you pass file to FileUtils.mv, it still contains the=
=20
upcase string. But, at that point, the file with the upcase name doesn't=20
exist anymore and the method fails. Using downcase!, instead, file is=20
modified (downcased), so the first parameter to mv is the downcase string,=
=20
which corresponds to an existing file. To solve your problem, you can=20
substitute downcase! with downcase in the call to rename and file with=20
file.downcase in the call to mv.

I hope this helps

Stefano
 
P

Peter Bailey

Stefano Crocco wrote:

To solve your problem, you can
substitute downcase! with downcase in the call to rename and file with
file.downcase in the call to mv.

I hope this helps

Stefano

Brilliant. Yes, that did it. I got rid of the screamer, !, and I put in
file.downcase with my move command. Thank you very much!
 

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,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top