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

fs.isDir not working? Need Help!

Started by Exerro, 30 August 2012 - 10:53 PM
Exerro #1
Posted 31 August 2012 - 12:53 AM
im making yet another major update for my os which is a complete rewrite and im including a very good filebrowser in it…but i cant get very far with it because fs.isDir doesnt seem to be working.
i have this code:

for i = 1,explorerItemCount do
  if fs.isDir(listOfFiles[i]) then
   writePos((string.len(listOfFiles[i]))+1,4+i,"  - Directory")
  else
   writePos((string.len(listOfFiles[i]))+1,4+i,"  - File")
  end
end
(writePos is a function that is like this function writePos(x,y,text) term.setCursorPos(x,y) term.write(text) end)
when i just start the filebrowser it works fine but if i go into a directory it doesnt work
what it does to get files/dirs is:

function getFiles()
listOfFiles = fs.list(dirViewing)
explorerItemCount = #listOfFiles
end

please help because this is an extremely important part of my os (or will be at least)
KaoS #2
Posted 31 August 2012 - 08:22 AM
try fs.isDir(shell.resolve(filename))
Luanub #3
Posted 31 August 2012 - 08:53 AM
You just need to add in the parent directory into the fs.isDir(). Here's and example..


currentDir = shell.dir()
for i = 1,explorerItemCount do
  if fs.isDir(currentDir.."/"..listOfFiles[i]) then

You can also try


if fs.isDir(shell.resolveProgram(listOfFiles[i]) then

I'm not sure how well it works I've never used it. It might be more robust when dealing with multiple folders in the path.
Edited on 31 August 2012 - 06:56 AM
Exerro #4
Posted 31 August 2012 - 12:08 PM
oh yeh i cant beleive i missed the parent code :l i feel like such a noob…what is the shall.resolve thing and would it make it possible to get out of just the computer folder?
one more thing is - is there a command like dirViewing.parent or anything that would return the directory that dirViewing is in?
KaoS #5
Posted 31 August 2012 - 12:27 PM
shell.resolve('filename') returns the full path to the specified file or folder so you can use it in most fs and io functions, to go one directory up use fs.setDir(shell.resolve('..'))

EDIT: .. is understood in almost any file system to mean one path up, you need the second because the '' is a special char in Lua, try print('') and you will only get
Edited on 31 August 2012 - 10:29 AM
Exerro #6
Posted 31 August 2012 - 12:36 PM
i added this to my code:

elseif key == 13 then
dirViewing = shell.resolve("..")
and it said bluetide:385: Invalid path
KaoS #7
Posted 31 August 2012 - 12:43 PM
I just tried it and it worked fine….. please post the entire code
Exerro #8
Posted 31 August 2012 - 12:49 PM
Spoiler

--this is the BlueTide OS 2.0.0 program
--copyright of BlueTide Programs.inc, Benedict Allen, all members of BlueTide Programs.inc and all members of programcraft.inc
--if you wish to use this program please contact Benedict Allen(awsumben13 on cc forums) and make sure you give the credit to BlueTide Programs
--for tips and info visit the BlueTide Website at www.bluetideprograms.webs.com or for more programs www.bluetideprograms.weebly.com/download.html
--usability functions
local function clear()
term.clear()
term.setCursorPos(1,1)
end
local function clearPos(x,y)
term.clear()
term.setCursorPos(x,y)
end
local function writePos(x,y,text)
term.setCursorPos(x,y)
term.write(text)
end
function clearPrint(x,y,text)
term.clear()
term.setCursorPos(x,y)
term.write(text)
end
function clearLine(y)
writePos(1,y,"												  ")
end
function reset(file, x, y)
if fs.exists(file) then
  h = fs.open(file, "w")
  h.write("")
  h.close()
  term.setCursorPos(x,y)
  term.write("File reset")
  sleep(2)
else
  term.setCursorPos(x,y)
  term.write("file not found")
  sleep(2)
end
end
function tip(help)
while true do
  clear()
  print(help)
  writePos(1,18,"press enter or backspace to exit")
  event, key = os.pullEvent()
  if key == 28 or key == 14 then
   break
  end
end
end
function drawBox(x,y,z,n)
writePos(x,y,"+")
term.setCursorPos(x+1,y)
for i = 1,(z-x)-1 do
  write("-")
end
writePos(z,y,"+")
term.setCursorPos(x+1,n)
for i = 1,(z-x)-1 do
  write("-")
end
for i = 1,(n-y)-1 do
  writePos(x,y+i,"|")
end
for i = 1,(n-y)-1 do
  writePos(z,y+i,"|")
end
writePos(x,n,"+")
writePos(z,n,"+")
end
--end
--programs functions
function drawFieldOne()
writePos(1,1,"Welcome: Benedict				 BlueTideOS 1.4.0")
writePos(1,2,"==================================================")
writePos(1,17,"==================================================")
writePos(1,18,"  Programs   Notifications   Shortcuts   Explorer ")
if fieldSelected == 1 then
  if f1Selected == 1 then
   writePos(2,18,"[")
   writePos(11,18,"]")
  elseif f1Selected == 2 then
   writePos(13,18,"[")
   writePos(27,18,"]")
  elseif f1Selected == 3 then
   writePos(29,18,"[")
   writePos(39,18,"]")
  elseif f1Selected == 4 then
   writePos(41,18,"[")
   writePos(50,18,"]")
  end
end
end
function runFieldOne()
clear()
drawFieldTwo()
drawFieldOne()
if programRunning ~= "desktop" then
  writePos(20,10,"[not selected]")
end
event, key, p2, p3, p4, p5 = os.pullEvent()
if key == 203 and f1Selected > 1 then
  f1Selected = f1Selected-1
elseif key == 205 and f1Selected < 4 then
  f1Selected = f1Selected+1
elseif key == 14 then
  exit()
elseif key == 15 then
  fieldSelected = 2
elseif key == 42 then
  event, key, p2, p3, p4, p5 = os.pullEvent()
  if key == 32 then
   programRunning = "desktop"
   drawFieldTwo()
  end
elseif key == 28 and f1Selected == 1 then
  programApp()
elseif key == 28 and f1Selected == 2 then
  notificationShower()
elseif key == 28 and f1Selected == 3 then
  shortcutApp()
elseif key == 28 and f1Selected == 4 then
  programRunning = "explorer"
  fieldSelected = 2
elseif key == 29 or key == 157 then
  tip("BlueTide OS v-"..version.."nn  Controls:nn   Switch field/tab = tabn   Exit = backspacen   Move cursor = arrow keysn   Get controls = ctrln   Select Option = enternn  Shortcut keys:nn   ctrl = helpn   tab = switch tabsn   control panel = right shiftn   desktop = left shift-d")
end
end
function drawFieldTwo()
if programRunning == "explorer" then
  drawBrowserScreen()
elseif programRunning == "desktop" then
  drawDesktop()
end
end
function runFieldTwo()
clear()
drawFieldOne()
drawFieldTwo()
if programRunning == "explorer" then
  fileBrowser()
elseif programRunning == "desktop" then
  fieldSelected = 1
end
if key == 15 then
  fieldSelected = 1
end
end
function drawBrowserScreen()
if dirViewing == "/" then
  dirDisplayed = "Computer"
else
  dirDisplayed = dirViewing
  k = string.len(dirDisplayed)
  if k > 25 then
   dirDisplayed = string.sub(dirDisplayed, k-25,k)
  end
end
writePos(1,3,"BlueTide Explorer - "..dirDisplayed)
writePos(1,4,"--------------------------------------------------")
getFiles()
if explorerItemCount > 12 then
  newItemCount = 12
else
  newItemCount = explorerItemCount
end
for i = 1,newItemCount do
  if fs.isDir(dirDisplayed.."/"..listOfFiles[i]) then
   writePos((string.len(listOfFiles[i]))+1,4+i,"  - Directory")
  else
   writePos((string.len(listOfFiles[i]))+1,4+i,"  - File")
  end
end
if explorerItemCount < 13 then
  for i = 1,explorerItemCount do
   writePos(2,4+i,listOfFiles[i])
  end
else
  for i = 1,12 do
   writePos(2,4+i,listOfFiles[i])
  end
  writePos(2,14,"More...")
end
if fieldSelected == 2 then
  if browserSelected < 13 then
   writePos(1,4+browserSelected,">")
  end
end
end
function getFiles()
listOfFiles = fs.list(dirViewing)
explorerItemCount = #listOfFiles
end
function fileBrowser()
clear()
drawFieldOne()
drawFieldTwo()
event, key, p2, p3, p4, p5 = os.pullEvent()
if key == 208 and browserSelected < explorerItemCount and browserSelected < 12 then
  browserSelected = browserSelected+1
elseif key == 200 and browserSelected > 1 then
  browserSelected = browserSelected-1
elseif key == 15 then
  fieldSelected = 1
elseif key == 14 then
  exit()
elseif key == 42 then
  event, key, p2, p3, p4, p5 = os.pullEvent()
  if key == 32 then
   programRunning = "desktop"
   drawFieldTwo()
  end
elseif key == 13 and dirViewing ~= "bluetide" then
  dirViewing = shell.resolve("..")
elseif key == 28 then
  if fs.isDir(dirViewing.."/"..listOfFiles[browserSelected]) then
   table.insert(fileOrder, "/") table.insert(fileOrder, listOfFiles[browserSelected])
   dirViewing = dirViewing.."/"..listOfFiles[browserSelected]
  else
  
  end
end
end
--end
--variables
browserSelected = 1
listOfFiles = {}
explorerItemCount = #listOfFiles
explorerItems = {"";}
dirViewing = "bluetide"
notificationAn1Time = 0.001
programSelected = 1
f1Selected = 1
loopBreaker = false
fieldSelected = 1
blueTidePrograms = {"BlueMail","BlueWord","BlueTide AntiVirus","BlueVille","BlueNewGame","Tutorial","BlueTunes"}
OS_shortcuts = {}
characters = {}
programRunning = "desktop"
fileOrder = {""}
userLoggedIn = "DefaultUser"
version = "2.0.0"
shortcutPath = "bluetide/users/"..userLoggedIn.."/shortcuts"
shortcuts = fs.list(shortcutPath)
shortcutSelected = 1
notificationAn2Time = 0.3
notificationCount = 0
possibleNotifications = {"New email", "Disk Inserted", "New peripheral found", "Redstone signal change", "Rednet GPS ping"}
notifications = {"Email from 1", "Disk Inserted", "New peripheral found", "Redstone signal change", "Rednet GPS ping"}
notificationSelected = 1
--end
--code
while loopBreaker ~= true do
if fieldSelected == 1 then
  runFieldOne()
elseif fieldSelected == 2 then
  runFieldTwo()
end
end
clear()
--end
i removed all of the unneccesarry functions and stuff and the reason it has such a wierd setup is so it can draw and run any of the functions and always have the taskbar present and so when in the taskbar it can draw the program without running it