# Trouble with pathname (i.e slashes) & windows

**URL:** <https://www.ruby-forum.com/t/trouble-with-pathname-i-e-slashes-windows/115633>\
**Category:** Ruby\
**Created:** [September 27, 2007, 1:56am UTC](https://www.ruby-forum.com/t/trouble-with-pathname-i-e-slashes-windows/115633 "2007-09-27T01:56:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 27, 2007, 1:56am UTC](https://www.ruby-forum.com/t/trouble-with-pathname-i-e-slashes-windows/115633/1 "2007-09-27T01:56:08Z")

</div>

Hi,

I am a bit of a ruby newbie, and i cant figure out this problem. I am  
recursively searching through some directories, and when i find the  
one i want, i try to pass it to an external process, through ‘system’  
but the forward slashes screw things up.

i tried replacing the forward slashes through gsub, but i end up with  
2 slashes. for example, if i am trying to ‘dir’ a path, this ends up  
being sent through system:

dir c:\myStartDir/currentDir #obviously this wont work, so i tried  
using gsub, and i get this

dir c:\myStartDir\currentDir #and ‘dir’ complains it cant find the  
directory.

my code looks something like this

dirPath = File.dirname(path).gsub(’/’, “\”)  
system("dir " + dirPath)

path is determined using Find.find(c:\myStartDir)

any help is greatly appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 27, 2007, 2:13am UTC](https://www.ruby-forum.com/t/trouble-with-pathname-i-e-slashes-windows/115633/2 "2007-09-27T02:13:04Z")

</div>

On 9/27/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> path is determined using Find.find(c:\myStartDir)
> 
> any help is greatly appreciated. Thanks!

I use path.gsub(File::SEPARATOR, File::ALT\_SEPARATOR) for the  
conversion.  
But I don’t think that’s your problem.

How did you print those paths? For me Find.find(“c:\ruby”) {|f| puts  
f.gsub(‘/’, “\”)}  
works fine. Can you replace system("dir " + dirPath ) with puts ("dir  
" + dirPath) to see the output? If you use p to print values, it will  
escape special chars, therefore slashes will be doubled. The same in  
irb. Or, replace "dir " with "echo dir " and see the result.

I guess you should test dirPath whether it’s a directory. ()

system("dir " + dirPath) if File.directory? dirPath

or better still,

if File.directory? path  
dirPath = File.dirname(path).gsub(‘/’, “\”)  
system("dir " + dirPath)  
end

Jano

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 27, 2007, 3:17am UTC](https://www.ruby-forum.com/t/trouble-with-pathname-i-e-slashes-windows/115633/3 "2007-09-27T03:17:20Z")

</div>

On 9/26/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I am a bit of a ruby newbie, and i cant figure out this problem. I am  
> recursively searching through some directories, and when i find the  
> one i want, i try to pass it to an external process, through ‘system’  
> but the forward slashes screw things up.

Your best bet is probably to follow these rules:

1. Inside Ruby (meaning, anywhere but between two backtick characters  
or in the ‘system’ method), always use forward-slashes.

2. When calling out to the system, do this:

class String  
def nightmareify  
self.gsub(///, ‘\\’)  
end  
end

irb\> puts “c:/foo/baz”.nightmareify  
c:\foo\baz

Feel free to choose a different method name, depending on your level  
of irritation at this:

> **[Larry Osterman's WebLog](https://learn.microsoft.com/en-us/archive/blogs/larryosterman/)**

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 27, 2007, 11:26pm UTC](https://www.ruby-forum.com/t/trouble-with-pathname-i-e-slashes-windows/115633/4 "2007-09-27T23:26:04Z")

</div>

I figured out my problem…turns out that my path had spaces in the  
name, and i forgot to surround the path with quotations. Kinda dumb,  
but its not one of those things you think of immediately.

thanks!
