Search code examples
fileluafilenames

Open file without Filename with lua skript


Good evening,

I am currently working on a programm that takes information from a file into a Database, for testing purposes I used to open Testfiles in the classical way via IO:

function reader (file, delimeter)
    local f = io.open(file)
    for line in f:lines() do
        lines[count] = splitty(line, delimeter)
        count = count + 1;
    end
end

(this part also containes the first part of a splitter)

But in the actual environment, the database programm imediatly moves the file in another directory with a name change to, for example this:

$30$15$2016$09$26$13$27$24$444Z$.Pal.INV.csv

Now I know the directory but I can't really predict the name, so I wanted to know if there might be a way to open files without knowing their name. (and delete them after reading them)

I had ideas to use a modified link:

local inputFile = "D:\\Directory\\(*all)"

but it failed.

Other aviable information: The system is until now only planned on Windows PCs. The directory will always only contain the one file that is to ready, no other files.


Solution

  • -- directory name and file name should consist of ASCII-7-bit characters only
    local dir = [[C:\Temp\New Folder]]
    local file = io.popen('dir /b/s/a-d "'..dir..'" 2>nul:'):read"*a":match"%C+"
    if not file then
       error"No files in this directory"
    end
    -- print the file name of first file in the directory
    print(file)  --> C:\Temp\New Folder\New Text Document.txt