列举文件所有文件
如下代码不能够列举子文件夹内的文件,能够说下问题所在嘛?
void __fastcall BrowDir(AnsiString PathName,TStringGrid * Num)
{
int col=1;
TSearchRec sr;
if (FindFirst(PathName+ "*.* ", faAnyFile, sr) == 0) //列举所有的目录
{
do
{
if((sr.Attr & faDirectory) && sr.Name!= ". " && sr.Name!= ".. ") //判断是否是目录,并排除目录“.”和“..”
{
BrowDir(PathName+sr.Name+ "\\ ", Num);//调用函数本身,进入子目录
}
} while (FindNext(sr) == 0);
FindClose(sr);
}
if (FindFirst(PathName+ "*.* ", faAnyFile, sr) == 0) //列举所有文件
{
do
{
if(!(sr.Attr & faDirectory))
Num-> Cells[6][++col]=(PathName+sr.Name);
} while (FindNext(sr) == 0);
FindClose(sr);
}
}
//---------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
TSearchRec sr;
SYSTEMTIME SystemTime;
FILETIME FileTime;
int col=1;
StringGrid1-> RowCount=35;
AnsiString strTemp=ExtractFileDir(Application-> ExeName)+ "\\ "; //得到当前运行程序的路径
Form1-> Caption=strTemp;
BrowDir(strTemp,StringGrid1);
}
//---------------------------------------
[解决办法]
int col=1; 改成static int col=1;
[解决办法]
我这是加载到TreeView1中
void __fastcall TForm1::BrowDir(TTreeNodes * Nodes,AnsiString PathName,TTreeNode * Num)
{
TSearchRec sr;
TTreeNode* Layel;
//列举所有的目录
if (FindFirst(PathName+ "*.* ", faAnyFile, sr) == 0)
{
do
{
//判断是否是目录,并排除目录“.”和“..”
if((sr.Attr & faDirectory) && sr.Name!= ". " && sr.Name!= ".. ")
{
//增加新节点
Layel=Nodes-> AddChild(Num, sr.Name);
// Layel=Nodes-> AddChild(Num, "目录: " + sr.Name);
//调用函数本身,进入子目录
BrowDir(Nodes,PathName+sr.Name+ "\\ ",Layel);
}
else
{
if(sr.Name!= ". " && sr.Name!= ".. ")
{
Nodes-> AddChild(Num, sr.Name);
}
}
} while (FindNext(sr) == 0);
FindClose(sr);
}
//列举所有文件
/*
if (FindFirst(PathName+ "*.* ", faAnyFile, sr) == 0)
{
do
{
if(!(sr.Attr & faDirectory))
Nodes-> AddChild(Num, sr.Name);
// Nodes-> AddChild(Num, "文件: " + sr.Name);
} while (FindNext(sr) == 0);
FindClose(sr);
}
*/
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//设置光标为漏斗
Screen-> Cursor=crHourGlass;
//激活Animate
Animate1-> Active=true;
AnsiString Path=Edit1-> Text;
//如果Path最后一个字符不是“\”就在后面加上“\”
if(Path.SubString(Path.Length(),1)!= "\\ ")
Path+= "\\ ";
BrowDir(TreeView1-> Items,Path,TreeView1-> Items-> Add(NULL,Path));
//设置光标为正常状态
Screen-> Cursor=crDefault;
//关闭Animate
Animate1-> Active=false;
}
//---------------------------------------