Chdir on Windows - beginner doubt

M

Marcelo S.

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 Pine'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.
 
L

Luis Lavena


Hello Marcelo,
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 Pine'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: "

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!
 
G

Gordon Thiesfeld

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 Pine'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?

Luis beat me to it. ;-)

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"]
 
L

Luis Lavena

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

Marcelo S.

Luis Lavena 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.
 
M

Marcelo S.

Gordon Thiesfeld 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.
 
B

botp

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
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top