Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding a random file

Status
Not open for further replies.

BungoMan

Programmer
May 13, 2002
12
US
for a program i am making i need to find a completely random file on the HD, basicly i want it to determine a random directory, then find a random file within that directory, i have NO clue where to begin on this one, i have read some stuff about getting directory lists

im thinking itll have to be a recursive function that puts all of the directories in the top of the HD in an array then chooses one at random, then it opens that directory, then it looks for files, if it finds files then it decides whether or not to look for more folders or open a file, if it (like a 1 in 4 chance to open the file if theres folders there), if it opens a file it chooses one in a similar manner that it chooses directories. if it decides to choose a directory it repeats itself. if it ends up in a directory with no files it should automaticly just up to the previous directory. i hope this makes sense... i just need to know the functions/syntax to do this, i can figure out most of it, but without a function(s) that takes directory names and puts them into subs in an array im lost =/
 
This sounds like the sort of thing a virus program would do!
I'm wondering if I'll regret giving you any help.

Code:
HANDLE MyFind;
WIN32_FIND_DATA FindData;
double FileSize;
char FileName[256];

MyFind = FindFirstFile("c:\\", &FindData);

do {
    if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) /* It's a directory */;
    strcpy(FileName, FindData.cFileName);
    FileSize = (FindData.nFileSizeHigh * MAXDWORD) + FindData.nFileSizeLow;
} while (FindNextFile(MyFind, &FindData));
 
no no viruses here, just curiosity (spelling?), i oftern find myself wondering about the weird stuff i can do and when i get stumped it annoys me to no end =/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top