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

How to auto-find my file location?

Started by LeDark Lua, 27 July 2015 - 09:44 AM
LeDark Lua #1
Posted 27 July 2015 - 11:44 AM
For example my file is in the: /Dada/Yup/FileContent/main.lua
And i want to find my file always, cuz this file will be changing locations :)/>

Thanks in advance.
flaghacker #2
Posted 27 July 2015 - 12:24 PM

shell.getRunningProgram ()
for the file path, and

fs.getDir (shell.getRunningProgram ())
for the directory.
LewisTehMinerz #3
Posted 27 July 2015 - 12:26 PM
I think fs.find is your answer.

(This code should work)


local file = textutils.serialize( fs.find( "main.lua" ) )
if #file == 0 then
  error( "Cannot find file...", 0 )
elseif #file ==  1 then
  print( "main.lua is located at: " .. file[ 1 ] )
elseif #file > 1 then
  print( "main.lua is at multiple locations." )
  print( "It could be at " .. file[ 1 ] )
  for i = 2, #file do
    print( "Or at " .. file[ i ] )
  end
end
MKlegoman357 #4
Posted 27 July 2015 - 12:48 PM
I think fs.find is your answer.

(This code should work)


local file = textutils.serialize( fs.find( "main.lua" ) )
if #file == 0 then
  error( "Cannot find file...", 0 )
elseif #file ==  1 then
  print( "main.lua is located at: " .. file[ 1 ] )
elseif #file > 1 then
  print( "main.lua is at multiple locations." )
  print( "It could be at " .. file[ 1 ] )
  for i = 2, #file do
	print( "Or at " .. file[ i ] )
  end
end

fs.find doesn't look for a file in the whole filesystem. It simply allows to find files based on a given path. A part of the path can be a wildcard.
LeDark Lua #5
Posted 27 July 2015 - 08:32 PM
Thanks guys, ill test it tomorow.