PDA

View Full Version : Listing the contents of a directory


PointOfLight
01-07-2007, 03:33 PM
Supposedly there is code for this in the Code Sharing forum already, but I couldn't find it. This is just raw code, so it would need to be cleaned up so you could display it however you want, but here goes:


struct(lpData$, WIN32_FIND_DATA);

handle$ = FindFirstFile("c:\\temp\\*.*", &lpData$);
if(handle$ == INVALID_HANDLE_VALUE)
ShowMessage("No files found");
return(true);
end;

msg$ = "";
result$ = 1;
while(result$ > 0)
msg$ = msg$ + lpData.cFileName$ + "\n";
result$ = FindNextFile(handle$, &lpData$);
end;

FindClose(handle$);

ShowMessage(msg$);

kornalius
01-07-2007, 05:12 PM
Oups, my bad, I was sure I saw that piece of code here at some point.

PointOfLight
01-07-2007, 07:49 PM
Okay, I decided to make this a little more usable. I have put the code into a function called:

FindFiles(path$, spec$, files$)

path$ is the directory you want to search
spec$ is the files to look for. If you leave it empty - "" - then the function will append *.* to the end of the path. So, if you want to search for text files, put "*.txt" as the second parameter
files$ is a list containing all of the files found matching your criteria

FindFiles returns the number of files found


result$ = FindFiles("c:\\temp", "*.txt", &files$);
if(result$ > 0)
msg$ = "";
foreach(files$)
msg$ = msg$ + files.cFileName$ + "\n";
end;
ShowMessage(msg$);
end;


The information stored in files$ is actually the structure that FindFirstFile / FindNextFile uses. The reason for that is because these functions also return directory names, so you will need the first field of the structure to determine if the item is a directory or not. If this doesn't completely make sense, please feel free to ask more questions.

PointOfLight
01-07-2007, 07:50 PM
Sorry, forgot I couldn't attach PPL files.[br]1168199421_49_FT2619_findfile.zip

Heinz
01-08-2007, 09:14 AM
Great! Thanks a lot Eric! That is very helpfull :)

matteo.m
01-08-2007, 02:07 PM
Yeah very nice of you Eric , thanks a lot

didier.13
08-21-2007, 06:23 PM
I faced an issue with this code, it works on PC but not on PPC. Kornalius explained me it was because default format on PPC is widechar while is it char on PC. So the returned values must be converted using char() command on PPC.
To work on any platform, the code should be written:

result$ = FindFiles("c:\\temp", "*.txt" , &files$);
if(result$ > 0)
msg$ = "";
foreach(files$)
msg$ = msg$ + char(files.cFileName$ + "\ n");
end;
ShowMessage(msg$);
end;

I did not find the solution by myself, Kornelius did, I simply wanted to share it with next users.

Didier

kornalius
08-21-2007, 06:46 PM
Thank you for updating this thread Didier, very appreciated.

Slither
11-05-2007, 12:11 AM
Hi Guys,

downloaded POL's PPL function and added it in my initialisation code area. I then went and made a button on the form and attached didier's suggested crossplatform code.

When I execute it shows a single character of either comma, square or & symbol everytime the showmessage is run. When it reaches the end of the foreach condition it shows the last showmessage which has all files in that directory listed perfectly in one showmessage box.

Is this normal. I'm aware that current directory and Up one level is represented by a period and a double period respectively, is there any reason for the squares???

[Edit : Found the issue, i had a showmessage in the wrong place at the wrong time...lolol]

Works great I like it!

Regards,

Slither2006.