Read dir into Dataset

  • Thread starter Thread starter John Devlon
  • Start date Start date
J

John Devlon

Hi,

Does anyony know how to read an entire directory into a DataSet, including
all sub-directories and filenames ?

Thanx

John
 
John Devlon said:
Hi,

Does anyony know how to read an entire directory into a DataSet, including
all sub-directories and filenames ?

Thanx

John

Hi John

Here is a complete routine i used to read complete drives...

Set the 'Drv' parameter to the wanted drive/folder. i.e. GetDir("c:\")

I use it to be put into a Listbox, but you can easily change the code to put
it into a dataset instead

Hope it helps you...

/Finn

+++++++++++++++++++++++++++++++++++++
Private Sub GetDir(ByVal Drv As String)


' Create a new DirectoryInfo object.
Dim dir As New System.IO.DirectoryInfo(Drv)

If Not dir.Exists Then
Throw New System.IO.DirectoryNotFoundException("The directory
does not exist.")
End If

' Call the GetFileSystemInfos method.
Dim infos As System.IO.FileSystemInfo() =
dir.GetFileSystemInfos("*.*")

ListDirectoriesAndFiles(infos, "*.*")

TextBox2.Text = files
End Sub

Private Sub ListDirectoriesAndFiles(ByVal FSInfo() As
System.IO.FileSystemInfo, ByVal SearchString As String)
' Check the parameters.
If FSInfo Is Nothing Then
Throw New ArgumentNullException("FSInfo")
End If
If SearchString Is Nothing OrElse SearchString.Length = 0 Then
Throw New ArgumentNullException("SearchString")
End If

' Iterate through each item.
Dim i As System.IO.FileSystemInfo
For Each i In FSInfo
' Check to see if this is a DirectoryInfo object.
If TypeOf i Is System.IO.DirectoryInfo Then

' Cast the object to a DirectoryInfo object.
Dim dInfo As System.IO.DirectoryInfo = CType(i,
System.IO.DirectoryInfo)

' Iterate through all sub-directories.
ListDirectoriesAndFiles(dInfo.GetFileSystemInfos(SearchString),
SearchString)

' Check to see if this is a FileInfo object.
ElseIf TypeOf i Is System.IO.FileInfo Then
Then LB1.Items.Add(i.FullName.ToString)
End If
Next i

End Sub
 
John Devlon said:
Hi,

Does anyony know how to read an entire directory into a DataSet, including
all sub-directories and filenames ?

Thanx

John

Hi John

Here is a complete routine i used to read complete drives...

Set the 'Drv' parameter to the wanted drive/folder. i.e. GetDir("c:\")

I use it to be put into a Listbox, but you can easily change the code to put
it into a dataset instead

Hope it helps you...

/Finn

+++++++++++++++++++++++++++++++++++++
Private Sub GetDir(ByVal Drv As String)


' Create a new DirectoryInfo object.
Dim dir As New System.IO.DirectoryInfo(Drv)

If Not dir.Exists Then
Throw New System.IO.DirectoryNotFoundException("The directory
does not exist.")
End If

' Call the GetFileSystemInfos method.
Dim infos As System.IO.FileSystemInfo() =
dir.GetFileSystemInfos("*.*")

ListDirectoriesAndFiles(infos, "*.*")

TextBox2.Text = files
End Sub

Private Sub ListDirectoriesAndFiles(ByVal FSInfo() As
System.IO.FileSystemInfo, ByVal SearchString As String)
' Check the parameters.
If FSInfo Is Nothing Then
Throw New ArgumentNullException("FSInfo")
End If
If SearchString Is Nothing OrElse SearchString.Length = 0 Then
Throw New ArgumentNullException("SearchString")
End If

' Iterate through each item.
Dim i As System.IO.FileSystemInfo
For Each i In FSInfo
' Check to see if this is a DirectoryInfo object.
If TypeOf i Is System.IO.DirectoryInfo Then

' Cast the object to a DirectoryInfo object.
Dim dInfo As System.IO.DirectoryInfo = CType(i,
System.IO.DirectoryInfo)

' Iterate through all sub-directories.
ListDirectoriesAndFiles(dInfo.GetFileSystemInfos(SearchString),
SearchString)

' Check to see if this is a FileInfo object.
ElseIf TypeOf i Is System.IO.FileInfo Then
Then LB1.Items.Add(i.FullName.ToString)
End If
Next i

End Sub
 
Sub Main ()
Dim DT As New System.Data.DataTable
DT.Columns.Add("path")
GetDirs("c:\", DT)
Dim DS As System.Data.DataSet = dt.DataSet
DataGrid1.DataSource = DS
DataGrid1.DataBind()
End Sub

Sub GetDirs(ByVal Root As String, ByRef DT As
System.data.DataTable)
Try
Dim row As System.data.DataRow = DT.NewRow
row("path") = Root
DT.Rows.Add(row)
Dim Dirs() As String = IO.Directory.GetDirectories(Root)
For i As Integer = 0 To Dirs.Length - 1
GetDirs(Dirs(i), DT)
Next
Catch ex As Exception

End Try
End Sub

Developers list your services
http://directory.kr3at.com
 
Back
Top