Searching files

i need to search files inside directories, and if has a directory inside this directory, search there too. how can i do it? my code right now is:

int LoadList (char *filename, int dir)
{
WIN32_FIND_DATA FindFileData;
HANDLE Find;
char temp[1024][50];
int list_count = 0;
int isdir;
FILE *arq;

Find = FindFirstFile (filename, &FindFileData);
arq = fopen("mp3_list.txt","w");

if (Find == INVALID_HANDLE_VALUE)
return 0;

do
{
isdir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;

if ((dir && isdir) || (!dir && !isdir))
{
StripExtension (FindFileData.cFileName, temp[list_count]);
fprintf(arq,"%s
",temp[list_count]);
list_count++;
}

} while (FindNextFile (Find, &FindFileData));

fclose(arq);

return list_count;
}

Comments

  • : i need to search files inside directories, and if has a directory inside this directory, search there too. how can i do it? my code right now is:
    :
    : int LoadList (char *filename, int dir)
    : {
    : WIN32_FIND_DATA FindFileData;
    : HANDLE Find;
    : char temp[1024][50];
    : int list_count = 0;
    : int isdir;
    : FILE *arq;
    :
    : Find = FindFirstFile (filename, &FindFileData);
    : arq = fopen("mp3_list.txt","w");
    :
    : if (Find == INVALID_HANDLE_VALUE)
    : return 0; [b][red]// 'arq' left open![/red][/b]
    :
    : do
    : {
    : isdir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
    :
    : if ((dir && isdir) || (!dir && !isdir))
    : {
    : StripExtension (FindFileData.cFileName, temp[list_count]);
    : fprintf(arq,"%s
    ",temp[list_count]);
    : list_count++;
    : }
    :
    : } while (FindNextFile (Find, &FindFileData));
    :
    : fclose(arq);
    : [b][red]'Find' left open![/red][/b]
    : return list_count;
    : }
    :
    [blue]At some point you have to call [italic]LoadList()[/italic] (call itself) inside a loop for directories. You should surround that call with these:[/blue][code]
    SetCurrentDirectory (FindFileData.cFileName); // Get into a folder
    LoadList (...);
    SetCurrentDirectory (".."); // Get back to call a sibling-folder
    [/code]
  • : : i need to search files inside directories, and if has a directory inside this directory, search there too. how can i do it? my code right now is:
    : :
    : : int LoadList (char *filename, int dir)
    : : {
    : : WIN32_FIND_DATA FindFileData;
    : : HANDLE Find;
    : : char temp[1024][50];
    : : int list_count = 0;
    : : int isdir;
    : : FILE *arq;
    : :
    : : Find = FindFirstFile (filename, &FindFileData);
    : : arq = fopen("mp3_list.txt","w");
    : :
    : : if (Find == INVALID_HANDLE_VALUE)
    : : return 0; [b][red]// 'arq' left open![/red][/b]
    : :
    : : do
    : : {
    : : isdir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
    : :
    : : if ((dir && isdir) || (!dir && !isdir))
    : : {
    : : StripExtension (FindFileData.cFileName, temp[list_count]);
    : : fprintf(arq,"%s
    ",temp[list_count]);
    : : list_count++;
    : : }
    : :
    : : } while (FindNextFile (Find, &FindFileData));
    : :
    : : fclose(arq);
    : : [b][red]'Find' left open![/red][/b]
    : : return list_count;
    : : }
    : :
    : [blue]At some point you have to call [italic]LoadList()[/italic] (call itself) inside a loop for directories. You should surround that call with these:[/blue][code]
    : SetCurrentDirectory (FindFileData.cFileName); // Get into a folder
    : LoadList (...);
    : SetCurrentDirectory (".."); // Get back to call a sibling-folder
    : [/code]
    :
    thanks! i'm working on it

  • [blue]This shows how to delete the files and directories, but you can easily modify it do so something else.[/blue]
    http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=585&MsgID=174802&Setting=A9999F0001

Sign In or Register to comment.

Howdy, Stranger!

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

Categories