# Navigate through folders

**URL:** <https://www.ruby-forum.com/t/navigate-through-folders/187733>\
**Category:** Ruby\
**Created:** [April 28, 2010, 3:08pm UTC](https://www.ruby-forum.com/t/navigate-through-folders/187733 "2010-04-28T15:08:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brunik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@brunik](https://www.ruby-forum.com/u/brunik)\
**Post date:** [April 28, 2010, 3:08pm UTC](https://www.ruby-forum.com/t/navigate-through-folders/187733/1 "2010-04-28T15:08:56Z")

</div>

Hello everyone,

ok… I have file (file.txt) which is written by a script. In this given  
file there’s a path (c:/path/path1/path2) and I have the following code  
in another script:

File.open(‘file.txt’).each\_line do |p|

Dir::chdir("#{p}")  
{  
bunch of code  
}

What I want to do after this is to go up to /path1 or /path and run some  
more code; and this is where I’m stuck.

Can anyone point me in the right direction?

Thank you,  
Andrei

---

<div class="post-metadata">

**Author:** ![brunik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@brunik](https://www.ruby-forum.com/u/brunik)\
**Post date:** [April 28, 2010, 3:59pm UTC](https://www.ruby-forum.com/t/navigate-through-folders/187733/2 "2010-04-28T15:59:32Z")

</div>

Andrei C. wrote:

> File.open(‘file.txt’).each\_line do |p|
> 
> Dir::chdir("#{p}")  
> {  
> bunch of code  
> }
> 
> What I want to do after this is to go up to /path1 or /path and run some  
> more code; and this is where I’m stuck.
> 
> Can anyone point me in the right direction?

Note that p contains a newline at the end, which I’d expect would give  
you an Errno::ENOENT.

But if the chdir is successful then you can walk up a level using “…”.  
Note that inside the block form of chdir this gives a warning.

> > Dir.pwd  
> > =\> “/home/candlerb”
> 
> > Dir.chdir("/etc/fonts") { Dir.chdir("…"); puts Dir.pwd }  
> > (irb):8: warning: conflicting chdir during another chdir block  
> > /etc  
> > =\> nil

Otherwise you could try:

Dir.chdir§ { … some code … }  
Dir.chdir("#{p}/…") { … some more code … }

or take a look at the Pathname library

> > require ‘pathname’  
> > =\> true
> 
> > p = Pathname.new("/etc/fonts")  
> > =\> #Pathname:/etc/fonts
> 
> > p.parent.to\_s  
> > =\> “/etc”

---

<div class="post-metadata">

**Author:** ![brunik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@brunik](https://www.ruby-forum.com/u/brunik)\
**Post date:** [April 28, 2010, 4:26pm UTC](https://www.ruby-forum.com/t/navigate-through-folders/187733/3 "2010-04-28T16:26:15Z")

</div>

“Andrei C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> bunch of code  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

From path2:  
Dir::chdir(“…”) # returns to path1  
Dir::chdir(“…..”) # returns to path

Hth gfb

---

<div class="post-metadata">

**Author:** ![brunik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@brunik](https://www.ruby-forum.com/u/brunik)\
**Post date:** [April 28, 2010, 4:30pm UTC](https://www.ruby-forum.com/t/navigate-through-folders/187733/4 "2010-04-28T16:30:02Z")

</div>

GianFranco B. wrote:

> Dir::chdir("…") # returns to path

I’m afraid this won’t:

irb(main):020:0\> “…”  
=\> “…”

You need “…/…” (preferred, even under Windows), or “…\…”, or  
‘…’

irb(main):021:0\> “…\…”  
=\> “…\…”  
irb(main):022:0\> “…\…”.size  
=\> 5

---

<div class="post-metadata">

**Author:** ![brunik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@brunik](https://www.ruby-forum.com/u/brunik)\
**Post date:** [April 29, 2010, 9:06am UTC](https://www.ruby-forum.com/t/navigate-through-folders/187733/5 "2010-04-29T09:06:51Z")

</div>

Thank you very much guys, this works great.

@Brian  
You were right about the error, though for me it returned  
(Errno::EINVAL) which I fixed with:

p.delete! “\n”

Best Regards,  
Andrei

---

<div class="post-metadata">

**Author:** ![brunik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@brunik](https://www.ruby-forum.com/u/brunik)\
**Post date:** [April 29, 2010, 6:31pm UTC](https://www.ruby-forum.com/t/navigate-through-folders/187733/6 "2010-04-29T18:31:05Z")

</div>

On Thu, Apr 29, 2010 at 9:06 AM, Andrei C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you very much guys, this works great.
> 
> @Brian  
> You were right about the error, though for me it returned  
> (Errno::EINVAL) which I fixed with:
> 
> p.delete! “\n”

p.chomp!

Jesus.
