0

I've a module inside an environment I use that, when I choose a file from a File Dialog, store the path inside a variable (let say var path).

I need than to get the list of all files within path and store it into an array.

So what I've tried is:

list = Dir[path + "*"]

the fact is that path is somethings like this:

D:\Google Drive\Samples\Black Octopus Sound\Drums - Kicks\

and when I read it, the array is empty. It's due to the fact that the slash should be / instead of \. So I've do this:

path = path.gsub('\\','\/')

but the result is:

D:\/Google Drive\/Samples\/Black Octopus Sound\/Drums - Kicks\/

looks like path stored in the variable is:

D:\\Google Drive\\Samples\\Black Octopus Sound\\Drums - Kicks\\

is that normal? Because If I just print the path, it looks correct:

D:\Google Drive\Samples\Black Octopus Sound\Drums - Kicks\

is this the intended bahaviour of escaping path in Ruby? Am I wrong? How can I best manage this situation?

And why can't I write:

path = "D:\Google Drive\Samples\Black Octopus Sound\Drums - Kicks\"

but only:

path = "D:\Google Drive\Samples\Black Octopus Sound\Drums - Kicks\ " // notice the last empty space in the end

it says unterminated string meets end of file

markzzz
  • 47,390
  • 120
  • 299
  • 507
  • Well, it says "unterminated string" because the ``\`` character is used for escaping, thus `\"` is how you write a double quote inside a double-quoted string. So the right way to write it would be `path = "D:\\Google Drive\\Samples\\Black Octopus Sound\\Drums - Kicks\\"`, which prints as ``D:\Google Drive\Samples\Black Octopus Sound\Drums - Kicks\``. – Toribio Jan 11 '16 at 21:35
  • Because a backslash means something in an interpolated Ruby string. – Dave Newton Jan 11 '16 at 21:36
  • 1
    Ruby doesn't need backslashes in filenames. It understands what the OS needs and treats forward slashes appropriately. The [IO documentation](http://ruby-doc.org/core-2.3.0/IO.html#method-c-open) says: `Ruby will convert pathnames between different operating system conventions if possible. For instance, on a Windows system the filename "/gumby/ruby/test.rb" will be opened as "\gumby\ruby\test.rb".` Use forward-slashes and make your life easier. – the Tin Man Jan 11 '16 at 23:14
  • 1
    I'd recommend reading "[Backslashes in single quoted strings vs. double quoted strings](http://stackoverflow.com/q/648156/128421)" – the Tin Man Jan 11 '16 at 23:29

2 Answers2

1
\   means =>> Escapes the next metacharacter. 

Meaning you cannot use backslash on windows as part of a glob, example.

 Dir["D:\foo*"] will not work, use Dir["D:/foo*"] instead.`
z atef
  • 7,138
  • 3
  • 55
  • 50
0

Try this:

path = 'D:\Google Drive\Samples\Black Octopus Sound\Drums - Kicks\'

Difference in quotes. '' will automaticaly escape backslashes and "" not.

Also you wrote:

It's due to the fact that the slash should be / instead of \

Windows uses backslash in paths but as @theTinMan noticed, ruby will convert pathnames between different operating system conventions if possible.

Quote from doc

When specifying a Windows-style filename in a Ruby string, remember to escape the backslashes

Also pretty simple example you can find here.

axvm
  • 1,876
  • 2
  • 11
  • 19
  • 1
    `C:/Users/Admin` *IS* correct. See http://ruby-doc.org/core-2.3.0/IO.html#method-c-open, which specifically says: `Ruby will convert pathnames between different operating system conventions if possible. For instance, on a Windows system the filename "/gumby/ruby/test.rb" will be opened as "\gumby\ruby\test.rb". ` – the Tin Man Jan 11 '16 at 23:15
  • @theTinMan Actualy I said that about windows itself not just about ruby. But you are right, I'll edit my answer. – axvm Jan 11 '16 at 23:26
  • @theTinMan: no! If I use `list = Dir[path + "*"]` on path with `\`, the list is empty. I mean: if I have `path = "D:\\Google Drive\\Samples\\Black Octopus Sound\\Leviathan\\Drums - Kicks\\"` and I do `list = Dir[path + "*"]`, it returns `[]` – markzzz Jan 12 '16 at 09:03
  • Instead, this works: `list = Dir["D:/Google Drive/Samples/Black Octopus Sound/Leviathan/Drums - Kicks/*"]` and returns the correct list – markzzz Jan 12 '16 at 09:08