كيف يمكن معرفة جميع اسماء الملفات في مجلد معين و من ثم تخزين اسمائها في مصفوفة
معرفة اسماء الملفات
إبحث يا أخي في مكتبة الأكواد حتما ستجد ما يعينك.
procedure TForm1.Button1Click(Sender: TObject);
Var Path : String;
Info : TSearchRec;
begin
ListBox1.Items.Clear;
ListBox2.Items.Clear;
{ For be sur that back slash is insert in the end of the path }
Path := IncludeTrailingPathDelimiter(Edit1.Text);
{ Search for the first entry in the derectory}
If FindFirst(Path+'*.*',faAnyFile,Info)=0 Then
Begin
Repeat
{ Files as listed in ListBox1 }
{ directories are listed in ListBox2 }
If Not((Info.Attr And faDirectory)=0)
Then ListBox2.Items.Add(Info.FindData.cFileName)
Else ListBox1.Items.Add(Info.FindData.cFileName)
{ Search for the nextand so }
Until FindNext(Info)<>0;
{ in the case one or more entries are find }
{ call FindClose to realese resources of the search }
FindClose(Info);
End;
end;أرجو أن يكون هذا ما تبحث عنه. أستعمال Findfirst and findnext
سأتغيب عن المنتدى وستكون لي زيارات خفيفة بين الحين و الحين
يا صاحب الهم إن الهم منفرج.....................أبشر بخير فإن الفارج الله.
اليأس يقطع أحيانا بصاحبه..........................لا تيأس فإن الكافي الله.
الله يحدث بعد العسر ميسرة.........................لا تفزعن فإن الصانع الله.
إذا بليت فثق بالله وارض به...................فإن الذي يكشف البلوى هو الله.
و الله ما لك غير الله من أحد.......................فحسبك الله في كل لك الله
أخوكم مراد
في حالة ما إذا أردت إحصاء عدد الملفات في مجلد بإخذ بعين الإعتبار الملفات المضمنة داخل المجلدات subdirectories:
// Function: Count files in a path including files in subdirectories // return: the number of files Function FilesCount(Path:String):Integer; Var sInfo:TSearchRec; Begin Path:=IncludeTrailingPathDelimiter(Path); // init the count with zero Result:=0; // Find first entry in the directory If FindFirst(Path+'*.*',faAnyFile,sInfo)=0 Then Begin Repeat // it must ignore the: . and .. whose are always at the beginin of the directories // else the function loop for ever in recursion If (sInfo.Name<>'.') And (sInfo.Name<>'..') Then Begin If (sInfo.Attr And faDirectory)<>0 // if is a subdirectory : call the same function = recursion Then Result:= Result + FilesCount(Path + sInfo.FindData.cFileName) // else increment the value of the count of files Else Inc(Result); End; // search next Until FindNext(sInfo)<>0; // free resource sInfo FindClose(sInfo); End; End;
سأتغيب عن المنتدى وستكون لي زيارات خفيفة بين الحين و الحين
يا صاحب الهم إن الهم منفرج.....................أبشر بخير فإن الفارج الله.
اليأس يقطع أحيانا بصاحبه..........................لا تيأس فإن الكافي الله.
الله يحدث بعد العسر ميسرة.........................لا تفزعن فإن الصانع الله.
إذا بليت فثق بالله وارض به...................فإن الذي يكشف البلوى هو الله.
و الله ما لك غير الله من أحد.......................فحسبك الله في كل لك الله
أخوكم مراد
بارك الله فيك يا اخي مراد لقد افدتني كثيرا :)
function TForm1.ListFiles(Dir: string): TStringList;
var
Searchrec:TWin32FindData;
FindHandle:THandle;
DirList,resultList:TStringList;
FileName:String;
I:integer;
begin
if dir[length(dir)]<>'\' then dir:=dir+'\';
DirList:=TStringList.Create;
resultList:=TStringList.Create;
FindHandle:=FindFirstFile(pchar(dir+'*.*'),Searchrec);
try
if FindHandle<>INVALID_HANDLE_VALUE then
repeat
FileName:=Searchrec.cFileName;
if ((Searchrec.dwFileAttributes and Windows.FILE_ATTRIBUTE_DIRECTORY)<>0)then
begin
if FileName[1] <> '.' then begin
DirList.Add(dir+FileName);
resultList.Add(dir+filename);end; end
else begin
resultList.Add(dir+filename);
end;
until FindNextFile(FindHandle,Searchrec) = false;
finally windows.FindClose(FindHandle);
end;
for I:=0 to DirList.Count-1 do
begin
ListFiles(DirList);
end;
DirList.free;
Result:=resultList;
end;
//-------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
myList:TStringList;
begin
myList:=ListFiles('D:\New Folder (4)');
ListBox1.Items.Assign(myList);
end;لا اريد كود جاهز ولكن الهدف هو معرفة اين الخطا الذى وقعت فيه لكى لا يكوم البرنامج بالبحث عن الملفات الموجودة فى المجلدات المتضمنة وشكرا
(ملحوظة لقد بحثت فى المنتدى ولم اجد الخطأ)
{ربى زدنى علما}
بل هناك خطأ . المثال لن يظهر إلا معلومات أول مجلد فقط ..
جرب هذا وحاول تعرف الفرق
function TForm1.ListFiles(Dir: string): TStringList;
var
Searchrec:TWin32FindData;
FindHandle:THandle;
{DirList,}resultList:TStringList;
FileName:String;
I:integer;
begin
if dir[length(dir)]<>'\' then
dir:=dir+'\';
// DirList:=TStringList.Create;
resultList:=TStringList.Create;
FindHandle:=FindFirstFile(pchar(dir+'*.*'),Searchrec);
try
if FindHandle<>INVALID_HANDLE_VALUE then
repeat
FileName:=Searchrec.cFileName;
if ((Searchrec.dwFileAttributes and Windows.FILE_ATTRIBUTE_DIRECTORY)<>0)then
begin
if FileName[1] <> '.' then
begin
//DirList.Add(dir+FileName);
resultList.Add(dir+filename);
resultList.AddStrings(ListFiles(dir+FileName));
end;
end
else
begin
resultList.Add(dir+filename);
end;
until FindNextFile(FindHandle,Searchrec) = false;
finally
windows.FindClose(FindHandle);
end;
{for I:=0 to DirList.Count-1 do
begin
ListFiles(DirList);
end;}
//DirList.free;
Result:=resultList;
end;نسيت ذكر أنه بالامكان الاستغناء عن resultList وإستبدالها بـ result ...
طريقتك أيضا ممكنه بعمل التعديل الأتي
for I:=0 to DirList.Count-1 do begin resultList.AddStrings(ListFiles(DirList)); end;
أو الآتي إن كنت إستغنيت عن resultList
for I:=0 to DirList.Count-1 do begin result.AddStrings(ListFiles(DirList)); end;
طبعاً هذا باستعمال أول كود وضعته أنت
"First they ignore you, then they laugh at you, then they attack you, then you win"
===
اقتباسحُكَّامُـنَا إِنْ تَصَـدّوا لِلْحِمَــى اقْتَحَمُـوا ***** وَإِنْ تَصَدَّى لَـهُ المُسْتَعْمِـرُ انْسَحَبُـوا
هُمْ يَفْرشـُونَ لِجَيْـشِ الغَــزْوِ أَعْيُنَـهُـمْ ***** وَيَدَّعُــونَ وُثُـوبَـاً قَـبْـلَ أَنْ يَثِـبُــوا
الحَاكِمُـونَ و«وَاشُنْـطُـنْ» حُكُومَتُـهُـمْ ***** وَاللامِعُــونَ وَمَـا شَعَّـوا وَلا غَرَبُــوا
القَاتِلُـــونَ نُبُــوغَ الشَّـعْــبِ تَرْضِـيَـــةً ***** لِلْمُعْتَدِيــنَ وَمَـا أَجْـدَتْـهُـمُ الـقُــرَبُ
لَهُمْ شُمُـوخُ «المُثَنَّـى» ظَاهِـرَاً وَلَهُـمْ ***** هَـوَىً إِلَـى بَابَـك الخَرْمِـيّ يُنْتَسَـبُ
البردوني "أبو تمام وعروبة اليوم" 1971
===
اقتباسعبيد الهوى يحكمون البـلاد ***** ويحكمهــم كلّهـــم درهــــــمو تقتـادهـم شهـوة لا تنــام ***** وهـــم فـي جـهالتهــم نــــوّم
ففــي كـــلّ ناحيــة ظـالـــم ***** غبــــــيّ يسـلـّطــــه أظلـــم
أيا من شبعتم على جوعــنا ***** وجـــوع بنينـا ألـم تتخمـــوا ؟
ألم تفهموا غضبة الكادحين ***** على الظلـم ؟ لا بدّ أن تفهموا
البردوني "نحن و الحاكمون "
:) كنت اظنه يريد محتوى المجلد فقط من ملفات
لكنه يريد محتوى المجلج و محتوى المجلدات التي به ايضا...
الكود متكرر في المنتدى, كنت كتبت كود بالإعتماد على routine لديلفي
سأتغيب عن المنتدى وستكون لي زيارات خفيفة بين الحين و الحين
يا صاحب الهم إن الهم منفرج.....................أبشر بخير فإن الفارج الله.
اليأس يقطع أحيانا بصاحبه..........................لا تيأس فإن الكافي الله.
الله يحدث بعد العسر ميسرة.........................لا تفزعن فإن الصانع الله.
إذا بليت فثق بالله وارض به...................فإن الذي يكشف البلوى هو الله.
و الله ما لك غير الله من أحد.......................فحسبك الله في كل لك الله
أخوكم مراد
محمدعبد العزيز شكرا لسعة صدرك
cipher
شكرا لك على التوضيح وسوف اجرب التعديلات وارد عليك
استاذى ikossan اسف ولاول مرة اعترض على رد لك لا يهمنى الكود مكرر او لا ما يهمنى الخطأ الذى وقعت فيه اسف جدا
شكرا لكم جميعا
for I:=0 to DirList.Count-1 do begin resultList.AddStrings(ListFiles(DirList)); end;
هذا التعديل حل المشكلة تماما شكرا لك اخى cipher
:rolleyes:
{ربى زدنى علما}
هذا الموضوع مغلق.

