This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Zudo's profile picture

Going up?

Started by Zudo, 17 November 2012 - 10:44 PM
Zudo #1
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
Lyqyd #2
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.
Zudo #3
Posted 17 November 2012 - 11:59 PM
No, I just mean going up from an internal directory e.g: rom/programs to rom/
remiX #4
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
Zudo #5
Posted 18 November 2012 - 12:53 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

I dont get that though
I need to set a variable's value to the previous directory
remiX #6
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
Tiin57 #7
Posted 18 November 2012 - 01:05 AM
Wouldn't

shell.setDir(x)
work?
Zudo #8
Posted 18 November 2012 - 01:10 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

Attempt to call nil on:
w = string.split(dir, "/")

Wouldn't

shell.setDir(x)
work?
I need to go up one directory
Viproz #9
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( ".." ))
remiX #10
Posted 18 November 2012 - 04:52 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

Attempt to call nil on:
w = string.split(dir, "/")

It should… do you have 'dir' defined?
Lyqyd #11
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, "(.*)/[^/]+$")
Kingdaro #12
Posted 18 November 2012 - 10:52 AM
Pretty sure "&amp;#46;&amp;#46;/" is the current directory, but up. So doing

cd &amp;#46;&amp;#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('&amp;#46;&amp;#46;/')

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