Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Need help w/ a simple recursive function

Need help w/ a simple recursive function

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    georgiek50
    wrote on last edited by
    #1

    I wrote a function a while back to clean out a directory and all of its contents and tried re-writing the same one tonight but can't seem to get it right. Any chance someone can point out my mistake?short ClearEntireDirectory(char *szDirectory) { // Function to work on a directory until everything is wiped out HANDLE hFile; WIN32_FIND_DATA wfd; if ( SetCurrentDirectory(szDirectory) == 0 ) return 1; hFile = FindFirstFile("*.*", &wfd); while ( FindNextFile(hFile, &wfd) != 0 ) { if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && strcmp(wfd.cFileName, "..") != 0 && strcmp(wfd.cFileName, ".") != 0 ) { if ( ClearEntireDirectory(wfd.cFileName) == 1 ) return 1; } DeleteFile(wfd.cFileName); } if ( SetCurrentDirectory("..") == 0 ) return 1; if ( RemoveDirectory(szDirectory) == 0 ) return 1; FindClose(hFile); return 0; }
    I keep getting error 32 (ERROR_SHARING_VIOLATION) when I try to ultimatelly remove the directory.

    G D 2 Replies Last reply
    0
    • G georgiek50

      I wrote a function a while back to clean out a directory and all of its contents and tried re-writing the same one tonight but can't seem to get it right. Any chance someone can point out my mistake?short ClearEntireDirectory(char *szDirectory) { // Function to work on a directory until everything is wiped out HANDLE hFile; WIN32_FIND_DATA wfd; if ( SetCurrentDirectory(szDirectory) == 0 ) return 1; hFile = FindFirstFile("*.*", &wfd); while ( FindNextFile(hFile, &wfd) != 0 ) { if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && strcmp(wfd.cFileName, "..") != 0 && strcmp(wfd.cFileName, ".") != 0 ) { if ( ClearEntireDirectory(wfd.cFileName) == 1 ) return 1; } DeleteFile(wfd.cFileName); } if ( SetCurrentDirectory("..") == 0 ) return 1; if ( RemoveDirectory(szDirectory) == 0 ) return 1; FindClose(hFile); return 0; }
      I keep getting error 32 (ERROR_SHARING_VIOLATION) when I try to ultimatelly remove the directory.

      G Offline
      G Offline
      georgiek50
      wrote on last edited by
      #2

      I'm such an idiot!!! It would help if FindClose cam before RemoveDirectory!!!!!

      1 Reply Last reply
      0
      • G georgiek50

        I wrote a function a while back to clean out a directory and all of its contents and tried re-writing the same one tonight but can't seem to get it right. Any chance someone can point out my mistake?short ClearEntireDirectory(char *szDirectory) { // Function to work on a directory until everything is wiped out HANDLE hFile; WIN32_FIND_DATA wfd; if ( SetCurrentDirectory(szDirectory) == 0 ) return 1; hFile = FindFirstFile("*.*", &wfd); while ( FindNextFile(hFile, &wfd) != 0 ) { if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && strcmp(wfd.cFileName, "..") != 0 && strcmp(wfd.cFileName, ".") != 0 ) { if ( ClearEntireDirectory(wfd.cFileName) == 1 ) return 1; } DeleteFile(wfd.cFileName); } if ( SetCurrentDirectory("..") == 0 ) return 1; if ( RemoveDirectory(szDirectory) == 0 ) return 1; FindClose(hFile); return 0; }
        I keep getting error 32 (ERROR_SHARING_VIOLATION) when I try to ultimatelly remove the directory.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        The calls to SetCurrentDirectory() are unnecessary. If the usage of this function is low, it probably makes no difference, but if it is an oft-used function, changing directories so many times is not efficient.


        "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups