Announcement

Collapse
No announcement yet.

HyperFast FileFind function xD

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • HyperFast FileFind function xD

    Hi, I made this function a few days ago but I don't know why I didn't posted it. It's my version of the FileFind function (without a dot lol). I made it as an alternative to the AMS File.Find function because it was blocking the application while doing the search.

    I'm using luafilesystem (it's already fast) and lua lanes modules to make it really fast (finds and prints more than 1400 files in less than 3 seconds with only 256mb of ram lol) because it will be running in a new thread without blocking the application. So here is the function:

    --Globals
    PHP Code:
    require"lanes";
    function 
    fFileFind(sPathtSearchbSubFoldersbCaseSensitivetToAdd)
        require
    "lfs";--Yeapit's inside of the lane
        function FileFindA(sPath, tSearch, bSubFolders, bCaseSensitive, tToAdd)
            local tS = (type(tSearch=="table")) and tSearch or {""};
            local bC = bCaseSensitive or false;
            if #tS > 0 then
                for x, y in pairs(tS) do
                    local a = (bC==true) and y or string.gsub(y, "(.)", function(s) return string.lower(s) end);
                    a = string.gsub(a, "(.)*(.)", function(s, sa) return "%b"..s..sa end);
                    a = string.gsub(a, "*(..)", function(s) return "%b"..s end);
                    a = string.gsub(a, "(..)(.)%.", function (s, ss) return (s=="%b") and s..ss.."." or s..ss.."%." end)
                    a = string.gsub(a, "^%.", "%%.")
                    tS[x] = string.gsub(a, "?", ".");
                end
            end
            return FileFindB(sPath, tS, bSubFolders, bC, tToAdd);
        end
        function FileFindB(sPath, tSearch, bSubFolders, bCaseSensitive, tToAdd)
            local tReturn = (type(tToAdd)=="table") and tToAdd or {};
            local tDirs = {};
            if lfs.chdir(sPath) then
                for x in lfs.dir(sPath) do
                    if x ~= "." and x ~= ".." then
                        if lfs.attributes(x, "mode") == "file" then
                            for y, z in pairs(tSearch) do
                                local a = (bCaseSensitive==false) and x or string.gsub(x, "(.)", function(s) return string.lower(s) end);
                                if string.find(a, z) then
                                    table.insert(tReturn, #tReturn+1, sPath.."\\"..x);
                                end
                            end
                        elseif lfs.attributes(x, "mode") == "directory" then
                            if bSubFolders then
                                table.insert(tDirs, #tDirs+1, sPath.."\\"..x);
                            end
                        end
                    end
                end
                if #tDirs > 0 and bSubFolders then
                    for x, y in pairs(tDirs) do
                        FileFindB(y, tSearch, true, bCaseSensitive, tReturn);
                    end
                end
                return (#tReturn>0) and tReturn or nil;
            else
                return nil;
            end
        end
        
        return FileFindA(sPath, tSearch, bSubFolders, bCaseSensitive, tToAdd)
    end
    local FileFind = lanes.gen("*", fFileFind) 

    --Example call in "On click" event of an object
    PHP Code:
    tFiles FileFind("C:\\Windows", {".dat""*.dll""s*.exe"}, truefalsenil); 

    -On click in other button, label, etc. or timer
    PHP Code:
    Debug.ShowWindow(true);
    if 
    tFiles.status == "done" then
        
    if tFiles[1then
            local s 
    "";
            
    Debug.Print("Files: "..#tFiles[1].."\r\n");
            
    for eachfile in pairs(tFiles[1]) do
                
    s..each.." - "..file.."\r\n";
            
    end
            Debug
    .Print(s.."\r\n");
        else
            
    Dialog.Message("""NoFiles");
        
    end    
    elseif tFiles.status == "error" then
        local a
    tFiles:join()
        
    Debug.Print(tostring(e));
    else
        
    Debug.Print("Status: "..tFiles.status.."\r\n");
    end 

    I hope you like and comments are accepted

    PD: For those who have installed lua for windows in their computers can use this code right after the example call and it will show the results in less than 1.7seconds:

    PHP Code:
    while tFiles.status == "running" or "pending" do
        if 
    tFiles.status == "done" then
            io
    .stderr:write("done\n")
            break;
        elseif 
    tFiles.status == "pending" then
            io
    .stderr:write("pending\n")
        else
            
    io.stderr:write(tFiles.status.."\n")
        
    end
    end
    if tFiles.status == "done" then
        
    if tFiles[1then
            
    print("Files: "..#tFiles[1].." in "..os.clock()-t1);
            
    local s "";
            for 
    eachfile in pairs(tFiles[1]) do
                
    s=s..each..":"..file.."\n";
            
    end
            
    print(s)
        else
            print(
    "NoFiles");
        
    end
    elseif tFiles.status == "error" then
        local a
    tFiles:join()
        print(
    tostring(e));
    else
        print(
    "Status: "..tFiles.status.."\r");
    end 

  • #2
    I'm loving these Lanes examples with AutoPlay, multi-threading is one of those things I've always wanted in AMS.

    Thanks for the example and keep up the great contributions!

    Comment


    • #3
      I love speed, especially when it comes to CPUs! Thanks for this (great) work

      EDIT
      In the last code container, there is a thing I didn't understand well, maybe I'm just a bit too tired... anyhow:
      Code:
      [COLOR="red"]while tFiles.status == "running" or "pending" do
          if tFiles.status == "done" then[/COLOR]
              io.stderr:write("done\n")
              break;
          elseif tFiles.status == "pending" then
              io.stderr:write("pending\n")
          else
              io.stderr:write(tFiles.status.."\n")
          end
      end
      The red part is the one I couldn't understand because, if I'm right, when while is true, the if will always be false and will be executed only the elseif/else code; when while is false there'll be nothing executed from this code... Am I right or did I miss something ?
      Last edited by T3STY; 03-21-2011, 06:34 PM.

      Comment


      • #4
        @Sakuya, thank you for your comment. I will try to post more lanes examples

        @T3STY, thank you for your comment. I was writing too fast that I forgot to write that line as it should be. I've to be monitoring the lane status cause you never know when does it will end. And if I don't write an "if status==x elseif status==y end" inside of the loop then it becomes an infinite loop. It's different from usual loops (where you do some operations that could change the flow of the loop) . It will run until the lane change the status of that global variable. So, the flow of the while loop will be controlled by the lane

        PHP Code:
        local t1=os.clock();--I forgot to add this line too before the example call hehe
        tFiles 
        FileFind("C:\\Windows", {".dat""*.dll""*.exe"}, truefalse);
        while 
        tFiles.status == "running" or tFiles.status == "pending" do
            if 
        tFiles.status == "done" then
                io
        .stderr:write("done\n")
                break;
            
        end
        end 
        BTW, i've tested this function again and it found 1665 files in 2.663secs . I really like this speed in slow computers. I think i will try to do my own explorer to use it instead of the windows explorer . This function, without a lua lane, was taking about 27secs (it was faster even than the ams file.find function since then lol )
        Last edited by webultra; 03-21-2011, 07:07 PM.

        Comment


        • #5
          Yeah, a really unusual loop, though, I think I got it.

          Comment


          • #6
            Can you upload this lua lanes modules somewhere?

            Comment


            • #7
              Yeap, I attached the lanes and luafilesystem modules.
              Attached Files

              Comment


              • #8
                seems to be nice piece of code but can you please post an apz example as i can't get it work

                Thanks a lot in advance ,,.

                Comment


                • #9
                  I'll soon...I'm a little busy now. BTW, I recommend you to put both dlls and the lanes.lua file at your project's root folder. Maybe that's why you can't get it work.

                  Comment


                  • #10
                    Hairy chest in code:

                    (.)*(.)




                    Sorry
                    - BoKu -

                    Comment


                    • #11
                      well , thanks for the reply i'll give it a try when i come back home ,,
                      thanks again ,,.

                      Comment


                      • #12
                        @webultra, any chance of a folder find version?

                        Comment


                        • #13
                          Ah, you have to read the luafilesystem help file. You will see that some attributes are not retrieved when the found item is a folder.

                          Comment


                          • #14
                            Originally posted by webultra View Post
                            Ah, you have to read the luafilesystem help file. You will see that some attributes are not retrieved when the found item is a folder.
                            Oh, cant lfs still find folders with lanes faster than the standard folderfind?

                            Also I had to change your on click from;

                            tFiles = FileFind

                            to;

                            tFiles = fFileFind

                            to make it work, was that a typo or am I running the enhanced file find outside lanes?

                            Comment


                            • #15
                              (wish I could edit)

                              I have to leave out this part also;

                              local FileFind = lanes.gen("*", fFileFind)
                              FileFind = easyLanes.NewLane("MySearchLane", fFileFind) <-- from the other example

                              or it returns a "index global value nill" error

                              Comment

                              Working...
                              X