🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.vbasic
Article #104795

Re: Przeszukiwanie podfolderów w VBA

#104795
From: mysz leśna
Date: Sun, 13 Mar 2011 13:08
97 lines
4679 bytes
Dosyć podobne rozwiązanie:

'w znacznej części na podstawie:
'http://www.datastrat.com/Download/Picture2K.zip
'http://msdn.microsoft.com/en-us/library/dk008ty4.aspx
'Dir Function

Private Sub ReadFolderInfo(listaPlikow As String, _
                           strFolder As String, _
                           subfold As Boolean, _
                           strExt As String, _
                           nLevels As Long, _
                           Optional ByVal nLevel As Long)

Dim arrFolders() As String
ReDim arrFolders(19) As String 'do 20 podkatalogów na początek
Dim strRelFolder As String
Dim strFile As String
Dim nLen As Long 'żeby dwa razy nie obliczać długości folderu
Dim iLoop As Long, iLoop2 As Long

strFile = Dir(strFolder, vbDirectory)
iLoop = -1

While strFile <> ""

    'wykluczenie "/.." zapobiega wchodzeniu na wyższy folder, "/." do
siebie samego
    'maksymalna długość ścieżki to 260 (259 + Null), ale zdarzają się
pliki dłuższe
    If strFile <> "." And strFile <> ".." And strFile <> "" And
Len(strFolder & strFile) < 260 Then
        'Display the names in E:\ that represent directories.
        'Use bitwise comparison to make sure strFile is a directory.
            On Error Resume Next
        If (GetAttr(strFolder & strFile) And vbDirectory) =
vbDirectory Then
            'dzięki Resume Next można by pominąć warunek And
Len(strFolder & strFile) < 260
            If Err > 0 Then
                Err.Clear
                On Error GoTo 0 'też --> Err = 0 jak po Err.Clear
                GoTo nastepny_plik
            End If
            On Error GoTo 0

            iLoop = iLoop + 1
            If iLoop > UBound(arrFolders) Then ReDim Preserve
arrFolders(iLoop)
            arrFolders(iLoop) = strFile 'podkatalog do sprawdzenia w
kolejnym ReadFolderInfo

        'pliki inne niż katalogi
        Else
            On Error GoTo 0
            If InStr(Right(strFile, Len(strExt)), strExt) > 0 Then _
                    listaPlikow = listaPlikow & vbNewLine & strFolder
& strFile
        End If
    End If '., ..

nastepny_plik:
    strFile = Dir() ' Get next entry

Wend 'Loop

If subfold Then
    'arrFolders() - podkatalogi zapisane w kolejnych
    'If (GetAttr( ... powyższej pętli While
    nLevel = nLevel + 1
    'Debug.Print nLevel & "  " & nLevels
    If nLevels <> -1 And nLevel > nLevels Then Exit Sub
    For iLoop2 = 0 To iLoop
        ReadFolderInfo listaPlikow, strFolder & arrFolders(iLoop2) &
"\", subfold, strExt, nLevels, nLevel
    Next iLoop2
End If

End Sub

Sub wypisz_nazwy()

Dim listaPlikow As String

'katalog musi być ze slashem "\" na końcu
Call ReadFolderInfo(listaPlikow, "E:\Documents and Settings
\Administrator\My Documents\Bazy danych\", True, "txt", -1)
'True czyli z podkatalogami
'-1 czyli wszystkie poziomy w dół
'wyświtela same nazwy po zamianie
'listaPlikow = listaPlikow & vbNewLine & strFolder & strFile
'na
'listaPlikow = listaPlikow & vbNewLine & strFile

Debug.Print listaPlikow

End Sub
Technical Details
Message-ID: <61e11ee8-c69f-4d1f-9cb3-9a399da4e600@r6g2000vbo.googlegroups.com>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!nf1.ipartners.pl!ipartners.pl!news.nask.pl!news.nask.org.pl!news.uni-stuttgart.de!news.belwue.de!newsfeed.ision.net!newsfeed2.easynews.net!ision!newsfeed3.dallas1.level3.net!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!r6g2000vbo.googlegroups.com!not-for-mail
References: <4d68ed34$0$2507$65785112@news.neostrada.pl> <4d68eea3$0$2449$65785112@news.neostrada.pl> <ikassl$ge6$1@inews.gazeta.pl> <4d691a6b$0$2453$65785112@news.neostrada.pl> <ikb88f$cb$1@inews.gazeta.pl>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!nf1.ipartners.pl!ipartners.pl!news.nask.pl!news.nask.org.pl!news.uni-stuttgart.de!news.belwue.de!newsfeed.ision.net!newsfeed2.easynews.net!ision!newsfeed3.dallas1.level3.net!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!r6g2000vbo.googlegroups.com!not-for-mail
From: mysz leśna <mysz.lesna@wp.pl>
Newsgroups: pl.comp.lang.vbasic
Subject: Re: Przeszukiwanie podfolderów w VBA
Date: Sun, 13 Mar 2011 06:08:21 -0700 (PDT)
Organization: http://groups.google.com
Lines: 98
Message-ID: <61e11ee8-c69f-4d1f-9cb3-9a399da4e600@r6g2000vbo.googlegroups.com>
References: <4d68ed34$0$2507$65785112@news.neostrada.pl> <4d68eea3$0$2449$65785112@news.neostrada.pl> <ikassl$ge6$1@inews.gazeta.pl> <4d691a6b$0$2453$65785112@news.neostrada.pl> <ikb88f$cb$1@inews.gazeta.pl>
NNTP-Posting-Host: 89.75.140.97
Mime-Version: 1.0
X-Trace: posting.google.com 1300021701 26114 127.0.0.1 (13 Mar 2011 13:08:21 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Sun, 13 Mar 2011 13:08:21 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: r6g2000vbo.googlegroups.com; posting-host=89.75.140.97; posting-account=6rWQXAoAAAAUjdjXYO6u1EllIObCU-VF
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; GTB6; chromeframe/10.0.648.127; chromeframe; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729),gzip(gfe)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit