I need to find all files with a same extension on a drive... - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

I need to find all files with a same extension on a drive...

Anyone could provide me an algorithm to do that?

Something that covers all the folders starting from the root?

Thanx.


Comments

  • Here's a complete prog. Will find jpg files in the drive you want, I've used the Win32 API FindFirstFile/FindNextFile, you can convert to C's findfirst/findnext easy if you want to..



    #include "windows.h"

    #include "stdio.h"



    void search_folder( const char *pszFolderName, const char *pszExt )

    {

    // add *.* to folder name to search for all files and folders

    char szBuffer[ _MAX_PATH ];

    wsprintf( szBuffer, "%s\*.*", pszFolderName );



    // find first file

    WIN32_FIND_DATA wfd;

    HANDLE h = FindFirstFile( szBuffer, &wfd );

    if( h != INVALID_HANDLE_VALUE )

    {

    do

    {

    // check if its a folder

    if( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )

    {

    // skip . and .. or else we'd recurse to hell

    if( lstrcmpi( wfd.cFileName, "." ) && lstrcmpi( wfd.cFileName, ".." ) )

    {

    // build new folder name and search it

    wsprintf( szBuffer, "%s\%s", pszFolderName, wfd.cFileName );

    search_folder( szBuffer, pszExt );

    }

    }

    else

    {

    // its a file, check extension

    char szTempExt[ _MAX_PATH ];

    _splitpath( wfd.cFileName, NULL, NULL, NULL, szTempExt );

    if( lstrcmpi( (char*) ( szTempExt + 1 ), pszExt ) == 0 )

    {

    // got one, build full file name and show it

    wsprintf( szBuffer, "%s\%s", pszFolderName, wfd.cFileName );

    printf( "%s
    ", szBuffer );

    }

    }

    }

    while( FindNextFile( h, &wfd ) ); // find next file



    CloseHandle( h );

    }

    }





    main()

    {

    search_folder( "c:", "jpg" );



    return 0;

    }





    : Anyone could provide me an algorithm to do that?

    : Something that covers all the folders starting from the root?

    : Thanx.

    :






Sign In or Register to comment.