Toon posts:

[EXCEL] bestandsnamen en locaties in een tabel

Pagina: 1
Acties:
  • 195 views sinds 30-01-2008
  • Reageer

Verwijderd

Topicstarter
Ìk probeer in Excel in een tabel alle bestandsnamen en lokaties vanaf eenbepaalde directry te tonen.
Alle bestanden uit de huidige directory lukt wel, maar ik kom niet dieper.
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sub SelectFiles()
Dim FileArray(100) As String
Dim tekst As String
Filepath = ThisWorkbook.Path
FileArray(0) = Dir(Filepath)
i = 0
While FileArray(i) <> ""
    i = i + 1
    FileArray(i) = Dir
Wend
If i > 1 Then
    For j = 0 To i - 1
    Sheets("Sheet1").Range("A2").Offset(j, 0) = FileArray(j)
    Next j
Else
    BestandNaam = Filepath + "\" + FileArray(0)
End If
End Sub


Hoe lees ik nu ook elke directory en elke subdirectory uit en krijg ik ook de directory namen mee?

  • ParaNoiMia
  • Registratie: Mei 2000
  • Laatst online: 20-08 21:18
Ik weet niet of je hiermee uit de voeten kan in VBA, maar zo doe ik het in VB en de kans is groot dat VBA hier ook mee uit de voeten kan ;)

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
Global MapTree As New Collection

Public Function GetTree(sPathToGet As String) As Boolean

' -------------------------------------------------------------
' Define local variables
' -------------------------------------------------------------
  Dim file_name As String
  Dim files As Collection
  Dim i As Integer

  Dim intAantal As Integer
  
' -------------------------------------------------------------
' Initialize variables
' -------------------------------------------------------------
  Set files = New Collection
  
' -------------------------------------------------------------
' if there is a trailing backslash then remove it.
' -------------------------------------------------------------
  If Right(sPathToGet, 1) = "\" Then
      sPathToGet = Left(sPathToGet, Len(sPathToGet) - 1)
  End If
  
' -------------------------------------------------------------
' Get a list of path & filenames from this folder on down.
' -------------------------------------------------------------
  file_name = Dir(sPathToGet & "\*.*", vbNormal Or vbReadOnly Or vbHidden Or _
                                       vbSystem Or vbArchive Or vbDirectory)
    
' -------------------------------------------------------------
' Loop thru the directory structure and add the
' path & filename to the collection.
' -------------------------------------------------------------
  Do While Len(file_name) > 0
      
      If (file_name <> "..") And (file_name <> ".") Then
          ' add to the collection
          files.Add sPathToGet & "\" & file_name
      End If
      
      file_name = Dir()   ' is there anything left?

  Loop

' -------------------------------------------------------------
' Loop thru the collection and get the files
' and directories
' -------------------------------------------------------------
  For i = 1 To files.Count
      
      ' move the path & filename to a variable
      file_name = files(i)
      
      ' See if it is a directory.
      If GetAttr(file_name) And vbDirectory Then
          ' This is a directory.
          ' get everything in it.
          GetTree file_name
      Else
          MapTree.Add file_name
      End If
      
      DoEvents                         ' allow other processes to happen
      
  Next

  
End Function


GetTree "c:\mapnaam"
intFiles = MapTree.Count

intTel = 1
For intTel = 1 To intFiles
   
      strFileName=MapTree(intTel)
Next

[ Voor 8% gewijzigd door ParaNoiMia op 17-01-2005 13:54 ]


Verwijderd

Topicstarter
Dank voor de snelle react.
dit stukje
code:
1
2
3
Global MapTree As New Collection

Public Function GetTree(sPathToGet As String) As Boolean

doet het niet goed.

Ik heb er dit van gemaakt

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Public Function GetTree(sPathToGet As String) As Boolean

End Function



Sub cool()

' -------------------------------------------------------------
' Define local variables
' -------------------------------------------------------------
  Dim MapTree As New Collection
  Dim file_name As String
  Dim files As Collection
  Dim i As Integer

  Dim intAantal As Integer
  .....

maar nu gaat het stukje

code:
1
2
3
4
      ' See if it is a directory.
      If GetAttr(file_name) And vbDirectory Then
          ' This is a directory.
          ' get everything in it.

mis. Wat nu?

  • ParaNoiMia
  • Registratie: Mei 2000
  • Laatst online: 20-08 21:18
Wat bedoel je met mis ? foutmelding ? zo ja welke ?

Verwijderd

Topicstarter
Hij geeft een foutmelding.
De error is
"Runtime-error '5'
Invalid procedure Call or Argument"

[ Voor 31% gewijzigd door Verwijderd op 17-01-2005 14:24 ]


  • Woudloper
  • Registratie: November 2001
  • Niet online

Woudloper

« - _ - »

Verwijderd schreef op maandag 17 januari 2005 @ 14:23:
Hij geeft een foutmelding.
De error is
"Runtime-error '5'
Invalid procedure Call or Argument"
Wat werkt er niet dan? Als ik namelijk bovenstaande code verander in:
Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Option Explicit
Dim MapTree As New Collection

Public Function GetTree(sPathToGet As String) As Boolean

' -------------------------------------------------------------
' Define local variables
' -------------------------------------------------------------
  Dim file_name As String
  Dim files     As Collection
  Dim i         As Integer

  Dim intAantal As Integer
  
' -------------------------------------------------------------
' Initialize variables
' -------------------------------------------------------------
  Set files = New Collection
  
' -------------------------------------------------------------
' if there is a trailing backslash then remove it.
' -------------------------------------------------------------
  If Right(sPathToGet, 1) = "\" Then
      sPathToGet = Left(sPathToGet, Len(sPathToGet) - 1)
  End If
  
' -------------------------------------------------------------
' Get a list of path & filenames from this folder on down.
' -------------------------------------------------------------
  file_name = Dir(sPathToGet & "\*.*", vbNormal Or vbReadOnly Or vbHidden Or _
                                       vbSystem Or vbArchive Or vbDirectory)
    
' -------------------------------------------------------------
' Loop thru the directory structure and add the
' path & filename to the collection.
' -------------------------------------------------------------
  Do While Len(file_name) > 0
      
      If (file_name <> "..") And (file_name <> ".") Then
          ' add to the collection
          files.Add sPathToGet & "\" & file_name
      End If
      
      file_name = Dir()   ' is there anything left?

  Loop

' -------------------------------------------------------------
' Loop thru the collection and get the files
' and directories
' -------------------------------------------------------------
  For i = 1 To files.Count
      
      ' move the path & filename to a variable
      file_name = files(i)
      
      ' See if it is a directory.
      If GetAttr(file_name) And vbDirectory Then
          ' This is a directory.
          ' get everything in it.
          GetTree file_name
      Else
          MapTree.Add file_name
      End If
      
      DoEvents                         ' allow other processes to happen
      
  Next

  
End Function


Sub TestCode()
Dim intFiles        As Integer
Dim intTel          As Integer
Dim strFileName     As String



    GetTree "C:\tempica"
    intFiles = MapTree.Count
    
    intTel = 1
    For intTel = 1 To intFiles
          strFileName = MapTree(intTel)
          Debug.Print strFileName
    Next

End Sub

werkt het gewoon binnen Excel.

Verwijderd

Topicstarter
Thanx.Nu werkt het inderdaad! Ik had de option explict fout gedaan.
Er mag een slotje op.

  • Woudloper
  • Registratie: November 2001
  • Niet online

Woudloper

« - _ - »

Verwijderd schreef op dinsdag 18 januari 2005 @ 16:19:
Thanx.Nu werkt het inderdaad! Ik had de option explict fout gedaan.
Er mag een slotje op.
We doen hier in principe niet aan slotjes wanneer vragen zijn opgelost. Verder is het 'Option Explicit' niet iets wat je fout gedaan kan hebben. 'Option Explicit' zorgt er gewoon voor dat je code goed wordt gevalideerd, ofwel al je variabele moeten gedeclareerd zijn. Dit behoed je voor eventuele fouten e.d.
Pagina: 1