0

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

LabRat
  • 1,996
  • 11
  • 56
  • 91

1 Answers1

0
Dim TARGET_FILE() As String 'This represent an array of string

File.ReadAllText

and

 System.IO.StreamWriter

does not receive an array of string as parameter

You should iterate your TARGETFILE array collection and do ReadAllText for every file:

 For Each File As String In TARGETFILE
         Dim ReadedText as string = System.IO.File.ReadAllText(File)
         Dim writer As New System.IO.StreamWriter("DestinationPath")
         writer.Write(ReadedText )
         Write.Close()
 Next
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
  • Err Now im lost i have replaced my for each file with your code as sugested by its all haywire now? – LabRat May 22 '13 at 09:51
  • You have to use foreach to read every file in the TARGETFILE collection you get from directory. once inside the For each statement you can do what you need. I just posted you an example – Carlos Landeras May 22 '13 at 09:54