Dir.glob problem

D

David Vlad

In the program Im making I need to read some wma files into a variable
and I did like this:

songs = Dir.glob("C:\\Users\\Public\\Music\\Sample Music\\*.wma")

Note that this is NOT the wrong directory, I've copy pasted it from the
map itself and also the files are wma and nothing else. My programming
teacher has no idea why this dosnt work and the other ppl I've talked
with dosnt know either. Most said something like "aside from a simple
mistake like wrong directory/extension, I don't know what could be going
wrong." So now this forum is my last resort, why on earth dosnt this
work?!
 
E

Eric Christopherson

In the program Im making I need to read some wma files into a variable
and I did like this:

songs = Dir.glob("C:\\Users\\Public\\Music\\Sample Music\\*.wma")

Note that this is NOT the wrong directory, I've copy pasted it from the
map itself and also the files are wma and nothing else. My programming
teacher has no idea why this dosnt work and the other ppl I've talked
with dosnt know either. Most said something like "aside from a simple
mistake like wrong directory/extension, I don't know what could be going
wrong." So now this forum is my last resort, why on earth dosnt this
work?!

What goes wrong with it?
 
M

Michael W Ryder

David said:
In the program Im making I need to read some wma files into a variable
and I did like this:

songs = Dir.glob("C:\\Users\\Public\\Music\\Sample Music\\*.wma")

Note that this is NOT the wrong directory, I've copy pasted it from the
map itself and also the files are wma and nothing else. My programming
teacher has no idea why this dosnt work and the other ppl I've talked
with dosnt know either. Most said something like "aside from a simple
mistake like wrong directory/extension, I don't know what could be going
wrong." So now this forum is my last resort, why on earth dosnt this
work?!

The only way I could get it to work is to use the example at
http://ruby-doc.org/core/classes/Dir.html and create a directory
variable such as: lfiles=Files.join("C:", "Users", "Public", "Music",
"Sample Music", "*.wma") and then use a=Dir.glob(lfiles).
The only problem with that approach is that the entire path is added to
each file. I don't know if this is a concern or not.
 
M

Michael W Ryder

Michael said:
The only way I could get it to work is to use the example at
http://ruby-doc.org/core/classes/Dir.html and create a directory
variable such as: lfiles=Files.join("C:", "Users", "Public", "Music",
"Sample Music", "*.wma") and then use a=Dir.glob(lfiles).
The only problem with that approach is that the entire path is added to
each file. I don't know if this is a concern or not.

Further experimenting found two other solutions. The first is to use:
a=Dir.glob("C:/Users/Public/Music/Sample Music/*.wma") which has the
same "problem" as my first solution.
A better solution may be to use Dir.chdir to change to the directory and
then use Dir.glob to extract the file names. This has the advantage of
not including the path in front of each file name.
 
M

Michael W Ryder

Michael said:
The only way I could get it to work is to use the example at
http://ruby-doc.org/core/classes/Dir.html and create a directory
variable such as: lfiles=Files.join("C:", "Users", "Public", "Music",
"Sample Music", "*.wma") and then use a=Dir.glob(lfiles).
The only problem with that approach is that the entire path is added to
each file. I don't know if this is a concern or not.

Further experimenting found two other solutions. The first is to use:
a=Dir.glob("C:/Users/Public/Music/Sample Music/*.wma") which has the
same "problem" as my first solution.
A better solution may be to use Dir.chdir to change to the directory and
then use Dir.glob to extract the file names. This has the advantage of
not including the path in front of each file name.
 
J

Joel VanderWerf

Michael said:
Further experimenting found two other solutions. The first is to use:
a=Dir.glob("C:/Users/Public/Music/Sample Music/*.wma") which has the
same "problem" as my first solution.
A better solution may be to use Dir.chdir to change to the directory and
then use Dir.glob to extract the file names. This has the advantage of
not including the path in front of each file name.

chdir is not thread safe, it changes the pwd which is global to the
process. Here's an alternative:

d = Dir.new("/tmp") # use fwd slashes even on windows
d.grep(/\.wma$/)

This should give you an array of filenames in the dir.
 
M

Michael W Ryder

Joel said:
chdir is not thread safe, it changes the pwd which is global to the
process. Here's an alternative:

d = Dir.new("/tmp") # use fwd slashes even on windows
d.grep(/\.wma$/)

This should give you an array of filenames in the dir.

Is there a way to expand this to include subdirectories like .glob
allows? .grep doesn't appear to work on the string created by File.join.
 
R

Robert Klemme

Further experimenting found two other solutions. The first is to use:
a=Dir.glob("C:/Users/Public/Music/Sample Music/*.wma") which has the
same "problem" as my first solution.
A better solution may be to use Dir.chdir to change to the directory and
then use Dir.glob to extract the file names. This has the advantage of
not including the path in front of each file name.

You can use Dir.entries for that:

Dir.new("C:/Users/Public/Music/Sample Music").entries.grep /\.wma\z/i

Kind regards

robert
 
R

Robert Klemme

Is there a way to expand this to include subdirectories like .glob
allows? .grep doesn't appear to work on the string created by File.join.

If you want to do globbing, include subdirectories and want to have
relative paths. I would look at Pathname. You can, for example,
generate relative paths via #relative_path_from:

irb(main):025:0>
Pathname("/var/log/foo").relative_path_from(Pathname("/var"))
=> #<Pathname:log/foo>
irb(main):026:0>
Pathname("/var/log/foo").relative_path_from(Pathname("/var")).to_s
=> "log/foo"

Pathname has also a method #find which yields Pathname instances to the
block and can be used for things like this.

Kind regards

robert
 
M

Michael W Ryder

David said:
In the program Im making I need to read some wma files into a variable
and I did like this:

songs = Dir.glob("C:\\Users\\Public\\Music\\Sample Music\\*.wma")

Note that this is NOT the wrong directory, I've copy pasted it from the
map itself and also the files are wma and nothing else. My programming
teacher has no idea why this dosnt work and the other ppl I've talked
with dosnt know either. Most said something like "aside from a simple
mistake like wrong directory/extension, I don't know what could be going
wrong." So now this forum is my last resort, why on earth dosnt this
work?!

Looking at the source code for Dir it looks like it should accept the
above directory separators if the macro DOSISH is defined. Where is
this defined? It looks like it is part of the build process so I would
assume it would be defined if building for a Windows platform.
 

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
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top