关于遍历指定目录下所有子目录以及指定目录下的文件

时间:2022-12-24 12:39:46
请教关于遍历指定目录下所有子目录以及指定目录下的文件并可以删除指定的文件如
再Edit中输入pc.txt或*.ini,并指定目录就将遍历整个目录下所有子目录以及指定目录下的文件并删除所有的pc.txt或*.ini,很急!!!谢谢!!

9 个解决方案

#1


占个头坑。
递归嘛。

#2


函数A 参数B是目录
找B下面的目录。
如果找到了,
循环调用A,参数是找到的目录。
没有目录了。
找ini文件,
删除
返回。

#3


TSearchRec srTemp;
        if(FindFirst(sPath + "*.*", faAnyFile, srTemp) == 0)
        {
            do
            {
                if((srTemp.Name != ".") && (srTemp.Name != ".."))
                {}
                }

            } while(FindNext(srTemp) == 0);

            FindClose(srTemp);
        }

#4


同意石头的做法,这是遍历莫个目录下的文件,如果要遍历所有子目录需要对srTemp进行判断,如果是子目录就进入,用石头的方法再遍历。
do 
  { 
  switch(srTemp.Attr) 
    { 
    case faVolumeID: 
          break; 
    case faDirectory: 
          if(srTemp.Name==".")//根目录 
            break; 
          if(srTemp.Name=="..")//上级目录 
            break; 
          //其他目录 
          size+=GetFile(Path+"\\"+srTemp.Name); 
          break; 
    case faSysFile: 
          break; 
    case faHidden: 
          break; 
    default: 
          size++; 
          LV->Add(Path+"\\"+srTemp.Name);//LV即ListBox控件 
            
    } 

#5


旧贴有很多的,找一下吧,就是上面的方法,只用了FindFirst,FindNext2个函数

#6


参考一下吧
bool  Tzhu::deldir(char * dir_fullpath) // 删除指定的目录
{
char dir[260];
char filename[260];
int len = 0;
int ch = '\\';

strcpy(dir, dir_fullpath);

len = strlen(dir);

char *temp = strrchr(dir, ch);
if(len < 4 || temp == NULL) // 可能为磁盘根目录或者不是有效的目录路径
return false;
if(temp != NULL)
{
if((temp - dir + 1) != len) // 在目录后添加'\'
strcat(dir, "\\");
}

GetCurrentDirectory(260, filename);
strcat(filename, "\\");
if(strcmp(dir, filename) == 0) // 如果要删除的目录是当前目录
{
strcat(filename, "..");
SetCurrentDirectory(filename); // 改变当前目录
}

WIN32_FIND_DATA finddata;
HANDLE fFile;
bool flag;

strcpy(filename, dir);
strcat(filename, "*.*");
fFile=FindFirstFile(filename, &finddata);
flag=true;

if(fFile!=INVALID_HANDLE_VALUE) // 此目录不是空目录
{
BOOL bfind = true;
while(bfind)
{
if(finddata.cFileName[0] != '.')
{
strcpy(filename, dir);
strcat(filename, finddata.cFileName);
if(finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{ // 删除找到的子目录
strcat(filename, "\\");
flag = flag && deldir(filename);
}
else
{ // 删除找到的文件
SetFileAttributes(filename, FILE_ATTRIBUTE_NORMAL);
flag = flag && DeleteFile(filename);
}
}
bfind = FindNextFile(fFile, &finddata);
}
FindClose(fFile);
}
if(flag)
{
SetFileAttributes(dir_fullpath, FILE_ATTRIBUTE_NORMAL);
if(RemoveDirectory(dir_fullpath))
return true;
}
return false;
}

#7


这样已经解决,但是还不能实现通配符'?'的输入,有没有简单点的方法
void __fastcall TForm1::deldir(AnsiString strPath, int iAttributes, TSearchRec sr,AnsiString filename)

{
    AnsiString strFileName;
    AnsiString strDeleteName;
    char *temp;
    char *file;
    char str[50];
    char str1[50];
    char str2[50];
    char filetemp[50];
    char file1[50];
    char file2[50];
    int i;
    int j;
    //.c_str()
    //23.StrPas 将指定的字符串???AnsiString?型字符串?象, 
//函数原型如下: AnsiString __fastcall StrPas(const char * Str);

    if ( FindFirst( strPath + "\\*.*",iAttributes, sr ) == 0 )
    {
        do{
            if( ( sr.Attr & iAttributes ) == sr.Attr )
            {
                strFileName = strPath + "\\" + sr.Name;
                if( sr.Attr == faDirectory && sr.Name != "." && sr.Name != ".." )
                    deldir( strFileName, iAttributes, sr,filename);
                if( sr.Attr != faDirectory )
                {
                   i=0;
                   j=0;
                   memset(str,0x00,sizeof(str));
                   memset(str1,0x00,sizeof(str1));
                   memset(str2,0x00,sizeof(str2));
                   memset(filetemp,0x00,sizeof(filetemp));
                   memset(file1,0x00,sizeof(file1));
                   memset(file2,0x00,sizeof(file2));
                   temp=sr.Name.c_str();
                   file=filename.c_str();
                   strcpy(str,temp);
                   strcpy(filetemp,file);

                   while(str[i]!='.')
                   {
                      str1[i]=str[i];
                      i++;
                   }
                   i++;
                   while(str[i]!='\0')
                   {
                       str2[j]=str[i];
                       i++;
                       j++;
                   }
                   i=0;
                   j=0;

                   while(filetemp[i]!='.')
                   {
                      file1[i]=filetemp[i];
                      i++;
                   }
                   i++;
                   while(filetemp[j]!='\0')
                   {
                       file2[j]=filetemp[i];
                       i++;
                       j++;
                   }

                   if(strcmp(filetemp,"*.*")==0)
                    {

                       ListBox1->Items->Add( strFileName+"     delete ok!");
                       DeleteFile(strFileName);
                    }
                    else
                    {
                        if (strchr(filetemp,'*')!=NULL )
                        {
                                if(strchr(filetemp,'*')>strchr(filetemp,'.'))
                                {
                                   strDeleteName=strPath+"\\"+file1+"."+str2;
                                   ListBox1->Items->Add(strDeleteName+"     delete ok!");
                                   if(FileExists(strDeleteName)==true)
                                   {
                                      DeleteFile(strDeleteName);
                                   }

                                }
                                else
                                {
                                   strDeleteName=strPath+"\\"+str1+"."+file2;
                                   if(FileExists(strDeleteName)==true)
                                   {
                                   ListBox1->Items->Add(strDeleteName+"     delete ok!");
                                   DeleteFile(strDeleteName);
                                   }

                                }
                        }
                        else
                        {
                            strDeleteName=strPath+"\\"+filename;
                            if(FileExists(strDeleteName)==true)
                            {
                             ListBox1->Items->Add(strDeleteName+"     delete ok!");
                             DeleteFile(strDeleteName);
                             }

                        }


                    }
                }
            }
        }while( FindNext( sr ) == 0 );
        FindClose(sr);
    }


}


//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{

    ListBox1->Clear();    
    TSearchRec sr;
    int iAttributes = 0;
    iAttributes |= faReadOnly;
    iAttributes |= faHidden;
    iAttributes |= faSysFile;
    iAttributes |= faVolumeID;
    iAttributes |= faDirectory;
    iAttributes |= faArchive;
    iAttributes |= faAnyFile;
    deldir(Edit1->Text, iAttributes, sr,Edit2->Text);

}

#8


简单的办法不知道,你只好用AnsiString的SubString函数了,取出字字符串进行判断,也不是很难的,估计也就几行代码。

#9


String path1,filename;
//Application->OnIdle=Idle(*Sender);
  TSearchRec Src;
  int retval;
    jdemo=0;
   path1="c:\\demo\\*.*";
    retval=FindFirst(path1,faAnyFile,Src);
      while(retval==0)
       {
       filename=Src.Name;
       //  if((Src.Attr&&(faDirectory||faVolumeID))!=0)
         {
         if(filename!="."&&filename!="..")
          {
         demosong[jdemo]="c:\\demo\\"+filename;
         jdemo++;
            }
         }
     retval=FindNext(Src);
      }
    FindClose(Src);

#1


占个头坑。
递归嘛。

#2


函数A 参数B是目录
找B下面的目录。
如果找到了,
循环调用A,参数是找到的目录。
没有目录了。
找ini文件,
删除
返回。

#3


TSearchRec srTemp;
        if(FindFirst(sPath + "*.*", faAnyFile, srTemp) == 0)
        {
            do
            {
                if((srTemp.Name != ".") && (srTemp.Name != ".."))
                {}
                }

            } while(FindNext(srTemp) == 0);

            FindClose(srTemp);
        }

#4


同意石头的做法,这是遍历莫个目录下的文件,如果要遍历所有子目录需要对srTemp进行判断,如果是子目录就进入,用石头的方法再遍历。
do 
  { 
  switch(srTemp.Attr) 
    { 
    case faVolumeID: 
          break; 
    case faDirectory: 
          if(srTemp.Name==".")//根目录 
            break; 
          if(srTemp.Name=="..")//上级目录 
            break; 
          //其他目录 
          size+=GetFile(Path+"\\"+srTemp.Name); 
          break; 
    case faSysFile: 
          break; 
    case faHidden: 
          break; 
    default: 
          size++; 
          LV->Add(Path+"\\"+srTemp.Name);//LV即ListBox控件 
            
    } 

#5


旧贴有很多的,找一下吧,就是上面的方法,只用了FindFirst,FindNext2个函数

#6


参考一下吧
bool  Tzhu::deldir(char * dir_fullpath) // 删除指定的目录
{
char dir[260];
char filename[260];
int len = 0;
int ch = '\\';

strcpy(dir, dir_fullpath);

len = strlen(dir);

char *temp = strrchr(dir, ch);
if(len < 4 || temp == NULL) // 可能为磁盘根目录或者不是有效的目录路径
return false;
if(temp != NULL)
{
if((temp - dir + 1) != len) // 在目录后添加'\'
strcat(dir, "\\");
}

GetCurrentDirectory(260, filename);
strcat(filename, "\\");
if(strcmp(dir, filename) == 0) // 如果要删除的目录是当前目录
{
strcat(filename, "..");
SetCurrentDirectory(filename); // 改变当前目录
}

WIN32_FIND_DATA finddata;
HANDLE fFile;
bool flag;

strcpy(filename, dir);
strcat(filename, "*.*");
fFile=FindFirstFile(filename, &finddata);
flag=true;

if(fFile!=INVALID_HANDLE_VALUE) // 此目录不是空目录
{
BOOL bfind = true;
while(bfind)
{
if(finddata.cFileName[0] != '.')
{
strcpy(filename, dir);
strcat(filename, finddata.cFileName);
if(finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{ // 删除找到的子目录
strcat(filename, "\\");
flag = flag && deldir(filename);
}
else
{ // 删除找到的文件
SetFileAttributes(filename, FILE_ATTRIBUTE_NORMAL);
flag = flag && DeleteFile(filename);
}
}
bfind = FindNextFile(fFile, &finddata);
}
FindClose(fFile);
}
if(flag)
{
SetFileAttributes(dir_fullpath, FILE_ATTRIBUTE_NORMAL);
if(RemoveDirectory(dir_fullpath))
return true;
}
return false;
}

#7


这样已经解决,但是还不能实现通配符'?'的输入,有没有简单点的方法
void __fastcall TForm1::deldir(AnsiString strPath, int iAttributes, TSearchRec sr,AnsiString filename)

{
    AnsiString strFileName;
    AnsiString strDeleteName;
    char *temp;
    char *file;
    char str[50];
    char str1[50];
    char str2[50];
    char filetemp[50];
    char file1[50];
    char file2[50];
    int i;
    int j;
    //.c_str()
    //23.StrPas 将指定的字符串???AnsiString?型字符串?象, 
//函数原型如下: AnsiString __fastcall StrPas(const char * Str);

    if ( FindFirst( strPath + "\\*.*",iAttributes, sr ) == 0 )
    {
        do{
            if( ( sr.Attr & iAttributes ) == sr.Attr )
            {
                strFileName = strPath + "\\" + sr.Name;
                if( sr.Attr == faDirectory && sr.Name != "." && sr.Name != ".." )
                    deldir( strFileName, iAttributes, sr,filename);
                if( sr.Attr != faDirectory )
                {
                   i=0;
                   j=0;
                   memset(str,0x00,sizeof(str));
                   memset(str1,0x00,sizeof(str1));
                   memset(str2,0x00,sizeof(str2));
                   memset(filetemp,0x00,sizeof(filetemp));
                   memset(file1,0x00,sizeof(file1));
                   memset(file2,0x00,sizeof(file2));
                   temp=sr.Name.c_str();
                   file=filename.c_str();
                   strcpy(str,temp);
                   strcpy(filetemp,file);

                   while(str[i]!='.')
                   {
                      str1[i]=str[i];
                      i++;
                   }
                   i++;
                   while(str[i]!='\0')
                   {
                       str2[j]=str[i];
                       i++;
                       j++;
                   }
                   i=0;
                   j=0;

                   while(filetemp[i]!='.')
                   {
                      file1[i]=filetemp[i];
                      i++;
                   }
                   i++;
                   while(filetemp[j]!='\0')
                   {
                       file2[j]=filetemp[i];
                       i++;
                       j++;
                   }

                   if(strcmp(filetemp,"*.*")==0)
                    {

                       ListBox1->Items->Add( strFileName+"     delete ok!");
                       DeleteFile(strFileName);
                    }
                    else
                    {
                        if (strchr(filetemp,'*')!=NULL )
                        {
                                if(strchr(filetemp,'*')>strchr(filetemp,'.'))
                                {
                                   strDeleteName=strPath+"\\"+file1+"."+str2;
                                   ListBox1->Items->Add(strDeleteName+"     delete ok!");
                                   if(FileExists(strDeleteName)==true)
                                   {
                                      DeleteFile(strDeleteName);
                                   }

                                }
                                else
                                {
                                   strDeleteName=strPath+"\\"+str1+"."+file2;
                                   if(FileExists(strDeleteName)==true)
                                   {
                                   ListBox1->Items->Add(strDeleteName+"     delete ok!");
                                   DeleteFile(strDeleteName);
                                   }

                                }
                        }
                        else
                        {
                            strDeleteName=strPath+"\\"+filename;
                            if(FileExists(strDeleteName)==true)
                            {
                             ListBox1->Items->Add(strDeleteName+"     delete ok!");
                             DeleteFile(strDeleteName);
                             }

                        }


                    }
                }
            }
        }while( FindNext( sr ) == 0 );
        FindClose(sr);
    }


}


//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{

    ListBox1->Clear();    
    TSearchRec sr;
    int iAttributes = 0;
    iAttributes |= faReadOnly;
    iAttributes |= faHidden;
    iAttributes |= faSysFile;
    iAttributes |= faVolumeID;
    iAttributes |= faDirectory;
    iAttributes |= faArchive;
    iAttributes |= faAnyFile;
    deldir(Edit1->Text, iAttributes, sr,Edit2->Text);

}

#8


简单的办法不知道,你只好用AnsiString的SubString函数了,取出字字符串进行判断,也不是很难的,估计也就几行代码。

#9


String path1,filename;
//Application->OnIdle=Idle(*Sender);
  TSearchRec Src;
  int retval;
    jdemo=0;
   path1="c:\\demo\\*.*";
    retval=FindFirst(path1,faAnyFile,Src);
      while(retval==0)
       {
       filename=Src.Name;
       //  if((Src.Attr&&(faDirectory||faVolumeID))!=0)
         {
         if(filename!="."&&filename!="..")
          {
         demosong[jdemo]="c:\\demo\\"+filename;
         jdemo++;
            }
         }
     retval=FindNext(Src);
      }
    FindClose(Src);