Seems like Win32's not doing things it's said to be. Anyway, I've worked along the lines of
this MSDN article, but somehow I can't get more than 2 valid file names, plus one "dummy" result; that dummy allows one to check if Interop.GetLastError() returns 18 (ERROR_NO_MORE_FILES) or something else is wrong. Indeed, Interop.GetLastError() does return ERROR_NO_MORE_FILES at the dummy, but I'm still getting only 2 useful results. Code:
JScript code:
/* FileSystem object and Print() functions defined elsewhere
furthermore: Paths.Root = MsgPlus.ScriptFilesPath + "\\";*/
FileSystem.EnumerateFiles = function(Directory) {
var ret = new Array();
var result = this.FindFirstFile(Directory);
if(result.hFind === 0) {
result.WIN32_FIND_DATA.Size = 0;
return ret;
}
ret.push(result);
do {
result = this.FindNextFile(result.hFind);
ret.push(result);
} while(result.hFind !== 0);
return ret;
}
FileSystem.FindClose = function(hFind) {
return Interop.Call("Kernel32.dll","FindClose",hFind);
}
FileSystem.FindNextFile = function(hFind,pData) {
ret = new Object();
ret.WIN32_FIND_DATA = typeof pData === "object" ? pData : Allocate(592); // confirmed working
ret.hFind = Interop.Call("Kernel32.dll","FindNextFileW",hFind,ret.WIN32_FIND_DATA.DataPtr);
ret.LastError = Interop.GetLastError();
return ret;
}
FileSystem.FindFirstFile = function(sFile) {
if(typeof sFile === "number") sFile = this.GetPathFromHandle(sFile); // confirmed working
var ret = new Object();
ret.WIN32_FIND_DATA = Allocate(592); // confirmed working
ret.hFind = Interop.Call("Kernel32.dll","FindFirstFileW",sFile,ret.WIN32_FIND_DATA.DataPtr);
ret.LastError = Interop.GetLastError();
return ret;
}
var FindResult = FileSystem.EnumerateFiles(Paths.Root + "*");
for(var i = 0; i < FindResult.length; i++) {
Print(FindResult[i].hFind);
Print(FindResult[i].LastError);
Print(FindResult[i].WIN32_FIND_DATA.readDWORD(0));
Print(FindResult[i].WIN32_FIND_DATA.readSTRING(44));
FindResult[i].WIN32_FIND_DATA.Size = 0;
FileSystem.FindClose(FindResult[i].hFind);
}
The output is always:
code:
> 322211400 // some handle
> 0 // Interop.GetLastError() result => nothing's wrong
> 16 // dwFileAttributes member from the WIN32_FIND_DATA structure => this result is the current directory
> . // cFileName[MAX_PATH] member from the same struct
> 1 // always the same handle
> 0 // Interop.GetLastError() result => still okay
> 16 // Again a directory
> .. //cFileName[MAX_PATH] => parent directory
> 0 // dummy result with return value 0
> 18 // ERROR_NO_MORE_FILES
> 0 // Attributes
> // NULL
Also, calling the function for, say "Paths.Root + *.js" does something similar: it gives the first 2 .js files and then returns the same dummy. Any advice to get this working is welcome.