Dir.glob problem

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

···

--
Posted via http://www.ruby-forum.com/.

What goes wrong with it?

···

On Fri, Mar 12, 2010 at 5:31 PM, David Vlad <cluny_gisslaren@hotmail.com> wrote:

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

David Vlad wrote:

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 class Dir - RDoc Documentation 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.

David Vlad wrote:

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.

Dir.glob('*.*') gives you all the files in the current directory. You
can put different paths / extensions on as you like.

Documentation and loads of examples here:
http://www.ruby-doc.org/core-1.9.3/Dir.html#method-c-glob

···

--
Posted via http://www.ruby-forum.com/.

Dir.glob("C:/Documents and Settings/prayer/My Documents/userdata/**.*")

···

--
Posted via http://www.ruby-forum.com/.

The asterisks are important: **/*

···

--
Posted via http://www.ruby-forum.com/.

Glob without any wildcards just gives you back what you sent.

irb(main):001:0> Dir.glob 'C:/'
=> ["C:/"]

···

--
Posted via http://www.ruby-forum.com/.

Michael W Ryder wrote:

David Vlad wrote:

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 class Dir - RDoc Documentation 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.

Michael W Ryder wrote:

David Vlad wrote:

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 class Dir - RDoc Documentation 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.

Michael W Ryder wrote in post #896942:

David Vlad wrote:

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.

To avoid "." and "." how to use the Dir::glob() method? Give me an
example.

Please

···

--
Posted via http://www.ruby-forum.com/\.

Joel Pearson wrote in post #1094372:

Dir.glob("C:/Documents and Settings/prayer/My Documents/userdata/**/*")

Hi, there is a confusion with Dir::glob method:

irb(main):001:0>Dir.glob('C:/Documents and Settings/peter/My
Documents/userdata/Test/')
=> ["C:/Documents and Settings/peter/My Documents/userdata/Test/"]

But the 'Test' folder also have another folder like "aaa". why it is not
appearing in the output?

Thanks

···

--
Posted via http://www.ruby-forum.com/\.

Joel Pearson wrote in post #1094511:

The asterisks are important: **/*

Yeah,I am with you. But what the difference is between "glob" without
**/* and "glob" with **/*?

Need to understand the basic.

Thanks

···

--
Posted via http://www.ruby-forum.com/\.

Michael W Ryder wrote:

Michael W Ryder wrote:

David Vlad wrote:

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 class Dir - RDoc Documentation 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.

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.

You can use Dir.entries for that:

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

Kind regards

  robert

···

On 03/13/2010 01:40 AM, Michael W Ryder wrote:

Michael W Ryder wrote:

David Vlad wrote:

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 class Dir - RDoc Documentation 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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

For example : To process only the files in the directory and in its
sub-directories :

Dir.glob("**/*") {|x|
  if File.directory?(x) then # it's a directory
  else # It's a file ...
}

···

Michael W Ryder wrote in post #896942:
> David Vlad wrote:
>> 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.

To avoid "." and "." how to use the Dir::glob() method? Give me an
example.

Please

The asterisks are explained in the documentation,
there are also examples.

BTW, most "basic" questions can be answered by looking
into the docs or by a quick internet search.

Google: "ruby glob"

1. Class: Dir (Ruby 1.9.3)
    [Official Docs]
2. Using Glob with Directories - Ruby - About.com
    [an article that explains Dir.glob]
3. ...
    ...

That's usually much faster than posting a question.

···

Am 31.01.2013 10:00, schrieb Arup Rakshit:

Joel Pearson wrote in post #1094511:

The asterisks are important: **/*

Yeah,I am with you. But what the difference is between "glob" without
**/* and "glob" with **/*?

Need to understand the basic.

--
<https://github.com/stomar/&gt;

Joel VanderWerf wrote:

Michael W Ryder wrote:

Michael W Ryder wrote:

David Vlad wrote:

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 class Dir - RDoc Documentation 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.

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.

Michel wrote in post #1094364:

For example : To process only the files in the directory and in its
sub-directories :

Dir.glob("**/*") {|x|
  if File.directory?(x) then # it's a directory
  else # It's a file ...
}

Yes But if I want to perform such search,on the directory say -
"C:\Documents and Settings\prayer\My Documents\userdata" . Then how to
pass it to the Glob() method?

Thanks

···

--
Posted via http://www.ruby-forum.com/\.

Part of the confusion may be what "glob" is doing. "glob" is a term
inherited from *nix shells, which often have some terse, strange terms
for things. While Dir.glob inherits some of the conventions, it also
expands on them. The basic premise, however, remains the same: return
things that match what I give, substituting where wild cards are
placed.