Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2004
    Posts
    5

    Thumbs down Unanswered: Can't figure out simple dir() function

    I am trying to populate a table with path\filenames for a dir and its sub dirs.

    From what I have been seeing dir() is the easiest way, but i am having trouble getting the text into the table.

    Ive been trying for a couple days and my boss wants to know whats going on.

    Thanks

    Ryan

  2. #2
    Join Date
    Jun 2004
    Location
    Florida, US
    Posts
    521
    what is the problem? why you can't put the text into table?
    ghozy.

  3. #3
    Join Date
    Jun 2004
    Posts
    5
    I cannot get the dir() function to generate a string with a list of all files in the dir and sun dirs or get that string to populate a table

    I am very new to databases. my database is very simple except for this one function.

    ryan

  4. #4
    Join Date
    Jun 2004
    Posts
    5
    this is my code so far

    Private Sub Command1_Click()
    Dim strDir As String
    Dim strFileName As String
    Dim strfilelist As String
    strDir = "u:\"
    strFileName = Dir(strDir)

    Do While strFileName <> "" ' Start the loop.
    If strfilelist = "" Then
    strfilelist = strFileName
    Else
    strfilelist = Me.lstfilelist.RowSource & ", " & strFileName
    End If
    strFileName = Dir ' Get next entry.
    Loop
    End Sub

  5. #5
    Join Date
    Jun 2004
    Location
    Florida, US
    Posts
    521
    dir function only gets one item per call, so you have to go for a loop to get all list. in your first Dir call should include a path, then you should keep calling dir function wihtout parameters, get next directory item till you get an Empty String.

    Code:
     
    Dim DirStr As String
    	DirStr = Dir("c:\") 'your first Dir call include a path
    Do While DirStr <> ""   ' if Dir returns empty string stop reading directory(end of list)
    	DirStr = Dir		' get next item in the directory
    	Debug.Print DirStr  'print dir item debug
    Loop
    ghozy.

  6. #6
    Join Date
    Jun 2004
    Location
    Florida, US
    Posts
    521
    sorry didn't see you sent a code. I will write a code for this problem after I finish my work here.
    ghozy.

  7. #7
    Join Date
    Jun 2004
    Location
    Florida, US
    Posts
    521
    here is the code I promised. this function gets a path as paramter and populates the Table3 table with filenames then returns number of records populated.


    Code:
     
    Public Function ListDir(Path As String) As Long
    '**********************************************
    ' This function copies filenames in a directory into table3 table
    ' that table should have Path and Filename fields.
    ' function returns number of dir entries copied into table.
    '**********************************************
    Dim ADOCon As ADODB.Connection  'to hold current connection
    Dim DirStr As String	'to hold a directory entry
    Dim DirCount As Long	'to count directory entries
    Set ADOCon = Application.CurrentProject.Connection  'get current connection
    DirCount = 0	'initialize variables
    ListDir = 0
    DirStr = Dir(Path)  'your first Dir call include a path
    If DirStr = "" Then Exit Function   'if dir is empty then exit function
    Do While DirStr <> ""   ' if Dir returns empty string stop reading directory(end of list)
        ' add current path and Dir entry to Table3 table.
    	ADOCon.Execute "INSERT INTO Table3 (Path, Filename) Values('" & Path & "', '" & DirStr & "');"
    	DirCount = DirCount + 1
    	DirStr = Dir	    ' get next item in the directory
    Loop
    ListDir = DirCount  'return number of dir entries back
    End Function
    ghozy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •