急!求删除目录及目录下的所有文件 wince

时间:2021-02-22 12:07:17
大家好!我想了各种办法想删除目录下的所有文件,怎么都不成功,网上的两种办法都试过了

一、包含了头文件#include "shellapi.h"
BOOL CsystemDlg::DelTree(LPCTSTR lpszPath)
{
    SHFILEOPSTRUCT FileOp;
    FileOp.fFlags = FOF_NOCONFIRMATION;
    FileOp.hNameMappings = NULL;
    FileOp.hwnd = NULL;
    FileOp.lpszProgressTitle = NULL;
    FileOp.pFrom = lpszPath;
    FileOp.pTo = NULL;
    FileOp.wFunc = FO_DELETE;


    return ::SHFileOperationW(&FileOp)==0;
}

运行结果:
无法解析的外部符号 SHFileOperationW,该符号在函数 "public: int __cdecl CsystemDlg::DelTree(wchar_t const *)" (?DelTree@CsystemDlg@@QAAHPB_W@Z) 中被引用

二、

CString strDir = L"\\NandFlash\\2";
if(strDir.IsEmpty())  
{   
     RemoveDirectory(strDir);   
     return;   
}   
//   首先删除文件及子文件夹   
CFileFind   ff;   
BOOL   bFound   =   ff.FindFile(strDir+"\\*",0);   
while(bFound)   
{   
    bFound   =   ff.FindNextFile();   
     if(ff.GetFileName()=="."||ff.GetFileName()=="..")   
     continue;   
      //   去掉文件(夹)只读等属性   
     SetFileAttributes(ff.GetFilePath(),   FILE_ATTRIBUTE_NORMAL);   
     if(ff.IsDirectory())  
     {   
          //   递归删除子文件夹   
DeleteDirectory(ff.GetFilePath());   
RemoveDirectory(ff.GetFilePath());   
      }   
      else 
      {   
 DeleteFile(ff.GetFilePath());   //   删除文件   
       }   
}   
ff.Close();   
//   然后删除该文件夹   
RemoveDirectory(strDir);   

}



运行结果:
1>.\systemDlg.cpp(1838) : error C2065: 'CFileFind' : undeclared identifier

9 个解决方案

#1


#include "direct.h"

void DeleteDirFile(char *pchPath)
{
WIN32_FIND_DATA stData;

char chFile[_MAX_DIR] = _T("");
sprintf_s(chFile, sizeof(chFile), "%s%s", pchPath, "*.*");

HANDLE hFind = ::FindFirstFile(chFile, &stData);

if (hFind != INVALID_HANDLE_VALUE)
{           
while (::FindNextFile(hFind,&stData))
{
char chName[_MAX_FNAME] = _T("");

//判断是否为目录
if (stData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
strcpy_s(chName, sizeof(chName), stData.cFileName);

//判断是否为.和..
if (strcmp(chName, ".") != 0 && strcmp(chName, "..") != 0)
{
//如果是真正的目录,进行递归
char chChildPath[_MAX_DIR] = _T("");
sprintf_s(chChildPath, sizeof(chChildPath), "%s%s\\", pchPath, stData.cFileName);
DeleteDirFile(chChildPath);
}
}
else
{
sprintf_s(chFile, sizeof(chFile), "%s%s", pchPath, stData.cFileName);

DeleteFile(chFile);
}
}
::FindClose(hFind);
}

RemoveDirectory(pchPath);
}

#2


#include "direct.h" 头文件不能包含,
结果显示:
fatal error C1083: Cannot open include file: 'direct.h': No such file or directory


我的运行平台是wince5.0

#4


关于问题二:
wince 下目前不支持 CFileFind 类操作。
可以用 ::FindFirstFile(),::FindNextFile()以及Delete()函数。

#5


问题一:
我也有这样的问题,刚解决。

在你的工程中包含了头文件#include "shellapi.h" ,

这时还缺少库(ceShell),再加一句:

#pragma comment (lib,"Ceshell.lib")

这时应该可以了。

#6


我的问题解决了,在网上找的答案,分享一下
          char FileRoad[256]="\0";
 char DirRoad[256]="\0";
 char FindedFile[256]="\0";
 char FindedDir[256]="\0";  

 /////
 strcpy(FileRoad,pRoad);
 strcpy(DirRoad,pRoad);
 strcat(DirRoad,"\\*.*");

 //////
 TCHAR lpszFile[256] = _T("\0");

  char pFilePathName[60] = "\0";
  memset(pFilePathName,'\0',60);

 //////
 int nLen = 0;
 int nwLen = 0;
 nLen = strlen(DirRoad) + 1;
 nwLen = MultiByteToWideChar(CP_ACP, 0, DirRoad, nLen, NULL, 0);
 MultiByteToWideChar(CP_ACP, 0,DirRoad, nLen, lpszFile, nwLen); //lpszFile:_T("\NandFlash\2\*.*")

 /////
 WIN32_FIND_DATA findData;
 memset(findData.cFileName,0,260);
//////

 HANDLE hFindFile = NULL;

 char sk[256] = "\0";
 hFindFile=FindFirstFileW(lpszFile,&findData);
 if(hFindFile!=INVALID_HANDLE_VALUE)
 {
do

 FindClose(hFindFile);
  memset(findData.cFileName,0,260);
 hFindFile=FindFirstFileW(lpszFile,&findData);

 nLen = wcslen(findData.cFileName)+1; 
 WideCharToMultiByte(CP_ACP, 0, findData.cFileName, nLen, pFilePathName, 2*nLen, NULL, NULL);
 //

 memset(sk,'\0',256);
 strcat(sk,FileRoad);
 strcat(sk,"\\");
 strcat(sk,pFilePathName);
 //
 nLen = strlen(sk) + 1;
 nwLen = MultiByteToWideChar(CP_ACP, 0, sk, nLen, NULL, 0);
 MultiByteToWideChar(CP_ACP, 0,sk, nLen, lpszFile, nwLen); 
 DeleteFileW(lpszFile);

memset(sk,'\0',256);
  }while(FindNextFile(hFindFile,&findData));
 }

 char Foldername[256] = "\0";
 memset(Foldername,'\0',256);
 strcpy(Foldername,pRoad);
 nLen = strlen(Foldername) + 1;
 nwLen = MultiByteToWideChar(CP_ACP, 0, Foldername, nLen, NULL, 0);
 MultiByteToWideChar(CP_ACP, 0,Foldername, nLen, lpszFile, nwLen); //lpszFile:_T("\NandFlash\2\*.*")
CString ss;
ss = (CString)lpszFile;
FindClose(hFindFile);

 RemoveDirectory(lpszFile);

#7


lori227 的解决方法明显有问题 
这个语句就有问题的
char chFile[_MAX_DIR] = _T(""); 

#8


http://armlinux.uueasy.com/read-htm-tid-52.html

#9


该回复于2012-11-02 09:39:38被版主删除

#1


#include "direct.h"

void DeleteDirFile(char *pchPath)
{
WIN32_FIND_DATA stData;

char chFile[_MAX_DIR] = _T("");
sprintf_s(chFile, sizeof(chFile), "%s%s", pchPath, "*.*");

HANDLE hFind = ::FindFirstFile(chFile, &stData);

if (hFind != INVALID_HANDLE_VALUE)
{           
while (::FindNextFile(hFind,&stData))
{
char chName[_MAX_FNAME] = _T("");

//判断是否为目录
if (stData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
strcpy_s(chName, sizeof(chName), stData.cFileName);

//判断是否为.和..
if (strcmp(chName, ".") != 0 && strcmp(chName, "..") != 0)
{
//如果是真正的目录,进行递归
char chChildPath[_MAX_DIR] = _T("");
sprintf_s(chChildPath, sizeof(chChildPath), "%s%s\\", pchPath, stData.cFileName);
DeleteDirFile(chChildPath);
}
}
else
{
sprintf_s(chFile, sizeof(chFile), "%s%s", pchPath, stData.cFileName);

DeleteFile(chFile);
}
}
::FindClose(hFind);
}

RemoveDirectory(pchPath);
}

#2


#include "direct.h" 头文件不能包含,
结果显示:
fatal error C1083: Cannot open include file: 'direct.h': No such file or directory


我的运行平台是wince5.0

#3


#4


关于问题二:
wince 下目前不支持 CFileFind 类操作。
可以用 ::FindFirstFile(),::FindNextFile()以及Delete()函数。

#5


问题一:
我也有这样的问题,刚解决。

在你的工程中包含了头文件#include "shellapi.h" ,

这时还缺少库(ceShell),再加一句:

#pragma comment (lib,"Ceshell.lib")

这时应该可以了。

#6


我的问题解决了,在网上找的答案,分享一下
          char FileRoad[256]="\0";
 char DirRoad[256]="\0";
 char FindedFile[256]="\0";
 char FindedDir[256]="\0";  

 /////
 strcpy(FileRoad,pRoad);
 strcpy(DirRoad,pRoad);
 strcat(DirRoad,"\\*.*");

 //////
 TCHAR lpszFile[256] = _T("\0");

  char pFilePathName[60] = "\0";
  memset(pFilePathName,'\0',60);

 //////
 int nLen = 0;
 int nwLen = 0;
 nLen = strlen(DirRoad) + 1;
 nwLen = MultiByteToWideChar(CP_ACP, 0, DirRoad, nLen, NULL, 0);
 MultiByteToWideChar(CP_ACP, 0,DirRoad, nLen, lpszFile, nwLen); //lpszFile:_T("\NandFlash\2\*.*")

 /////
 WIN32_FIND_DATA findData;
 memset(findData.cFileName,0,260);
//////

 HANDLE hFindFile = NULL;

 char sk[256] = "\0";
 hFindFile=FindFirstFileW(lpszFile,&findData);
 if(hFindFile!=INVALID_HANDLE_VALUE)
 {
do

 FindClose(hFindFile);
  memset(findData.cFileName,0,260);
 hFindFile=FindFirstFileW(lpszFile,&findData);

 nLen = wcslen(findData.cFileName)+1; 
 WideCharToMultiByte(CP_ACP, 0, findData.cFileName, nLen, pFilePathName, 2*nLen, NULL, NULL);
 //

 memset(sk,'\0',256);
 strcat(sk,FileRoad);
 strcat(sk,"\\");
 strcat(sk,pFilePathName);
 //
 nLen = strlen(sk) + 1;
 nwLen = MultiByteToWideChar(CP_ACP, 0, sk, nLen, NULL, 0);
 MultiByteToWideChar(CP_ACP, 0,sk, nLen, lpszFile, nwLen); 
 DeleteFileW(lpszFile);

memset(sk,'\0',256);
  }while(FindNextFile(hFindFile,&findData));
 }

 char Foldername[256] = "\0";
 memset(Foldername,'\0',256);
 strcpy(Foldername,pRoad);
 nLen = strlen(Foldername) + 1;
 nwLen = MultiByteToWideChar(CP_ACP, 0, Foldername, nLen, NULL, 0);
 MultiByteToWideChar(CP_ACP, 0,Foldername, nLen, lpszFile, nwLen); //lpszFile:_T("\NandFlash\2\*.*")
CString ss;
ss = (CString)lpszFile;
FindClose(hFindFile);

 RemoveDirectory(lpszFile);

#7


lori227 的解决方法明显有问题 
这个语句就有问题的
char chFile[_MAX_DIR] = _T(""); 

#8


http://armlinux.uueasy.com/read-htm-tid-52.html

#9


该回复于2012-11-02 09:39:38被版主删除