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

How quickly can you fix this file searching script?

Started by Tag365, 30 March 2015 - 11:14 PM
Tag365 #1
Posted 31 March 2015 - 01:14 AM
Post your fixed code here and make sure that it actually prints searched files!
– advanced search function script
local args = {}
local searchstring = args[1] – the string we want to search.
local matchedfilecontents = {}

– Searches for the searchstring in each file.
local function filesearch(path)
local filelist = fs.list(path) – we need a table with all the files at the path of the directory.
for _, file in ipairs(filelist) do – enter a loop to search the filenames for the given search string.
if fs.exists(path.."/"..file) then
if not fs.isDir(path.."/"..file) then – we don't want to error if this is not a valid file.
local handle = fs.open(path.."/"..file, "r")
if string.find(handle.readAll(), searchstring) then – if we see a match then add the file to the search directory.
matchedfilecontents[#matchedfilecontents + 1] = path.."/"..file
end
else
filesearch(path.."/"..file)
end
end
end –End the loop
end

print("Found "..#matchedfilecontents.." matches for "..searchstring..".")
for k, v in ipairs(matchedfilecontents) do
print(v)
end
– End of script source.
The race starts in 3… 2…. 1….. Go! The clock is ticking…
GopherAtl #2
Posted 31 March 2015 - 02:36 AM
there's a game thread for "find the bug in this code" problems. Please don't try to do clickbait titles in AaP.
KingofGamesYami #3
Posted 31 March 2015 - 03:15 AM

local tDirs = {"/"}
local matched = {}
local tArgs = {...}
while #tDirs > 0 do
  for k, v in pairs( fs.list( table.remove( tDirs, 1 ) ) do
    local fullPath = fs.combine( dir, v )
    if fs.isDir( fullPath ) then
      tDirs[ #tDirs + 1 ] = fullPath
    else
      local file = fs.open( fullPath, "r" )
      if file.readAll():find( tArgs[ 1 ] ) then
        matched[ #matched + 1 ] = fullPath
      end
      file.close()
    end
  end
end
for k, v in pairs( matched ) do
  print( v )
  sleep(0.1)
end

Does this count? :P/>
Edited on 31 March 2015 - 01:16 AM
Tag365 #4
Posted 04 April 2015 - 12:53 AM
If it works in the real game, then yes.