# Dir.chdir does not work on windows (xp)

**URL:** <https://www.ruby-forum.com/t/dir-chdir-does-not-work-on-windows-xp/180642>\
**Category:** Ruby\
**Created:** [December 25, 2009, 2:08pm UTC](https://www.ruby-forum.com/t/dir-chdir-does-not-work-on-windows-xp/180642 "2009-12-25T14:08:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sathvikl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sathvikl](https://www.ruby-forum.com/u/sathvikl)\
**Post date:** [December 25, 2009, 2:08pm UTC](https://www.ruby-forum.com/t/dir-chdir-does-not-work-on-windows-xp/180642/1 "2009-12-25T14:08:54Z")

</div>

Hi all ,

I am using Dir.chdir to change my current working directory.

class Test

def initialize  
spin  
end

def spin  
dire = “/c/Target/tioga-1.11/samples”  
dire2 = “c:\Target\tioga-1.11\samples”  
Dir.chdir(dire2)  
#system(“cd”, dire)  
end

end

My original project inundates with errors  
“The System cannot find the path specified”  
& this test program says no such file or directory.  
Even on MSYS Dir.chdir(dire) does not work !

There are other Dir routines like Dir.each() which gives the error

```
undefined method `each' for nil:NilClass [version: 1.11]

```

Can you please help me correct these errors.

---

<div class="post-metadata">

**Author:** ![sathvikl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sathvikl](https://www.ruby-forum.com/u/sathvikl)\
**Post date:** [December 25, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/dir-chdir-does-not-work-on-windows-xp/180642/2 "2009-12-25T14:46:27Z")

</div>

Hi,

Try using double backslashes: “c:\Target\tioga-1.11\samples”

Cheers,

---

<div class="post-metadata">

**Author:** ![sathvikl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sathvikl](https://www.ruby-forum.com/u/sathvikl)\
**Post date:** [December 25, 2009, 4:37pm UTC](https://www.ruby-forum.com/t/dir-chdir-does-not-work-on-windows-xp/180642/3 "2009-12-25T16:37:15Z")

</div>

Looks like you’re using cygwin paths for a non-cygwin version of Ruby.

Mismatch those is not good, and that would be the reason you can chdir  
properly.

First check your ruby version (ruby -v) if it says “mswin32” or  
“mingw32”, you should use drive letters.

* * *

frmsrcurl:  
[http://compgroups.net/comp.lang.ruby/Dir.chdir-does-not-work-on-windows-xp](http://compgroups.net/comp.lang.ruby/Dir.chdir-does-not-work-on-windows-xp)

---

<div class="post-metadata">

**Author:** ![sathvikl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sathvikl](https://www.ruby-forum.com/u/sathvikl)\
**Post date:** [December 25, 2009, 9:41pm UTC](https://www.ruby-forum.com/t/dir-chdir-does-not-work-on-windows-xp/180642/4 "2009-12-25T21:41:29Z")

</div>

Thank you for your replies.  
Yes the Ruby version says i386-mswin32  
and when I used “c:\Target\tioga-1.11\samples” I could chdir.

I need to execute another program and parse the output. So I used  
IO.popen  
[http://ruby-doc.org/core/classes/IO.html](http://ruby-doc.org/core/classes/IO.html)

```
pdflatex = "pdflatex"
@save_dir = "figures_out"
name = "red"
syscmd = "cd #{@save_dir} & #{pdflatex} -interaction=nonstopmode 

```

#{name}.tex"  
puts syscmd  
IO::popen(syscmd, “r+”)  
puts $?

It does not work. pdflatex complains Invalid Argument. However when I  
replace  
IO::popen(syscmd, “r+”) with system(syscmd) it does work but I cannot  
read the output back.

Can you please tell me why does this happen OR a workaround to achieve  
my goal.

====================================================  
Here is the actual code

```
  @measures = {}
  IO::popen(syscmd, "r+") do |f|
    #puts $?
    f.close_write # We don't need that.
    log = File.open(logname, "w")
    for line in f
      log.print line
      if line =~ /^(.*)\[(\d)\]=(.+pt)/
        n = $1
        num = $2.to_i
        dim = Utils::tex_dimension_to_bp($3)
        @measures[n] ||= []
        @measures[n][num] = dim
      end
    end
  end

```
