What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [?] Enumerating through files in Win32

[?] Enumerating through files in Win32
Author: Message:
SmokingCookie
Senior Member
****

Avatar

Posts: 815
Reputation: 15
30 / Male / Flag
Joined: Jul 2007
O.P. [?] Enumerating through files in Win32
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.
10-27-2011 07:54 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
[?] Enumerating through files in Win32 - by SmokingCookie on 10-27-2011 at 07:54 PM
RE: [?] Enumerating through files in Win32 - by matty on 10-27-2011 at 09:31 PM
RE: [?] Enumerating through files in Win32 - by SmokingCookie on 10-29-2011 at 09:37 AM
RE: [?] Enumerating through files in Win32 - by matty on 10-31-2011 at 11:10 AM
RE: [?] Enumerating through files in Win32 - by SmokingCookie on 10-31-2011 at 02:45 PM
RE: [?] Enumerating through files in Win32 - by matty on 10-31-2011 at 02:53 PM
RE: [?] Enumerating through files in Win32 - by SmokingCookie on 10-31-2011 at 03:55 PM
RE: [?] Enumerating through files in Win32 - by matty on 10-31-2011 at 05:36 PM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On