Seek File on Disk

Started by
8 comments, last by Antorian 21 years, 9 months ago
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
I''m 4 & I draw
Advertisement
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]
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
I''m 4 & I draw
Are you sure you don''t want C++ code? I have a non-recursive STL-style algorithm for directory walking that''s in C++.
---visit #directxdev on afternet <- not just for directx, despite the name
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 bufappend '\\' to bufappend fileData.cFileName to bufScanDirectory(buf) // scan new directorSetCurrentDirectory(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]
Yeah C or C++ (Without Windows Graphic Interface) is welcome.
Let me see that.
Thanks to all of you.

I''''m 4 & I draw
I''m 4 & I draw
findfirst() findnext() and findclose() are probably what you''re looking for. Only work on windows AFAIK, but it''s straight C.
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]
---visit #directxdev on afternet <- not just for directx, despite the name
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
I''m 4 & I draw
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?
---visit #directxdev on afternet <- not just for directx, despite the name

This topic is closed to new replies.

Advertisement