惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
美团技术团队
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
H
Help Net Security
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
博客园_首页
A
About on SuperTechFans
MongoDB | Blog
MongoDB | Blog
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
B
Blog
The Register - Security
The Register - Security
I
InfoQ
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
Last Week in AI
Last Week in AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
Jina AI
Jina AI
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News - Newest:
Hacker News - Newest: "LLM"
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
W
WeLiveSecurity
Latest news
Latest news
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 叶小钗
L
Lohrmann on Cybersecurity
博客园 - Franky
Recent Commits to openclaw:main
Recent Commits to openclaw:main
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - arvin2012

VC++常用方法函数集合, EVC++4.0 长用数据类型转换,FFT算法 指向函数的指针数组到底该怎么用 内存耗尽怎么办? 位运算符的一些简单应用 异步Socket通信总结 贵在坚持 vc++操作word MFC中的ClassWizard的使用 追MM与设计模式的有趣见解 动软.Net代码生成器 发布最新2.12版 vc中常用的方法 vc中读写注册表 简单好用的读写ini文件的类 Windows API 技巧集 vc 使用总结 VC 常见的108个问题 VC中动态生成控件 什么是COM,如何使用COM
c++ file and directory 删除,移动,目录浏览对话框,找某目录下的所有文件(包括子目录)
arvin2012 · 2008-07-28 · via 博客园 - arvin2012

<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();
}