I have the following code.
Dim text As String = IO.File.ReadAllText("C:Example.xtp")
This code is specific to a single file, however I would like to file.readalltext
for every file in a particular directory.
How can I achieve this?
0
You will need to use the IO.Directory.GetFiles function.
Dim files() As String = IO.Directory.GetFiles("c:")
For Each file As String In files
' Do work, example
Dim text As String = IO.File.ReadAllText(file)
Next
Dim fileEntries As String() = Directory.GetFiles("YourPath", "*.txt")
' Process the list of .txt files found in the directory. '
Dim fileName As String
For Each fileName In fileEntries
If (System.IO.File.Exists(fileName)) Then
'Read File and Print Result if its true
ReadFile(fileName)
End If
TransfereFile(fileName, 1)
Next
System.IO.Directory.GetFiles()
could help
Try this:
Dim text As String = ""
Dim files() As String = IO.Directory.GetFiles(sFolder)
For Each sFile As String In files
text &= IO.File.ReadAllText(sFile)
Next
Public PathsAll As String 'is All Path Finded
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim BeginPath As String = "D:" 'this is Begin Path
FilesSearch(BeginPath)
'TextBox1.Text = AddPath
End Sub
Public Sub FilesSearch(BeginPath As String)
Dim arrFolders(0) As String
Dim TmpDir As String
Dim arrFiles As String
Dim countDir As Integer = 0
arrFolders(countDir) = ""
If Microsoft.VisualBasic.Right(BeginPath, 1) <> "" Then
BeginPath = BeginPath & ""
End If
TmpDir = Dir(BeginPath, vbDirectory)
' On Error Resume Next
Do While TmpDir <> ""
AppendFiles(BeginPath & TmpDir)
If TmpDir <> "." And TmpDir <> ".." Then
If (GetAttr(BeginPath & TmpDir) And vbDirectory) = vbDirectory Then
arrFolders(countDir) = BeginPath & TmpDir & ""
countDir += 1
ReDim Preserve arrFolders(countDir)
End If
End If
TmpDir = Dir()
Loop
arrFiles = Dir(BeginPath)
Do Until arrFiles = ""
arrFiles = Dir()
Loop
For X = 0 To (UBound(arrFolders) - 1)
FilesSearch(arrFolders(X))
Next X
End Sub
Public Sub AppendFiles(InPath As String)
PathsAll += vbNewLine & InPath
End Sub
2