Howdy, Stranger!

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

Categories

Recursive Directory Listing

bcgreenbcgreen Member Posts: 4
OK, this is driving me nuts. I've reviewed tons of code snippets and can't figure this out. When you're doing a recursive listing of directories/files, how the heck do you manage going into and out of every directory? What I mean is this: I have code I've written that will list files in a directory. When it hits the first subdirectory, it'll go in, do it's thing, etc. But-- what about all the subdirectories after the first one I enter? I've seen code that just a few lines long that does the trick, but I can't see how it works. All I see is that when a directory is found, it's entered and scanned. But I don't see where the program is jumping back up to the original directory to scan the rest of the folders! HELP! My brain hurts!!!

Thanks,
Bryan

Comments

  • chick80chick80 Member Posts: 349
    Well, you do this with a recursive call.
    You have a function that will scan the files in a directory.
    The function simply do something like this (this is just pseudocode, it won't work, of course):

    [code]
    void ListFiles(char *dirname)
    {
    file = FindFirstFile(dirname);

    while (file)
    {
    // Do whatever you want with the file (print the name,
    // store it somewhere etc).

    file = FindNextFile(file);
    }
    }
    [/code]

    This will only list all the files/subdir, though.
    So you should improve this like:

    [code]
    void ListFiles(char *dirname)
    {
    file = FindFirstFile(dirname);

    while (file)
    {
    if (file == directory)
    {
    ListFiles(file);
    }
    else
    {
    // Do whatever you want with the file (print the name,
    // store it somewhere etc).
    }

    file = FindNextFile(file);
    }
    }
    [/code]

    So, when you find a subdir you just recall the function and do the same things in the subdir.
    When the function finishes to analyze the subdir, it will continue from the next file/subdir in the parent dir.

    For example if you have:

    [code]
    [green][/green]
    |-[green]
      [/green]
      | |
      | |-File 3
      |-File 1
      |-File 2
      [/code]

      The function starts to scan dir1, and finds dir2. So it will call itself recursively and finds file3. It stops and return to the first call, finds file1 and then file2.

      If I was not clear just ask!

      bye
      nICO

      [hr]
      [italic]How beautiful, if Sorrow had not made sorrow more beautiful than Beauty itself.[/italic]
      JOHN KEATS


  • bcgreenbcgreen Member Posts: 4
    OK...that's a clearer explanation than I've found anywhere else!

    Now I just need to figure out how to tweak my code to make this work; my first attempt was a recursive function and would drill down through each subdirectory--- but ONLY for the FIRST subdirectory it found...I'll use your pseudocode as a guide and see what I can come up with.

    Thank you very very much!!!


    : Well, you do this with a recursive call.
    : You have a function that will scan the files in a directory.
    : The function simply do something like this (this is just pseudocode, it won't work, of course):
    :
    : [code]
    : void ListFiles(char *dirname)
    : {
    : file = FindFirstFile(dirname);
    :
    : while (file)
    : {
    : // Do whatever you want with the file (print the name,
    : // store it somewhere etc).
    :
    : file = FindNextFile(file);
    : }
    : }
    : [/code]
    :
    : This will only list all the files/subdir, though.
    : So you should improve this like:
    :
    : [code]
    : void ListFiles(char *dirname)
    : {
    : file = FindFirstFile(dirname);
    :
    : while (file)
    : {
    : if (file == directory)
    : {
    : ListFiles(file);
    : }
    : else
    : {
    : // Do whatever you want with the file (print the name,
    : // store it somewhere etc).
    : }
    :
    : file = FindNextFile(file);
    : }
    : }
    : [/code]
    :
    : So, when you find a subdir you just recall the function and do the same things in the subdir.
    : When the function finishes to analyze the subdir, it will continue from the next file/subdir in the parent dir.
    :
    : For example if you have:
    :
    : [code]
    : [green][/green]
    : |-[green]
      [/green]
      : | |
      : | |-File 3
      : |-File 1
      : |-File 2
      : [/code]
      :
      : The function starts to scan dir1, and finds dir2. So it will call itself recursively and finds file3. It stops and return to the first call, finds file1 and then file2.
      :
      : If I was not clear just ask!
      :
      : bye
      : nICO
      :
      : [hr]
      : [italic]How beautiful, if Sorrow had not made sorrow more beautiful than Beauty itself.[/italic]
      : JOHN KEATS
      :
      :
      :

  • bcgreenbcgreen Member Posts: 4
    [b][red]This message was edited by bcgreen at 2004-5-8 0:49:59[/red][/b][hr]
    [b][red]This message was edited by bcgreen at 2004-5-7 20:40:50[/red][/b][hr]
    My code's not working! Here's what I have so far:

    [code]
    void scandir(HWND hwndDlg, char dirtoscan[])
    {
    SetCurrentDirectory(dirtoscan);
    strcat(dirtoscan, "\*");
    hNextFile = FindFirstFile(dirtoscan, &wfd);
    while(FindNextFile(hNextFile, &wfd))
    {
    filename = wfd.cFileName;
    if (strcmp(filename, "..") == 0 || strcmp(filename, ".") == 0)
    {
    continue;
    }

    if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
    GetFullPathName(filename, 255, path, &filepart);
    scandir(hwndDlg, path);
    }
    GetFullPathName(filename, 255, path, &filepart);
    SendDlgItemMessage(hwndDlg, LIST_VIEW, LB_INSERTSTRING, -1, (LPARAM)path);
    }
    }
    [/code]

    Note: "wfd" is a "WIN32_FIND_DATA" structure.

    It starts listing files, goes into the first directory it finds, list the files until it hits another directory, etc. without returning to where it left off the first time... It's not recursing properly. I know it's some simple mistake on my part, but I'm not advanced enough to figure it out. Help, anyone?

    ###UPDATE###
    OK, I've put all the variables my function uses inside the function (rather than making them global, as they were before) and my function now works. If someone could please explain why this is, please let me know! I'm glad my function works, but now I'm dying to know why all the variables must be local for the recursion to work!

    Thanks much!!!
    Bryan



    : OK, this is driving me nuts. I've reviewed tons of code snippets and can't figure this out. When you're doing a recursive listing of directories/files, how the heck do you manage going into and out of every directory? What I mean is this: I have code I've written that will list files in a directory. When it hits the first subdirectory, it'll go in, do it's thing, etc. But-- what about all the subdirectories after the first one I enter? I've seen code that just a few lines long that does the trick, but I can't see how it works. All I see is that when a directory is found, it's entered and scanned. But I don't see where the program is jumping back up to the original directory to scan the rest of the folders! HELP! My brain hurts!!!
    :
    : Thanks,
    : Bryan
    :





  • chick80chick80 Member Posts: 349
    Well, if you put the variables global, what happens is that when the function recalls itself it will overwrite the contents of the global var, and so cannot recurse properly.
    Local variables, instead, are "available" only in the function itself, so another function cannot access (and modify) them, no matter if the other function it's the same function called recursively.

    If you make the variables global, wfd will be first assigned to the first subdir, but when the functions call itself recursively it will be overwritten and will subsequently contain info on the subdir files.
    When the subdir has been scanned and you return to the calling function the original value of wfd will be lost, and so it won't work.

    nICO

    [hr]
    [italic]How beautiful, if Sorrow had not made sorrow more beautiful than Beauty itself.[/italic]
    JOHN KEATS


  • bcgreenbcgreen Member Posts: 4
    Thanks for clearing that up for me! You've been very helpful...

    Bryan

    : Well, if you put the variables global, what happens is that when the function recalls itself it will overwrite the contents of the global var, and so cannot recurse properly.
    : Local variables, instead, are "available" only in the function itself, so another function cannot access (and modify) them, no matter if the other function it's the same function called recursively.
    :
    : If you make the variables global, wfd will be first assigned to the first subdir, but when the functions call itself recursively it will be overwritten and will subsequently contain info on the subdir files.
    : When the subdir has been scanned and you return to the calling function the original value of wfd will be lost, and so it won't work.
    :
    : nICO
    :
    : [hr]
    : [italic]How beautiful, if Sorrow had not made sorrow more beautiful than Beauty itself.[/italic]
    : JOHN KEATS
    :
    :
    :

Sign In or Register to comment.