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

Return If File Exists In Any Directory

Started by CCJJSax, 31 August 2013 - 12:32 AM
CCJJSax #1
Posted 31 August 2013 - 02:32 AM
I know about fs.exists(string path) But if I'm in the main directory, and I want to see if you can run "dance" for example, it will return false. Is there a way to see if that file exists on the computer?
jay5476 #2
Posted 31 August 2013 - 05:03 AM
this is just from my head

function check(dir, file)
f = fs.list(dir)
for k, v in pairs(f) do
    local path = fs.combine(dir, v)
    if not fs.isReadOnly(path) then
      if fs.isDir(path)
      check(path) -- check the directory
    else
       if path == file then
      return true
      end
    end
  end
end
that should work
CCJJSax #3
Posted 31 August 2013 - 05:44 PM
this is just from my head

function check(dir, file)
f = fs.list(dir)
for k, v in pairs(f) do
	local path = fs.combine(dir, v)
	if not fs.isReadOnly(path) then
	  if fs.isDir(path)
	  check(path) -- check the directory
	else
	   if path == file then
	  return true
	  end
	end
  end
end
that should work

Missing one then I'm sure you mean "if fs.isDir(path) then" I'll post more when I have more info on if it worked or not. I'm kinda swamped with stuff to do at the moment.
Grim Reaper #4
Posted 31 August 2013 - 06:08 PM
this is just from my head

function check(dir, file)
f = fs.list(dir)
for k, v in pairs(f) do
	local path = fs.combine(dir, v)
	if not fs.isReadOnly(path) then
	  if fs.isDir(path)
	  check(path) -- check the directory
	else
	   if path == file then
	  return true
	  end
	end
  end
end

You'll also need to pass 'file' to the every recursive call of 'check'.
Lyqyd #5
Posted 31 August 2013 - 08:24 PM
It sounds kind of like shell.resolveProgram is what you're looking for, unless you really need to find it in any directory, rather than just determining if the string input resolves to a file on the path or in the current directory.