 {
{ CFileFind ff;
    CFileFind ff; CString path = szPath;
    CString path = szPath; 
     if(path.Right(1) != "\\")
    if(path.Right(1) != "\\") path += "\\";
        path += "\\";
 path += "*.*";
    path += "*.*";
 BOOL res = ff.FindFile(path);
    BOOL res = ff.FindFile(path);
 while(res)
    while(res) {
    { res = ff.FindNextFile();
        res = ff.FindNextFile(); if (!ff.IsDots() && !ff.IsDirectory())
        if (!ff.IsDots() && !ff.IsDirectory()) DeleteFile(ff.GetFilePath());
            DeleteFile(ff.GetFilePath()); else if (ff.IsDirectory())
        else if (ff.IsDirectory()) {
        { path = ff.GetFilePath();
            path = ff.GetFilePath(); RecursiveDelete(path);
            RecursiveDelete(path); RemoveDirectory(path);
            RemoveDirectory(path); }
        } }
    } }</PRE>
}</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)
<PRE>void CreateDir(char* Path) {
{ char DirName[256];
 char DirName[256]; char* p = Path;
 char* p = Path; char* q = DirName;
 char* q = DirName;  while(*p)
 while(*p) {
 { if (('\\' == *p) || ('/' == *p))
   if (('\\' == *p) || ('/' == *p)) {
   { if (':' != *(p-1))
     if (':' != *(p-1)) {
     { CreateDirectory(DirName, NULL);
        CreateDirectory(DirName, NULL); }
     } }
   } *q++ = *p++;
   *q++ = *p++; *q = '\0';
   *q = '\0'; }
 } CreateDirectory(DirName, NULL);
 CreateDirectory(DirName, NULL); }</PRE>
}</PRE>The DeleteAllFiles function deletes all the files (not folders) present in the specified path:
 <PRE>void DeleteAllFiles(char* folderPath)
<PRE>void DeleteAllFiles(char* folderPath) {
{ char fileFound[256];
 char fileFound[256]; WIN32_FIND_DATA info;
 WIN32_FIND_DATA info; HANDLE hp;
 HANDLE hp;  sprintf(fileFound, "%s\\*.*", folderPath);
 sprintf(fileFound, "%s\\*.*", folderPath); hp = FindFirstFile(fileFound, &info);
 hp = FindFirstFile(fileFound, &info); do
 do {
    { sprintf(fileFound,"%s\\%s", folderPath, info.cFileName);
       sprintf(fileFound,"%s\\%s", folderPath, info.cFileName); DeleteFile(fileFound);
       DeleteFile(fileFound); 
  }while(FindNextFile(hp, &info));
    }while(FindNextFile(hp, &info));  FindClose(hp);
 FindClose(hp); }</PRE>
}</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)
<PRE>void EmptyDirectory(char* folderPath) {
{ char fileFound[256];
 char fileFound[256]; WIN32_FIND_DATA info;
 WIN32_FIND_DATA info; HANDLE hp;
 HANDLE hp;  sprintf(fileFound, "%s\\*.*", folderPath);
 sprintf(fileFound, "%s\\*.*", folderPath); hp = FindFirstFile(fileFound, &info);
 hp = FindFirstFile(fileFound, &info); do
 do {
    { if (!((strcmp(info.cFileName, ".")==0)||
        if (!((strcmp(info.cFileName, ".")==0)|| (strcmp(info.cFileName, "..")==0)))
              (strcmp(info.cFileName, "..")==0))) {
        { if((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==
          if((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)== FILE_ATTRIBUTE_DIRECTORY)
                                     FILE_ATTRIBUTE_DIRECTORY) {
          { string subFolder = folderPath;
              string subFolder = folderPath; subFolder.append("\\");
              subFolder.append("\\"); subFolder.append(info.cFileName);
              subFolder.append(info.cFileName); EmptyDirectory((char*)subFolder.c_str());
              EmptyDirectory((char*)subFolder.c_str()); RemoveDirectory(subFolder.c_str());
              RemoveDirectory(subFolder.c_str()); }
          } else
          else {
          { sprintf(fileFound,"%s\\%s", folderPath, info.cFileName);
              sprintf(fileFound,"%s\\%s", folderPath, info.cFileName); BOOL retVal = DeleteFile(fileFound);
              BOOL retVal = DeleteFile(fileFound); }
          } }
        } 
  }while(FindNextFile(hp, &info));
    }while(FindNextFile(hp, &info));  FindClose(hp);
 FindClose(hp); }</PRE>
}</PRE>浏览目录dialog:
 void CTestBrowseDlg::OnBrowse()
void CTestBrowseDlg::OnBrowse()  {
{ CString str;
    CString str; BROWSEINFO bi;
    BROWSEINFO bi; char name[MAX_PATH];
    char name[MAX_PATH]; ZeroMemory(&bi,sizeof(BROWSEINFO));
    ZeroMemory(&bi,sizeof(BROWSEINFO)); bi.hwndOwner=GetSafeHwnd();
    bi.hwndOwner=GetSafeHwnd(); bi.pszDisplayName=name;
    bi.pszDisplayName=name; bi.lpszTitle="Select folder";
    bi.lpszTitle="Select folder"; bi.ulFlags=BIF_USENEWUI;
    bi.ulFlags=BIF_USENEWUI; LPITEMIDLIST idl=SHBrowseForFolder(&bi);
    LPITEMIDLIST idl=SHBrowseForFolder(&bi); if(idl==NULL)
    if(idl==NULL) return;
        return; SHGetPathFromIDList(idl,str.GetBuffer(MAX_PATH));
    SHGetPathFromIDList(idl,str.GetBuffer(MAX_PATH)); str.ReleaseBuffer();
    str.ReleaseBuffer(); m_Path=str;
    m_Path=str; if(str.GetAt(str.GetLength()-1)!='\\')
    if(str.GetAt(str.GetLength()-1)!='\\') m_Path+="\\";
        m_Path+="\\"; UpdateData(FALSE);
    UpdateData(FALSE); }
}得到某目录下的所有文件:
 void RecursiveDelete(CString szPath)
void RecursiveDelete(CString szPath) {
{ CFileFind ff;
    CFileFind ff; CString path = szPath;
    CString path = szPath;
 if(path.Right(1) != "\\")
    if(path.Right(1) != "\\") path += "\\";
        path += "\\";
 path += "*.*";
    path += "*.*";
 BOOL res = ff.FindFile(path);
    BOOL res = ff.FindFile(path);
 while(res)
    while(res) {
    { res = ff.FindNextFile();
        res = ff.FindNextFile(); if ( !ff.IsDots()&&ff.IsDirectory())
        if ( !ff.IsDots()&&ff.IsDirectory()) {
        { 
             path = ff.GetFilePath();
            path = ff.GetFilePath(); RecursiveDelete(path);
            RecursiveDelete(path); }
        } else if (!ff.IsDirectory()&&!ff.IsDots())
        else if (!ff.IsDirectory()&&!ff.IsDots()) {
        { CString ss ; ss= ff.GetFileName();
            CString ss ; ss= ff.GetFileName(); printf("%s\n",ss);
            printf("%s\n",ss); 
             }
        } }
    } ff.Close();
    ff.Close(); }
} 
                     
                    
                 
                    
                 

 
        

 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号