Archived

This topic is now archived and is closed to further replies.

Seek File on Disk

This topic is 5648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to do a program in C (not in C++) that seek all files with a given extension (for ex: get all the .MP3 file on the disk). Then I''ll use fopen routine to access the file I''ve got. What are the functions that I must use to get those lists? Dont'' want use OpenDialog or etc.. because i would like to do it in C with VC6... Thanks, and sorry for asking a such question I''''m 4 & I draw

Share this post


Link to post
Share on other sites
oh, sorry, I just realized you wanted C and not C++. Well I don't know about doing that in C, but the code below works in VC++.

possible a recursive path-search algo, looking something like

psuedo-code

          
void ScanDirectory(LPCTSTR path)
{
WIN32_FIND_DATA fileData
char buf[MAX_PATH]

SetCurrentDirectory(path)

Call FindFirstFile w/ fileData

loop forever
{
If fileData.cFileName contains ".mp3"
AddMp3ToList(fileData.cFileName, path);

if fileData.cFileName is a directory (fileData.dwFileAttributes == FILE_ATTRIBUTES_DIRECTORY)
{
copy path to buf
append '\\' to buf
append fileData.cFileName to buf
ScanDirectory(buf) // scan new directory

SetCurrentDirectory(path) // continue with this directory

}

if FindNextFile(file handle, fileData) == false
break
}
}


When converted to real code, that pseudo should scan every directory on the computer and then call AddMp3ToList for every MP3 it finds.

Let me know if you'd rather see real code.

[edited by - Ronin Magus on June 24, 2002 12:45:40 PM]

Share this post


Link to post
Share on other sites
Yes I would like see the real code
Just for "Getting path" and for the "FileData contains" because my problem it''s I just don''t know what are the function (methods) corresponding to that pseudo code.
Thanks.

I''''m 4 & I draw

Share this post


Link to post
Share on other sites
The code for seeing if fileData.cFileName contains ".mp3" would just be a searching algorithm through the string that looks for .mp3. It shouldn't be that hard to write but I don't have time right now, sorry


    if fileData.cFileName is a directory (fileData.dwFileAttributes == FILE_ATTRIBUTES_DIRECTORY)
{
copy path to buf
append '\\' to buf
append fileData.cFileName to buf
ScanDirectory(buf) // scan new director

SetCurrentDirectory(path) // continue with this directory

}


would become


        
if(fileData.dwFileattributes == FILE_ATTRIBUTE_DIRECTORY)
{
strcpy(buf, path);
strcat(buf, "\\");
strcat(buf, fileData.cFileName);
ScanDirectory(buf); // recursive call, watch out--it's subtle

SetCurrentDirectory(path);
}


The code above is what traverses the directory trees.


[edited by - Ronin Magus on June 24, 2002 3:27:06 PM]

Share this post


Link to post
Share on other sites
Well, here. It uses Shell Lightweight API for path manipulation and ATL/MFC CString for string manipulation, but you can replace both with your own functions and std::string very easily.

    
template <class Callback>
Callback DirectoryWalk(const CString& StartDir, BOOL Recurse, Callback fn)
{
TCHAR Path[MAX_PATH];
WIN32_FIND_DATA FindData;

std::stack <CString, std::vector <CString> > DirStack;
DirStack.push(StartDir);
while (!DirStack.empty())
{
CString Directory = DirStack.top();
DirStack.pop();
PathCombine(Path, Directory, _T("*.*"));
CFindFileHandle ffh = FindFirstFile(Path, &FindData);
if (ffh != INVALID_HANDLE_VALUE)
{
do
{
fn(Directory, FindData);
// Expand strcmp calls for optimization

if (Recurse && FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
!(FindData.cFileName[0] == _T('.') && (FindData.cFileName[1] == 0 || FindData.cFileName[1] == _T('.') && FindData.cFileName[2] == 0)))
{
PathCombine(Path, Directory, FindData.cFileName);
DirStack.push(Path);
}
} while (FindNextFile(ffh, &FindData));
}
}

return fn;
}



[edited by - IndirectX on June 26, 2002 12:30:42 AM]

Share this post


Link to post
Share on other sites
Yeah I''ve tried FinFirstFile FindNextFile et FindClose, it works perfectly (with recursive scan).
I also integrated multiple extension in my scan (So several result in one pass)
Thx everybody for help

(I also look for "how to get lan shared direcories" ? everybody knows how to get directories from Workgroup Shared directories?
The directories aren''t link with a Letter (ie T: <=> \\PC1\Files)
I just want to scan also in those directories that are shared on my workgroup)

Thx.

I''''m 4 & I draw

Share this post


Link to post
Share on other sites
quote:
Original post by Antorian
(I also look for "how to get lan shared direcories" ? everybody knows how to get directories from Workgroup Shared directories?
The directories aren''t link with a Letter (ie T: <=> \\PC1\Files)
I just want to scan also in those directories that are shared on my workgroup)


Same way you''d search for files?

Share this post


Link to post
Share on other sites