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
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Going up?
Started by Zudo, 17 November 2012 - 10:44 PMPosted 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.
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.
Posted 17 November 2012 - 11:59 PM
No, I just mean going up from an internal directory e.g: rom/programs to rom/
Posted 18 November 2012 - 12:37 AM
Try something like this.
EDIT:
This actually works better
Output:
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
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 bettert = {} 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
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
Posted 18 November 2012 - 01:05 AM
Wouldn't
shell.setDir(x)
work?Posted 18 November 2012 - 01:10 AM
Ok then just usedir = "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, "/")
I need to go up one directoryWouldn'twork?shell.setDir(x)
Posted 18 November 2012 - 01:20 AM
In the os we just do "cd .." So just need to see what does cd !
So this should work :
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( ".." ))
Posted 18 November 2012 - 04:52 AM
Ok then just usedir = "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?
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, "(.*)/[^/]+$")
Posted 18 November 2012 - 10:52 AM
Pretty sure "&#46;&#46;/" is the current directory, but up. So doing
EDIT: forums what the hell have you done with my periods.
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.