Navigate through folders

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

···

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

Andrei Caragea 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(p) { .. 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"

···

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

"Andrei Caragea" <dracoola4u2001@yahoo.com> wrote in message
news:9e4d72ecab1db1084d4e364fb1de521c@ruby-forum.com...

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
--
Posted via http://www.ruby-forum.com/\.

From path2:
Dir::chdir("..") # returns to path1
Dir::chdir("..\..") # returns to path

Hth gfb

GianFranco Bozzetti 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

···

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

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

···

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

p.chomp!

Jesus.

···

On Thu, Apr 29, 2010 at 9:06 AM, Andrei Caragea <dracoola4u2001@yahoo.com> 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"