local ffi = require 'ffi';
-- declarations for accessing winapi
ffi.cdef[[
typedef void* HANDLE;
typedef char CHAR;
typedef const char* LPCSTR;
typedef unsigned long DWORD;
typedef const wchar_t* WCHAR;
typedef unsigned short WORD;
typedef int BOOL;
typedef struct _FILETIME {
DWORD dwLowDateTime;
DWORD dwHighDateTime;
} FILETIME, *PFILETIME, *LPFILETIME;
typedef struct _WIN32_FIND_DATAA {
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwReserved0;
DWORD dwReserved1;
CHAR cFileName[255];
CHAR cAlternateFileName[14];
DWORD dwFileType; // Obsolete. Do not use.
DWORD dwCreatorType; // Obsolete. Do not use
WORD wFinderFlags; // Obsolete. Do not use
} WIN32_FIND_DATAA, *PWIN32_FIND_DATAA, *LPWIN32_FIND_DATAA;
HANDLE FindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData);
BOOL FindNextFileA(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData);
BOOL FindClose(HANDLE hFindFile);
]];
-- usage
local INVALID_HANDLE_VALUE = ffi.cast('void*', -1);
local lookup_files = function (path_pattern)
local file_list = { };
local find_data_struct = ffi.cast('LPWIN32_FIND_DATAA', ffi.new('WIN32_FIND_DATAA[1]'));
local hFindFile = ffi['C'].FindFirstFileA(path_pattern, find_data_struct); -- support patterns
if hFindFile ~= INVALID_HANDLE_VALUE then
repeat
file_list[#file_list+1] = ffi.string(find_data_struct.cFileName);
until ffi['C'].FindNextFileA(hFindFile, find_data_struct) == 0;
ffi['C'].FindClose(hFindFile);
end
return file_list;
end
for i, filename in ipairs(lookup_files('*.exe')) do
print(filename);
end