乞丐中的霸主(ぃ枫.net)(天行健,君子以自强不息;地势坤,君子以厚德载物! -《周易》)QQ讨论群(交流EVC++,VC++,USB驱动程序开发,上下机位通讯,无线网络通讯技术,Mobile开发)帮主QQ号:414885058

C#,C++ 学习

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
<PRE>void RecursiveDelete(CString szPath)
{
    CFileFind ff;
    CString path 
= szPath;
    
    
if(path.Right(1!= "\\")
        path 
+= "\\";

    path 
+= "*.*";

    BOOL res 
= ff.FindFile(path);

    
while(res)
    
{
        res 
= ff.FindNextFile();
        
if (!ff.IsDots() && !ff.IsDirectory())
            DeleteFile(ff.GetFilePath());
        
else if (ff.IsDirectory())
        
{
            path 
= ff.GetFilePath();
            RecursiveDelete(path);
            RemoveDirectory(path);
        }

    }

}
</PRE>

The CreateDir function creates folders and subfolders thereby completing the whole path passed to it. If the folder already exists, it is left unaffected, but if it doesn't exist, it is created. The CreateDirectory WIN32 API lets us create a directory, but it works only if the parent directory already exists. This function overcomes this limitation.

<PRE>void CreateDir(char* Path)
{
 
char DirName[256];
 
char* p = Path;
 
char* q = DirName; 
 
while(*p)
 
{
   
if (('\\' == *p) || ('/' == *p))
   
{
     
if (':' != *(p-1))
     
{
        CreateDirectory(DirName, NULL);
     }

   }

   
*q++ = *p++;
   
*= '\0';
 }

 CreateDirectory(DirName, NULL);
}
</PRE>

The DeleteAllFiles function deletes all the files (not folders) present in the specified path:

<PRE>void DeleteAllFiles(char* folderPath)
{
 
char fileFound[256];
 WIN32_FIND_DATA info;
 HANDLE hp; 
 sprintf(fileFound, 
"%s\\*.*", folderPath);
 hp 
= FindFirstFile(fileFound, &info);
 
do
    
{
       sprintf(fileFound,
"%s\\%s", folderPath, info.cFileName);
       DeleteFile(fileFound);
 
    }
while(FindNextFile(hp, &info)); 
 FindClose(hp);
}
</PRE>


The EmptyDirectory function deletes all the contents from a specified directory. The RemoveDirectory WIN32 API deletes an existing empty directory, but it doesn't work if the directory isn't empty. This function overcomes this limitation:

<PRE>void EmptyDirectory(char* folderPath)
{
 
char fileFound[256];
 WIN32_FIND_DATA info;
 HANDLE hp; 
 sprintf(fileFound, 
"%s\\*.*", folderPath);
 hp 
= FindFirstFile(fileFound, &info);
 
do
    
{
        
if (!((strcmp(info.cFileName, ".")==0)||
              (strcmp(info.cFileName, 
"..")==0)))
        
{
          
if((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==
                                     FILE_ATTRIBUTE_DIRECTORY)
          
{
              
string subFolder = folderPath;
              subFolder.append(
"\\");
              subFolder.append(info.cFileName);
              EmptyDirectory((
char*)subFolder.c_str());
              RemoveDirectory(subFolder.c_str());
          }

          
else
          
{
              sprintf(fileFound,
"%s\\%s", folderPath, info.cFileName);
              BOOL retVal 
= DeleteFile(fileFound);
          }

        }

 
    }
while(FindNextFile(hp, &info)); 
 FindClose(hp);
}
</PRE>

浏览目录dialog:
void CTestBrowseDlg::OnBrowse() 
{
    CString str;
    BROWSEINFO bi;
    
char name[MAX_PATH];
    ZeroMemory(
&bi,sizeof(BROWSEINFO));
    bi.hwndOwner
=GetSafeHwnd();
    bi.pszDisplayName
=name;
    bi.lpszTitle
="Select folder";
    bi.ulFlags
=BIF_USENEWUI;
    LPITEMIDLIST idl
=SHBrowseForFolder(&bi);
    
if(idl==NULL)
        
return;
    SHGetPathFromIDList(idl,str.GetBuffer(MAX_PATH));
    str.ReleaseBuffer();
    m_Path
=str;
    
if(str.GetAt(str.GetLength()-1)!='\\')
        m_Path
+="\\";
    UpdateData(FALSE);
}

得到某目录下的所有文件:
void RecursiveDelete(CString szPath)
{
    CFileFind ff;
    CString path 
= szPath;

    
if(path.Right(1!= "\\")
        path 
+= "\\";

    path 
+= "*.*";

    BOOL res 
= ff.FindFile(path);

    
while(res)
    
{
        res 
= ff.FindNextFile();
        
if ( !ff.IsDots()&&ff.IsDirectory())
        
{
            
            path 
= ff.GetFilePath();
            RecursiveDelete(path);
        }

        
else if (!ff.IsDirectory()&&!ff.IsDots())
        
{
            CString ss ; ss
= ff.GetFileName();
            printf(
"%s\n",ss);
            
        }

    }

    ff.Close();
}
posted on 2008-07-28 11:59  arvin2012  阅读(618)  评论(0)    收藏  举报