Ravin
Ravin

Reputation: 21

Trying to loop through Directory to list all the files using lua

I am new to lua to trying to understand and put pieces to together and looking out for some help. I have gone through the existing articles on lua file looping but unable to get the desired output.

Question - I have a folder with files, Folder path - "D:\Test_Files\Outbound\Client\final" Files in the folder with extension - .txt

Trying to :

  1. Get the count of files in the folder(in this case "final" folder).
  2. Read every file, building a loop something similar to this:

    list = {}
    for i=0,(#Totalfilecount) do 
    local fr = io.open('D:\Test_Files\Outbound\Client\final\'..filename.,'rb') 
    local f = fr.read('*.txt')
    
    Customfunction(f) -- Passing file content to customfunction to apply business logic.  
    end
    

Questions :

  1. How to get file count from a directory?
  2. How to read the directory to check if the files with "*.txt" exist?
  3. How to use table list to store each file name and read through the loop?
  4. How to read each file via loop and pass the value to function "Customfunction(f)"?

Code is expected to run on windows. Please share suggestions in pure lua without using external file system functions such as 'lfs' as we do not like to import external functions.

Any Suggestions/help will be greatly appreciated!

Upvotes: 2

Views: 9000

Answers (1)

user6889435
user6889435

Reputation:

You can't (at least shouldn't) do this without extensions to Lua. To accomplish this, you have to download LuaFileSystem library. You can do it using LuaRocks:

$ luarocks install luafilesystem

Use library as such:

require "lfs"

function dirtree(dir)
    assert(dir and dir ~= "", "Please pass directory parameter")
    if string.sub(dir, -1) == "/" then
        dir=string.sub(dir, 1, -2)
    end

    local function yieldtree(dir)
        for entry in lfs.dir(dir) do
            if entry ~= "." and entry ~= ".." then
                entry=dir.."/"..entry
                local attr=lfs.attributes(entry)
                coroutine.yield(entry,attr)
                if attr.mode == "directory" then
                    yieldtree(entry)
                end
            end
        end
    end

    return coroutine.wrap(function() yieldtree(dir) end)
end

An example use of code above:

for filename, attr in dirtree("D:\Test_Files\Outbound\Client\final") do
    print(attr.mode, filename)
end 

You have to check does extension equal to txt. To read file extension use this snippet:

function GetFileExtension(path)
    return path:match("^.+(%..+)$")
end

So, to answer your question(s), you can get amount of files in directory just by counting elements in array returned in dirtree. To answer second question, just use code from the post. Table that you want is returned by dirtree(), but you may want to extract only .txt files from it. To read a file, just check other SO answers. You've got given name (in array), so use it.

EDIT: You can parse result of dir and ls command to get directory listing, but you shouldnt. Althrough this way you wouldn't need to install any libraries, your code is going to be heavily OS-depedent.

Adding libraries to your code isn't so bad. Hacking things is worse.

(Not sure file extension extracting function is going to work. I didn't make dirtree code used in this post, it belongs to David Kastrup)

Upvotes: 3

Related Questions