Jump to content




Going up?


  • You cannot reply to this topic
11 replies to this topic

#1 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 17 November 2012 - 11:44 PM

Is there a way of going "up" a directory in the filesystem? I tried doing ".." but it didn't work.

currentdir = "/"
while true do
 print(currentdir)
 print("")
 list = fs.list(currentdir)
 for i = 1, #list do
  print(i .. ": " .. list[i])
 end
 a, b = os.pullEvent("char")
 if b == "0" then
  currentdir = ".." -- Here is the problem
 else
  if fs.isDir(list[tonumber(:)/>/>]) then
   currentdir = list[tonumber(:D/>/>]
  else
   shell.run(list[tonumber(:D/>/>])
  end
 end
end


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 17 November 2012 - 11:58 PM

Do you mean above the root of the computer's file system? You can't get out of the computer's sandbox.

#3 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 17 November 2012 - 11:59 PM

No, I just mean going up from an internal directory e.g: rom/programs to rom/

#4 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 18 November 2012 - 12:37 AM

Try something like this.

s = "rom/programs"
print(s)
for k, v in string.gmatch(s, "(%w+)/(%w+)") do
    z = k
end

print(s) -- Output: "rom/programs"
print(z) -- Output: "rom"

EDIT:

This actually works better

t = {}
dir = "rom/programs"
for token in string.gmatch(dir, "(%w+)") do
    table.insert(t, token)
end

for i = 1, #t do
    print(t[i])
end

Output:
rom
programs


#5 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 18 November 2012 - 12:53 AM

 sIdEkIcK_, on 18 November 2012 - 12:37 AM, said:

Try something like this.

s = "rom/programs"
print(s)
for k, v in string.gmatch(s, "(%w+)/(%w+)") do
	z = k
end

print(s) -- Output: "rom/programs"
print(z) -- Output: "rom"

EDIT:

This actually works better

t = {}
dir = "rom/programs"
for token in string.gmatch(dir, "(%w+)") do
	table.insert(t, token)
end

for i = 1, #t do
	print(t[i])
end

Output:
rom
programs

I dont get that though
I need to set a variable's value to the previous directory

#6 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 18 November 2012 - 12:56 AM

Ok then just use
dir = "rom/programs"
w = string.split(dir, "/") -- The second argument is the pattern that the dir has, which is /
-- this function returns as a table
for i = 1, #w do
    print(w[i])
end

Output:
rom
programs


#7 Tiin57

    Java Lunatic

  • Members
  • 1,412 posts
  • LocationIndiana, United States

Posted 18 November 2012 - 01:05 AM

Wouldn't
shell.setDir(x)
work?

#8 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 18 November 2012 - 01:10 AM

 sIdEkIcK_, on 18 November 2012 - 12:56 AM, said:

Ok then just use
dir = "rom/programs"
w = string.split(dir, "/") -- The second argument is the pattern that the dir has, which is /
-- this function returns as a table
for i = 1, #w do
	print(w[i])
end

Output:
rom
programs

Attempt to call nil on:

 sIdEkIcK_, on 18 November 2012 - 12:56 AM, said:

w = string.split(dir, "/")

 tiin57, on 18 November 2012 - 01:05 AM, said:

Wouldn't
shell.setDir(x)
work?
I need to go up one directory

#9 Viproz

  • Members
  • 58 posts

Posted 18 November 2012 - 01:20 AM

In the os we just do "cd .." So just need to see what does cd !

local tArgs = { ... }
if #tArgs < 1 then
print( "Usage: cd <path>" )
return
end
local sNewDir = shell.resolve( tArgs[1] )
if fs.isDir( sNewDir ) then
shell.setDir( sNewDir )
else
   print( "Not a directory" )
   return
end

So this should work :
shell.setDir(shell.resolve( ".." ))


#10 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 18 November 2012 - 04:52 AM

 Mr CC geek, on 18 November 2012 - 01:10 AM, said:

 sIdEkIcK_, on 18 November 2012 - 12:56 AM, said:

Ok then just use
dir = "rom/programs"
w = string.split(dir, "/") -- The second argument is the pattern that the dir has, which is /
-- this function returns as a table
for i = 1, #w do
	print(w[i])
end

Output:
rom
programs

Attempt to call nil on:

 sIdEkIcK_, on 18 November 2012 - 12:56 AM, said:

w = string.split(dir, "/")

It should... do you have 'dir' defined?

#11 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 18 November 2012 - 10:48 AM

If you've got a string that contains your path, this should get it to be the whole path, without the last folder:

path = string.match(path, "(.*)/[^/]+$")


#12 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 18 November 2012 - 10:52 AM

Pretty sure "&#46;&#46;/" is the current directory, but up. So doing
cd &#46;&#46;/
on the command line, if you're in afolder/anotherfolder/, should send you to afolder/. In a lua script, it'd be
shell.setDir('&#46;&#46;/')

EDIT: forums what the hell have you done with my periods.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users