Jump to content




How to auto-find my file location?


  • You cannot reply to this topic
4 replies to this topic

#1 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 27 July 2015 - 10: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.

#2 flaghacker

  • Members
  • 655 posts

Posted 27 July 2015 - 11:24 AM

shell.getRunningProgram ()
for the file path, and
fs.getDir (shell.getRunningProgram ())
for the directory.

#3 LewisTehMinerz

  • Members
  • 174 posts
  • LocationMinecraft in Minecraft in Minecraft in ComputerCraft... in Minecraft

Posted 27 July 2015 - 11:26 AM

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


#4 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 27 July 2015 - 11:48 AM

 LewisTehMinerz, on 27 July 2015 - 11:26 AM, said:

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.

#5 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 27 July 2015 - 07:32 PM

Thanks guys, ill test it tomorow.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users