Soru Folder içindeki txt dosya listesini almak için gerekli kodu çalıştıramadım

Katılım
29 Aralık 2004
Mesajlar
82
Altın Üyelik Bitiş Tarihi
20-11-2019
Aşağıdaki basit bir kodu çalıştıramadım. Amacım belirli bir folder' daki dosyaların listesini çıkarmaktır. Anlayan varsa yardımcı olabilir misiniz ?

Not : Excel 2013' de çalışıyorum.

Option Explicit

Type FoundFileInfo
sPath As String
sName As String
End Type
------------------------------------------------------------------------------------------------------------------------------
Function FindFiles(ByVal sPath As String, ByRef recFoundFiles() As FoundFileInfo, ByRef iFilesFound As Integer, _
Optional ByVal sFileSpec As String = "*.*", Optional ByVal blIncludeSubFolders As Boolean = False) As Boolean
End Function
-----------------------------------------------------------------------------------------------------------------------------
Public Sub Ara()
Dim iFilesNum As Integer
Dim iCount As Integer
Dim recMyFiles() As FoundFileInfo
Dim blFilesFound As Boolean

blFilesFound = FindFiles("C:\Users\XXXXX\Desktop\New Folder\", recMyFiles, iFilesNum, "*.txt?", True)
If blFilesFound Then
For iCount = 1 To iFilesNum
With recMyFiles(iCount)
MsgBox "Path:" & vbTab & .sPath & vbNewLine & "Name:" & vbTab & .sName, vbInformation, "Found Files"
End With
Next
Else
MsgBox "No file(s) found matching the specified file spec.", vbInformation, "File(s) not Found"
End If

End Sub
 

Zeki Gürsoy

Uzman
Uzman
Katılım
31 Aralık 2005
Mesajlar
4,236
Excel Vers. ve Dili
Office 2019 (64 bit) - Türkçe
Fonksiyonun tam halini buldum:
https://stackoverflow.com/questions/47772156/vba-excel-search-in-folders-and-subsolders-specific-extension-and-write-in-exc

"FindFiles" fonksiyonunu aşağıdaki ile değiştirin.

PHP:
Function FindFiles(ByVal sPath As String, _
    ByRef recFoundFiles() As FoundFileInfo, _
    ByRef iFilesFound As Integer, _
    Optional ByVal sFileSpec As String, _
    Optional ByVal blIncludeSubFolders As Boolean = True) As Boolean

    Dim iCount As Integer           '* Multipurpose counter
    Dim sFileName As String         '* Found file name
    '*
    '* FileSystem objects
    Dim oFileSystem As Object, _
        oParentFolder As Object, _
        oFolder As Object, _
        oFile As Object

    Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set oParentFolder = oFileSystem.GetFolder(sPath)
    If oParentFolder Is Nothing Then
        FindFiles = False
        On Error GoTo 0
        Set oParentFolder = Nothing
        Set oFileSystem = Nothing
        Exit Function
    End If
    sPath = IIf(Right(sPath, 1) = "\", sPath, sPath & "\")
    '*
    '* Find files
    sFileName = Dir(sPath & sFileSpec, vbNormal)
    If sFileName <> "" Then
        For Each oFile In oParentFolder.Files
            If LCase(oFile.Name) Like LCase(sFileSpec) Then
                iCount = UBound(recFoundFiles)
                iCount = iCount + 1
                ReDim Preserve recFoundFiles(1 To iCount)
                With recFoundFiles(iCount)
                    .sPath = sPath
                    .sName = oFile.Name
                End With
            End If
        Next oFile
        Set oFile = Nothing         '* Although it is nothing
    End If
    If blIncludeSubFolders Then
        '*
        '* Select next sub-forbers
        For Each oFolder In oParentFolder.SubFolders
            FindFiles oFolder.Path, recFoundFiles, iFilesFound, sFileSpec, blIncludeSubFolders
        Next
    End If
    FindFiles = UBound(recFoundFiles) > 0
    iFilesFound = UBound(recFoundFiles)
    On Error GoTo 0
    '*
    '* Clean-up
    Set oFolder = Nothing           '* Although it is nothing
    Set oParentFolder = Nothing
    Set oFileSystem = Nothing

End Function
 
Katılım
29 Aralık 2004
Mesajlar
82
Altın Üyelik Bitiş Tarihi
20-11-2019
Teşekkürler, fakat bir liste almak için bu kadar uzun bir koda ihtiyaç olduğunu düşünmüyorum. Ayrıca kodlarda yazılanları anlamak ve özümsemek istiyorum. Verdiğiniz koddaki tüm yazılan satırların gerekliliği konusunda bir fikrim yok. Daha basit kodlarla ilerlemek istiyorum.
 
Katılım
29 Aralık 2004
Mesajlar
82
Altın Üyelik Bitiş Tarihi
20-11-2019
Bu konudan anlayan biri bana, bir folder içerisindeki txt dosyaları listeleyen en basit kodu yazabilir mi? Ben yukarıdaki örnekte olduğu gibi onlarca satıra gerek olmadığını düşünüyorum.
 
Katılım
29 Aralık 2004
Mesajlar
82
Altın Üyelik Bitiş Tarihi
20-11-2019
10-15 satırlık kod olduğu için basit olarak tanımladım. Bir folderdaki dosyaları listelemek herhalde VBA' da en zor işlerden birisi değil, onun için.
Aslında işi küçümsemek maksadı ile değil, kendime yakıştıramadığım için öyle yazdım. Web sitesinden alıntı yapıp kodu yazdım ama çalışmadı.
 
Üst