انتقل إلى المحتوى الرئيسي

كيف تسرد جميع الملفات في المجلد والمجلدات الفرعية في ورقة عمل؟

هل سبق لك أن حاولت سرد جميع أسماء الملفات من مجلد في ورقة عمل بما في ذلك الملفات الموجودة داخل مجلداته الفرعية؟ في الواقع ، لا توجد طريقة مباشرة بالنسبة لنا لسرد أسماء الملفات من مجلد ومجلده الفرعي في Excel ، ومع ذلك ، سأقدم اليوم بعض الحيل السريعة لحل هذه المهمة.

أدرج جميع أسماء الملفات في المجلد والمجلد الفرعي برمز VBA

قم بإدراج جميع أسماء الملفات في مجلد ومجلد فرعي بسرعة وسهولة باستخدام Kutools for Excel


عادةً ، لا يحتوي Excel على ميزة مضمنة للتعامل مع هذه المهمة ، ولكن يمكنك تطبيق رمز VBA التالي لإكمال هذه المشكلة.

1. قم بتنشيط ورقة عمل جديدة تسرد أسماء الملفات.

2. اضغط باستمرار على ALT + F11 في Excel ، ويفتح ملف ميكروسوفت فيسوال باسيك للتطبيقات نافذة.

3. انقر إدراج > وحدة، والصق الكود التالي في ملف نافذة الوحدة.

رمز فبا: سرد كافة أسماء الملفات في المجلد والمجلد الفرعي

Sub MainList()
'Updateby Extendoffice
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show <> -1 Then Exit Sub
xDir = folder.SelectedItems(1)
Call ListFilesInFolder(xDir, True)
End Sub
Sub ListFilesInFolder(ByVal xFolderName As String, ByVal xIsSubfolders As Boolean)
Dim xFileSystemObject As Object
Dim xFolder As Object
Dim xSubFolder As Object
Dim xFile As Object
Dim rowIndex As Long
Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFileSystemObject.GetFolder(xFolderName)
rowIndex = Application.ActiveSheet.Range("A65536").End(xlUp).Row + 1
For Each xFile In xFolder.Files
  Application.ActiveSheet.Cells(rowIndex, 1).Formula = xFile.Name
  rowIndex = rowIndex + 1
Next xFile
If xIsSubfolders Then
  For Each xSubFolder In xFolder.SubFolders
    ListFilesInFolder xSubFolder.Path, True
  Next xSubFolder
End If
Set xFile = Nothing
Set xFolder = Nothing
Set xFileSystemObject = Nothing
End Sub
Function GetFileOwner(ByVal xPath As String, ByVal xName As String)
Dim xFolder As Object
Dim xFolderItem As Object
Dim xShell As Object
xName = StrConv(xName, vbUnicode)
xPath = StrConv(xPath, vbUnicode)
Set xShell = CreateObject("Shell.Application")
Set xFolder = xShell.Namespace(StrConv(xPath, vbFromUnicode))
If Not xFolder Is Nothing Then
  Set xFolderItem = xFolder.ParseName(StrConv(xName, vbFromUnicode))
End If
If Not xFolderItem Is Nothing Then
  GetFileOwner = xFolder.GetDetailsOf(xFolderItem, 8)
Else
  GetFileOwner = ""
End If
Set xShell = Nothing
Set xFolder = Nothing
Set xFolderItem = Nothing
End Function

4. بعد لصق الرمز في الوحدة النمطية ، اضغط على F5 مفتاح لتشغيل هذا الرمز ، و وحدات الماكرو ينبثق مربع الحوار ، حدد ملف القائمة الرئيسية اسم الماكرو ، ثم انقر فوق موافق يجري زر ، انظر لقطة الشاشة:

doc list في المجلد الفرعي 1

5. وفي تصفح نافذة ، يرجى تحديد المجلد الذي تريد سرد جميع أسماء الملفات بما في ذلك المجلدات الفرعية ، انظر الصورة:

doc list في المجلد الفرعي 2

6. بعد تحديد المجلد ، انقر فوق OK الزر ، وجميع أسماء الملفات الموجودة في المجلد ومجلداته الفرعية مدرجة في ورقة العمل الحالية من الخلية A2 ، انظر لقطات الشاشة:

doc list في المجلد الفرعي 3
1
doc list في المجلد الفرعي 4

باستخدام الكود أعلاه ، يمكنك فقط سرد أسماء الملفات ، وفي بعض الأحيان ، تحتاج إلى سرد سمات أخرى ، مثل حجم الملف ونوع الملف ووقت الإنشاء والمجلد الذي يحتوي على وهكذا. كوتولس ل إكسيل يحتوي على وظيفة مفيدة - قائمة اسم الملف، باستخدام هذه الميزة ، يمكنك بسرعة سرد جميع أسماء الملفات أو أنواع معينة منها في مجلد ومجلداته الفرعية.

كوتولس ل إكسيل : مع أكثر من 300 وظيفة إضافية مفيدة في Excel ، يمكنك تجربتها مجانًا دون قيود في أيام 30.

بعد تثبيت كوتولس ل إكسيل، برجاء اتباع الخطوات التالية:

1. انقر مشروع > استيراد و تصدير > قائمة اسم الملف، انظر لقطة الشاشة:

2. في قائمة اسم الملف في مربع الحوار ، قم بالعمليات التالية:

A: انقر doc list في المجلد الفرعي 7زر لاختيار المجلد الذي تريده لسرد أسماء الملفات ؛

B: حدد نوع الملف الذي تريد سرده من ملف نوع الملفات قسم.

C: حدد وحدة حجم ملف واحدة تريد عرضها من ملف وحدة حجم الملف والقسم الخاص به.

ملاحظات: لسرد أسماء الملفات من المجلد الفرعي ، يرجى التحقق تضمين الملفات في الدلائل الفرعية، يمكنك أيضًا التحقق من تشمل الملفات والمجلدات المخفية ما تحتاج إليه. إذا قمت بالتدقيق إنشاء ارتباطات تشعبية الخيار ، فإنه سيتم إنشاء ارتباطات تشعبية لكل أسماء الملفات والمجلدات.

تنزيل وتجربة مجانية الآن!

3. ثم اضغط OK الزر ، تم عرض كافة الملفات الموجودة في المجلد المحدد ومجلداته الفرعية بالسمات التالية في ورقة عمل جديدة. انظر لقطة الشاشة:

doc list في المجلد الفرعي 8

انقر لمعرفة المزيد من التفاصيل حول هذه الأداة المساعدة لقائمة اسم الملف.

قم بتنزيل Kutools for Excel والإصدار التجريبي المجاني الآن!


كوتولس ل إكسيل: مع أكثر من 300 وظيفة إضافية مفيدة في Excel ، يمكنك تجربتها مجانًا دون قيود خلال 30 يومًا. تنزيل وتجربة مجانية الآن!

أفضل أدوات إنتاجية المكتب

🤖 مساعد Kutools AI: إحداث ثورة في تحليل البيانات على أساس: التنفيذ الذكي   |  إنشاء التعليمات البرمجية  |  إنشاء صيغ مخصصة  |  تحليل البيانات وإنشاء الرسوم البيانية  |  استدعاء وظائف Kutools...
الميزات الشعبية: البحث عن التكرارات أو تمييزها أو تحديدها   |  حذف الصفوف الفارغة   |  دمج الأعمدة أو الخلايا دون فقدان البيانات   |   جولة بدون صيغة 
سوبر بحث: معايير متعددة VLookup    VLookup ذات القيمة المتعددة  |   VLookup عبر أوراق متعددة   |   بحث غامض ....
قائمة منسدلة متقدمة: إنشاء القائمة المنسدلة بسرعة   |  القائمة المنسدلة التابعة   |  قائمة منسدلة متعددة التحديد ....
مدير العمود: إضافة عدد محدد من الأعمدة  |  نقل الأعمدة  |  تبديل حالة رؤية الأعمدة المخفية  |  مقارنة النطاقات والأعمدة 
الميزات المميزة: التركيز على الشبكة   |  عرض تصميم   |   شريط الفورمولا الكبير    مدير المصنفات والأوراق   |  مكتبة الموارد (النص السيارات)   |  منتقي التاريخ   |  اجمع أوراق العمل   |  تشفير/فك تشفير الخلايا    إرسال رسائل البريد الإلكتروني عن طريق القائمة   |  سوبر تصفية   |   مرشح خاص (تصفية غامق / مائل / يتوسطه خط ...) ...
أفضل 15 مجموعة أدوات12 نص الأدوات (إضافة نص, إزالة الأحرف، ...)   |   +50 رسم الأنواع (مخطط جانت، ...)   |   40+ عملي الصيغ (احسب العمر على أساس تاريخ الميلاد، ...)   |   19 إدخال الأدوات (أدخل رمز الاستجابة السريعة, إدراج صورة من المسار، ...)   |   12 تحويل الأدوات (أرقام إلى كلمات, نتيجة تحويل عملة، ...)   |   7 دمج وتقسيم الأدوات (الجمع بين الصفوف المتقدمة, تقسيم الخلايا، ...)   |   ... و اكثر

عزز مهاراتك في Excel باستخدام Kutools for Excel، واختبر كفاءة لم يسبق لها مثيل. يقدم Kutools for Excel أكثر من 300 ميزة متقدمة لتعزيز الإنتاجية وتوفير الوقت.  انقر هنا للحصول على الميزة التي تحتاجها أكثر...

الوصف


يجلب Office Tab الواجهة المبوبة إلى Office ، ويجعل عملك أسهل بكثير

  • تمكين التحرير والقراءة المبوبة في Word و Excel و PowerPointوالناشر والوصول و Visio والمشروع.
  • فتح وإنشاء مستندات متعددة في علامات تبويب جديدة من نفس النافذة ، بدلاً من النوافذ الجديدة.
  • يزيد من إنتاجيتك بنسبة 50٪ ، ويقلل مئات النقرات بالماوس كل يوم!
Comments (20)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Nice work, exactly what I was trying to create. But this is 1000% better.
This comment was minimized by the moderator on the site
Sorry... I gave you the wrong code (below), here is the code I modified..
Code:
Sub MainList()
On Error Resume Next
Dim xFileSystemObject As Object
Dim xFolder As Object
Dim xSubFolder As Object
Dim xFile As Object
Dim rowIndex As Long
Dim answer As Variant
answer = False
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select a Folder"
.AllowMultiSelect = False
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show = -1 Then
xDir = folder.SelectedItems(1)
Else
Exit Sub
End If
End With

Call ListFilesInFolder(xDir, True)
End Sub

Sub ListFilesInFolder(ByVal xFolderName As String, ByVal xIsSubfolders As Boolean)
Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFileSystemObject.GetFolder(xFolderName)
rowIndex = Application.ActiveSheet.Range("A65536").End(xlUp).Row + 1

' Add a space then the Folder Name to the Worksheet
rowIndex = rowIndex + 1
With Application.ActiveSheet.Cells(rowIndex, 1)
.Value = xFolder.Name
.Font.Size = 12
.Font.FontStyle = "Bold Italic"
End With
rowIndex = rowIndex + 1

For Each xFile In xFolder.Files
Application.ActiveSheet.Cells(rowIndex, 1).Formula = xFile.Name
rowIndex = rowIndex + 1
Next xFile
If xIsSubfolders Then
For Each xSubFolder In xFolder.SubFolders
ListFilesInFolder xSubFolder.Path, True
Next xSubFolder
rowIndex = rowIndex + 1
End If

Set xFile = Nothing
Set xFolder = Nothing
Set xFileSystemObject = Nothing

Call MainList
End Sub


This comment was minimized by the moderator on the site
I have modified your code to make it recycle and re-run the folder dialog continuously until you press Cancel.Unfornatually it generates some errors.1. If subfolders become involved in a folder selected then the next folder selected becomes out of numerical order.2. If subfolders become involved in a folder selected the Cancel button has to be repeated and repeated depending on how many folders you have added.
Code:
Sub MainList()
'Updateby Extendoffice
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show <> -1 Then Exit Sub
xDir = folder.SelectedItems(1)
Call ListFilesInFolder(xDir, True)
End SubSub ListFilesInFolder(ByVal xFolderName As String, ByVal xIsSubfolders As Boolean)
Dim xFileSystemObject As Object
Dim xFolder As Object
Dim xSubFolder As Object
Dim xFile As Object
Dim rowIndex As Long
Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFileSystemObject.GetFolder(xFolderName)
rowIndex = Application.ActiveSheet.Range("A65536").End(xlUp).Row + 1
For Each xFile In xFolder.Files
Application.ActiveSheet.Cells(rowIndex, 1).Formula = xFile.Name
rowIndex = rowIndex + 1
Next xFile
If xIsSubfolders Then
For Each xSubFolder In xFolder.SubFolders
ListFilesInFolder xSubFolder.Path, True
Next xSubFolder
End If
Set xFile = Nothing
Set xFolder = Nothing
Set xFileSystemObject = Nothing
End Sub

any ideas ?
This comment was minimized by the moderator on the site
Does it work on MAC too?
This comment was minimized by the moderator on the site
Hey, so if I have to just extract an extension from the whole list, where should I make changes?
This comment was minimized by the moderator on the site
Really new to VBA. how do i use the above code but have the file path built into it so i don't have to search for it every time?
This comment was minimized by the moderator on the site
My VBA is saying that the variable folder is not defined. Anyone know why this is?
This comment was minimized by the moderator on the site
What was the purpose of the parameter ByVal xIsSubfolders As Boolean?
This comment was minimized by the moderator on the site
Extremamente elegante este código!
This comment was minimized by the moderator on the site
Sub "GetFileOwner()" in code above is not used.
This comment was minimized by the moderator on the site
Hello, jumpjack,
The above code works well in my Excel, which Excel version do you use?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations