I’m trying to edit my code to open all files in the folder ATC (including any subdirectory’s)
If any of these files are found not to contain the text “Index…” that, that particular file is appended to include the text found in document “C:\stuff.txt” and then save the file with the changes…
The issues I'm having are with file names.
First problem is that my TARGET_FILE won’t open; second issue is that my TARGET_FILE wont save?
Can anybody help? I have marked the code with '<<----ISSUE #1 & '<<----ISSUE #2 too point out the problems.
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Get folder containing files to ellipherate through...
Dim TARGET_FILE() As String = IO.Directory.GetFiles("C:\VTS\TREADSTONE LT\ATC", "*", SearchOption.AllDirectories)
For Each file As String In TARGET_FILE
Dim sReader As New StreamReader(file)
Dim content As String = sReader.ReadToEnd()
sReader.Close()
If Text.Contains("Index...") Then
'do nothing
Else
Dim text As String = IO.File.ReadAllText(TARGET_FILE) '<<----ISSUE #1
Dim EXTRA As String = IO.File.ReadAllText("C:\STUFF.TXT")
Dim index As Integer = text.IndexOf("<Tools>")
Dim countChars As Integer
countChars = "<Tools>".Length
If index >= 0 Then
' String is in file, starting at character "<Tools>" insert text "TEST_HELLO"
text = text.Insert(index + countChars, EXTRA)
Dim Writer As System.IO.StreamWriter
Writer = New System.IO.StreamWriter(TARGET_FILE) '<<----ISSUE #2
Writer.Write(text)
Writer.Close()
'Close this program\ form...
'Me.Close()
End If
End If
Next
Me.Close()
End Sub