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. Delete subfolder

Delete subfolder

Scheduled Pinned Locked Moved C / C++ / MFC
help
5 Posts 4 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.
  • M Offline
    M Offline
    mihai123
    wrote on last edited by
    #1

    Weird problem I have this function (is a function changed I used it to another purpose)

    void browseFolder(TCHAR *path, TCHAR *initial)
    {
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind=INVALID_HANDLE_VALUE;
    TCHAR dir[MAX_PATH];
    TCHAR aux[MAX_PATH];
    StringCchCopy(aux, MAX_PATH, path);
    StringCchCat(aux, MAX_PATH, TEXT("\\*"));
    hFind= FindFirstFile(aux,&FindFileData);

    //browse the folder
    do
    {
    	if (FindFileData.dwFileAttributes & FILE\_ATTRIBUTE\_DIRECTORY)
    	{
    		if((\_tcscmp (FindFileData.cFileName, \_T(".")) != 0) && (\_tcscmp (FindFileData.cFileName, \_T("..")) != 0))
    		{  
    			//if is a subfolder we need to change the path and we call the function again
    			//change path
    			StringCchCopy(dir, MAX\_PATH, path);
    			StringCchCat(dir, MAX\_PATH, TEXT("\\\\"));
    			StringCchCat(dir, MAX\_PATH, FindFileData.cFileName);  // oldPat\\\\dirName
    			//\_tprintf(TEXT("\\n 
    

    %s\n"), FindFileData.cFileName);
    browseFolder(dir,initial); //if is folder we call the function again

    		}
    	}
    	else
    	 { //files we delete it
    		
    		 //set the current directory for copping the file
    		
    		  //create the correct path name for the files that will be downloaded
    		  TCHAR pathFileName\[MAX\_PATH\];
    		  StringCchCopy(pathFileName, MAX\_PATH, path);
                          StringCchCat(pathFileName, MAX\_PATH, TEXT("\\\\"));
    		  StringCchCat(pathFileName, MAX\_PATH, FindFileData.cFileName);
    
    		  CString szMessage;
    		  szMessage.Format(\_T("%s"),pathFileName);
    		  \_tprintf(\_T("%s\\n"),szMessage);
    		  DeleteFile(szMessage);
    	 }
     }
     while (FindNextFile(hFind, &FindFileData) != 0);
    // delete &FindFileData;
     hFind= INVALID\_HANDLE\_VALUE;
     
     \_tprintf(path);
     RemoveDirectory(path);
    

    }

    The function delete all the files but lives the folders intact I cannot understand why. Seems like they are in use and they cannot be deleted....

    T _ D 3 Replies Last reply
    0
    • M mihai123

      Weird problem I have this function (is a function changed I used it to another purpose)

      void browseFolder(TCHAR *path, TCHAR *initial)
      {
      WIN32_FIND_DATA FindFileData;
      HANDLE hFind=INVALID_HANDLE_VALUE;
      TCHAR dir[MAX_PATH];
      TCHAR aux[MAX_PATH];
      StringCchCopy(aux, MAX_PATH, path);
      StringCchCat(aux, MAX_PATH, TEXT("\\*"));
      hFind= FindFirstFile(aux,&FindFileData);

      //browse the folder
      do
      {
      	if (FindFileData.dwFileAttributes & FILE\_ATTRIBUTE\_DIRECTORY)
      	{
      		if((\_tcscmp (FindFileData.cFileName, \_T(".")) != 0) && (\_tcscmp (FindFileData.cFileName, \_T("..")) != 0))
      		{  
      			//if is a subfolder we need to change the path and we call the function again
      			//change path
      			StringCchCopy(dir, MAX\_PATH, path);
      			StringCchCat(dir, MAX\_PATH, TEXT("\\\\"));
      			StringCchCat(dir, MAX\_PATH, FindFileData.cFileName);  // oldPat\\\\dirName
      			//\_tprintf(TEXT("\\n 
      

      %s\n"), FindFileData.cFileName);
      browseFolder(dir,initial); //if is folder we call the function again

      		}
      	}
      	else
      	 { //files we delete it
      		
      		 //set the current directory for copping the file
      		
      		  //create the correct path name for the files that will be downloaded
      		  TCHAR pathFileName\[MAX\_PATH\];
      		  StringCchCopy(pathFileName, MAX\_PATH, path);
                            StringCchCat(pathFileName, MAX\_PATH, TEXT("\\\\"));
      		  StringCchCat(pathFileName, MAX\_PATH, FindFileData.cFileName);
      
      		  CString szMessage;
      		  szMessage.Format(\_T("%s"),pathFileName);
      		  \_tprintf(\_T("%s\\n"),szMessage);
      		  DeleteFile(szMessage);
      	 }
       }
       while (FindNextFile(hFind, &FindFileData) != 0);
      // delete &FindFileData;
       hFind= INVALID\_HANDLE\_VALUE;
       
       \_tprintf(path);
       RemoveDirectory(path);
      

      }

      The function delete all the files but lives the folders intact I cannot understand why. Seems like they are in use and they cannot be deleted....

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      HEY ! how many times are you going to ask this ? can't you follow the same thread ? it's not a chat buddy, it's a forum. so ask you question, and wait !

      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      1 Reply Last reply
      0
      • M mihai123

        Weird problem I have this function (is a function changed I used it to another purpose)

        void browseFolder(TCHAR *path, TCHAR *initial)
        {
        WIN32_FIND_DATA FindFileData;
        HANDLE hFind=INVALID_HANDLE_VALUE;
        TCHAR dir[MAX_PATH];
        TCHAR aux[MAX_PATH];
        StringCchCopy(aux, MAX_PATH, path);
        StringCchCat(aux, MAX_PATH, TEXT("\\*"));
        hFind= FindFirstFile(aux,&FindFileData);

        //browse the folder
        do
        {
        	if (FindFileData.dwFileAttributes & FILE\_ATTRIBUTE\_DIRECTORY)
        	{
        		if((\_tcscmp (FindFileData.cFileName, \_T(".")) != 0) && (\_tcscmp (FindFileData.cFileName, \_T("..")) != 0))
        		{  
        			//if is a subfolder we need to change the path and we call the function again
        			//change path
        			StringCchCopy(dir, MAX\_PATH, path);
        			StringCchCat(dir, MAX\_PATH, TEXT("\\\\"));
        			StringCchCat(dir, MAX\_PATH, FindFileData.cFileName);  // oldPat\\\\dirName
        			//\_tprintf(TEXT("\\n 
        

        %s\n"), FindFileData.cFileName);
        browseFolder(dir,initial); //if is folder we call the function again

        		}
        	}
        	else
        	 { //files we delete it
        		
        		 //set the current directory for copping the file
        		
        		  //create the correct path name for the files that will be downloaded
        		  TCHAR pathFileName\[MAX\_PATH\];
        		  StringCchCopy(pathFileName, MAX\_PATH, path);
                              StringCchCat(pathFileName, MAX\_PATH, TEXT("\\\\"));
        		  StringCchCat(pathFileName, MAX\_PATH, FindFileData.cFileName);
        
        		  CString szMessage;
        		  szMessage.Format(\_T("%s"),pathFileName);
        		  \_tprintf(\_T("%s\\n"),szMessage);
        		  DeleteFile(szMessage);
        	 }
         }
         while (FindNextFile(hFind, &FindFileData) != 0);
        // delete &FindFileData;
         hFind= INVALID\_HANDLE\_VALUE;
         
         \_tprintf(path);
         RemoveDirectory(path);
        

        }

        The function delete all the files but lives the folders intact I cannot understand why. Seems like they are in use and they cannot be deleted....

        _ Offline
        _ Offline
        _AnsHUMAN_
        wrote on last edited by
        #3

        Did you try GetLastError() or check that whether the files have been deleted completely before calling RemoveDirectory(). Try putting some Sleep(interval) between deleting files and the folder if you want to test this

        Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

        1 Reply Last reply
        0
        • M mihai123

          Weird problem I have this function (is a function changed I used it to another purpose)

          void browseFolder(TCHAR *path, TCHAR *initial)
          {
          WIN32_FIND_DATA FindFileData;
          HANDLE hFind=INVALID_HANDLE_VALUE;
          TCHAR dir[MAX_PATH];
          TCHAR aux[MAX_PATH];
          StringCchCopy(aux, MAX_PATH, path);
          StringCchCat(aux, MAX_PATH, TEXT("\\*"));
          hFind= FindFirstFile(aux,&FindFileData);

          //browse the folder
          do
          {
          	if (FindFileData.dwFileAttributes & FILE\_ATTRIBUTE\_DIRECTORY)
          	{
          		if((\_tcscmp (FindFileData.cFileName, \_T(".")) != 0) && (\_tcscmp (FindFileData.cFileName, \_T("..")) != 0))
          		{  
          			//if is a subfolder we need to change the path and we call the function again
          			//change path
          			StringCchCopy(dir, MAX\_PATH, path);
          			StringCchCat(dir, MAX\_PATH, TEXT("\\\\"));
          			StringCchCat(dir, MAX\_PATH, FindFileData.cFileName);  // oldPat\\\\dirName
          			//\_tprintf(TEXT("\\n 
          

          %s\n"), FindFileData.cFileName);
          browseFolder(dir,initial); //if is folder we call the function again

          		}
          	}
          	else
          	 { //files we delete it
          		
          		 //set the current directory for copping the file
          		
          		  //create the correct path name for the files that will be downloaded
          		  TCHAR pathFileName\[MAX\_PATH\];
          		  StringCchCopy(pathFileName, MAX\_PATH, path);
                                StringCchCat(pathFileName, MAX\_PATH, TEXT("\\\\"));
          		  StringCchCat(pathFileName, MAX\_PATH, FindFileData.cFileName);
          
          		  CString szMessage;
          		  szMessage.Format(\_T("%s"),pathFileName);
          		  \_tprintf(\_T("%s\\n"),szMessage);
          		  DeleteFile(szMessage);
          	 }
           }
           while (FindNextFile(hFind, &FindFileData) != 0);
          // delete &FindFileData;
           hFind= INVALID\_HANDLE\_VALUE;
           
           \_tprintf(path);
           RemoveDirectory(path);
          

          }

          The function delete all the files but lives the folders intact I cannot understand why. Seems like they are in use and they cannot be deleted....

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

          I do not see that you heeded the earlier suggestion of calling FindClose(hfind).

          "Love people and use things, not love things and use people." - Unknown

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          M 1 Reply Last reply
          0
          • D David Crow

            I do not see that you heeded the earlier suggestion of calling FindClose(hfind).

            "Love people and use things, not love things and use people." - Unknown

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            M Offline
            M Offline
            mihai123
            wrote on last edited by
            #5

            Thank you from the bottom of heart. The FindClose was the magic function now it works fine.

            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