对数字文件名批量导入排序
//----------------------------------------------------------------------------
int __fastcall CrnSortList(TStringList *lst, int Index1, int Index2)
{
String sz1=ExtractFileName(lst->Strings[Index1]).SubString(1,ExtractFileName(lst->Strings[Index1]).Length()-4);
String sz2= ExtractFileName(lst->Strings[Index2]).SubString(1,ExtractFileName(lst->Strings[Index2]).Length()-4);
if( Form5->isNum(sz1) && Form5->isNum(sz2)) //如果是数字
{
if (sz1.ToInt()<sz2.ToInt()) // 楼主要求:张三<李四
{
return -1;
}
else if (sz1.ToInt() >sz2.ToInt()) // 楼主要求:张三<李四
return 1;
else
return 0;
}
else //如果是字符比对
{
if (sz1<sz2) // 楼主要求:张三<李四
{
return -1;
}
else if (sz1 >sz2) // 楼主要求:张三<李四
return 1;
else
return 0;
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm5::BitBtn2Click(TObject *Sender)
{
AnsiString Dir="";
//++++++++++++++++++++++++++++++++++++++++++++
//计算时间
clock_t start,end,dtStart;
start=clock();
//
RzProgressBar1->Percent=0;
//++++++++++++++++++++++++++++++++++++++++++++
//Dir里保存着所选的文件夹绝对路径
//为空退出
//Dir= RzSelectFolderDialog1->SelectedPathName;
//判定是否批量处理
// if(!YNpl)
// {
if(RzSelectFolderDialog1->Execute())
{
Dir =RzSelectFolderDialog1->Directory;
//Form5->RzMaskEdit1->EditText=RightStr(Dir,3);
//RzMaskEdit1->EditText= RightStr(Dir,3);
}
// else
// return;
// }
// else
// Dir=PLpath;
//-----------------------
if (Dir.Length()==0)
return;
int i,num,Lnum;
Lnum=ListView1->Items->Count;
TListItem *pItem=0;
TSearchRec sr;
//int iAttributes=0x0000003F;
//识别是否彩果
if(isNum(System::Strutils::RightStr(Dir,3)))
{
//Edit3->Text=System::Strutils::RightStr(Dir,3);
}
if (FindFirst(Dir+"\*.txt",faAnyFile,sr) == 0)
{ ListView1->Items->BeginUpdate();
TStringList *Flist =new TStringList;
Flist->Clear();
do
{
Flist->Append(Dir+"\"+sr.Name);
//++++++++++++++++++++++++++++++++++++++++++++
//if(Lnum<100)
//RzProgressBar1->Percent=Lnum;
//Application->ProcessMessages();
//++++++++++++++++++++++++++++++++++++++++++++
} while(FindNext(sr) == 0);
FindClose(sr);
//
//Flist->Sorted=true;
Flist->CustomSort(CrnSortList); // 进行排序
for(int i=0;i<Flist->Count;i++)
{
pItem=ListView1->Items->Add();
pItem->Caption=Lnum+1;
pItem->SubItems->Add(ExtractFileName(Flist->Strings[i]));
pItem->SubItems->Add("***"); //注数
pItem->SubItems->Add(Flist->Strings[i]); //注数
// pItem->SubItems->Add("*");
Lnum++;
}
ListView1->Items->EndUpdate();
}
Lnum=ListView1->Items->Count;
//PageControl1->ActivePageIndex=0;
// +++++++++++++++++++++++++++++++++++++++++++++++
//操作完成计时
end=clock();
dtStart=end-start;
float dSec=float(dtStart)/1000;
RzStatusPane1->Caption="预导入大底:"+String(Lnum)+" 个";
ListView1->Columns->Items[0]->Caption="id ["+AnsiString(Lnum)+"]";
RzStatusPane2->Caption =
"耗时:"+FloatToStrF(dSec,ffFixed,4,3)+" 秒 ";
RzProgressBar1->Percent=100;
}
//----------------------------------------------------------------------------
//判断是否为数字
bool TForm5::isNum(String str)
{
try{
float f; //StrToInt(str);
if(TryStrToFloat(str,f)==true)
return true;
else
return false;
}
catch(Exception &exception)