Chdir on Windows - beginner doubt

Folks,

Hi, this is my first post here. Tried to search the forum for a specific
answer, but didn’t find it. I’m not a programmer, just a curious. Got
Chris P.'s “Learning to Program”, and I’m going through all the
exercises. I’m in the YAML chapter now, and I’m trying to adapt the
picture-renaming exercise.

Somehow, though, my computer doesn’t do the task. I’m not sure if the
problem is in Chdir or in another instruction (or even in the addresses
I gave).

I use Windows Vista. These are the first two lines, where I use Dir:

Dir.chdir (‘D:\Fotos’)
pic_names=Dir[‘C:\Users\Marcelo\Pictures\*.{JPG.jpg}’]

The double inverted slashes were put after I read something about this
being the way Ruby gets the syntax in Windows.

It says "Downloading 0 files: "

And nothing else happens.

What did I do wrong?

Best,

Marcelo.

On Sun, Dec 12, 2010 at 1:39 PM, Marcelo S. [email protected]
wrote:

I gave).

And nothing else happens.

What did I do wrong?

Luis beat me to it. :wink:

Also, I wouldn’t recommend using the {JPG,jpg} . You may get
unexpected results. I seem to remember case being a problem in older
versions, but more modern versions handle it better.

irb(main):002:0> Dir[“C:/Users/gthiesfeld/pictures/.{JPG,jpg}"]
=> [“C:/Users/gthiesfeld/pictures/shadow.jpg”,
“C:/Users/gthiesfeld/pictures/shadow.jpg”]
irb(main):003:0> Dir["C:\Users\gthiesfeld\pictures\
.{JPG,jpg}”]
=> []
irb(main):004:0> Dir[“C:\Users\gthiesfeld\pictures\.jpg"]
=> []
irb(main):005:0> Dir["C:/Users/gthiesfeld/pictures/
.jpg”]
=> [“C:/Users/gthiesfeld/pictures/shadow.jpg”]
irb(main):006:0> Dir[“C:/Users/gthiesfeld/pictures/*.JPG”]
=> [“C:/Users/gthiesfeld/pictures/shadow.jpg”]

irb(main):016:0> Dir[“C:/Users/gthiesfeld/pictures/*.{png,jpg}”]
=> [“C:/Users/gthiesfeld/pictures/Capture.PNG”,
“C:/Users/gthiesfeld/pictures/Capture1.PNG”,
“C:/Users/gthiesfeld/pictures/shadow.jpg”]

Luis L. [email protected] wrote:

What did I do wrong?

Nothing you did wrong, but Dir globing is expecting forward slashes
instead of backslashes.

I’m a relative newcomer here, and that surprises me greatly. The
Windows
APIs handle either direction, and I know of no other language with this
limitation. Has there been a debate about this, or is this just a
simple
religious issue?

On Dec 12, 10:39pm, Tim R. [email protected] wrote:

I’m a relative newcomer here, and that surprises me greatly. The Windows
APIs handle either direction, and I know of no other language with this
limitation. Has there been a debate about this, or is this just a simple
religious issue?

It might be the ‘root’ of the language and that mainly the core of
Ruby developers do not use Windows.

The few Windows users we got used to forward slashes and Windows API
recognize both, so we just ignore these differences and accept them
compensated with the other Ruby benefits.

On Dec 12, 4:39pm, “Marcelo S.” [email protected] wrote:

Folks,

Hello Marcelo,

I use Windows Vista. These are the first two lines, where I use Dir:

Dir.chdir (‘D:\Fotos’)
pic_names=Dir[‘C:\Users\Marcelo\Pictures\*.{JPG.jpg}’]

The double inverted slashes were put after I read something about this
being the way Ruby gets the syntax in Windows.

It says "Downloading 0 files: "

Download 0 files is part of the script, correct?

And nothing else happens.

Perhaps the script is expecting a list of files and got none.

What did I do wrong?

Nothing you did wrong, but Dir globing is expecting forward slashes
instead of backslashes.

Try, in a IRB prompt:

irb(main):001:0> Dir[“C:\Users\Luis\Pictures\Avatars\*.png”]
=> []

Got zero results, now with forward slashes:

irb(main):003:0> Dir[“C:/Users/Luis/Pictures/Avatars/*.png”]
=> [“C:/Users/Luis/Pictures/Avatars/IMG_0231.png”, “C:/Users/Luis/
Pictures/Avatars/IMG_0231_CROP (Custom).png”, “C:/Users/Luis/Pictures/
Avatars/IMG_0231_CROP.png”, “C:/Users/Luis/Pictures/Avatars/Logo
Multimedia systems.png”]

Got some results.

If the path you entered was from the command line or from a input,
then you can try do File.expand_path to transform the double
backslashes to forward slashes.

Hope that helps and welcome to Ruby!

Gordon T. wrote in post #967961:

Also, I wouldn’t recommend using the {JPG,jpg} . You may get
unexpected results. I seem to remember case being a problem in older
versions, but more modern versions handle it better.

Did work well indeed with the newest version. The book is a few years
old. But still it works well in IRB but not in the program.

UPDATE: This was the problem. I replaced that with simple jpg and it
worked like a charm.

Thanks!!!

Marcelo.

On Mon, Dec 13, 2010 at 11:45 AM, Marcelo S. [email protected]
wrote:

pic_names=Dir[‘G:/DCIM/100HP960/*.{JPG.jpg}’]

hmm you have, JPG.jpg in there

maybe you meant a comma there instead of a dot.

btw, windows is not case sensitve, so either *.JPG or *.jpg should
work.

best regards -botp

botp wrote in post #968010:

On Mon, Dec 13, 2010 at 11:45 AM, Marcelo S. [email protected]
wrote:

pic_names=Dir[‘G:/DCIM/100HP960/*.{JPG.jpg}’]

hmm you have, JPG.jpg in there

Yep, found that out after I copied the program here.

Gotta get myself better glasses.

Thanks!

Luis L. wrote in post #967959:

Hey, Luis. Thanks for the welcome!

But it didn’t work; I’ve used single forward slashes, double forward
slashes, single backslashes and double backslashes. Nothing happened. On
IRB prompt the Dir did work, indeed.

Maybe it helps to say I’m using ruby 1.9.2p0 and editing the program in
SciTE?

Download 0 files is part of the script, correct?

Yes. Comes from this command:

print "Downloading #{pic_names.length} files: "

This is the full program:

Dir.chdir (‘D:/Fotos’)
pic_names=Dir[‘G:/DCIM/100HP960/*.{JPG.jpg}’]
puts ‘What would you like to call this batch?’
batch_name=gets.chomp
puts
print "Downloading #{pic_names.length} files: "
pic_number=1
pic_names.each do |name|
print ‘.’
new_name=if pic_number<10
“#{batch_name}0#{pic_number}.jpg”
else
“#{batch_name}#{pic_number}.jpg”
end
File.rename name, new_name
pic_number=pic_number+1
end
puts
puts ‘Done!’

Thanks once again!

Best,

Marcelo.