jdubya2000
IS-IT--Management
Hi!
I am new to VB and am trying to write a script that will append several text files to one single file. I imagine this should be fairly easy, and have searched far and wide for a simple example, but can't quite seem to get it working. The text files will be in different directories.
Most examples I find do not seem to work because of the "As" used in defining objects. Is there anything I need to put at the top of the script to get this to work? I am simply using a .vbs file in Windows XP to run the script.
Any help would be most appreciated!
Josh
Sub ConcatenateFiles(ByVal resultFile) As String, ByVal header As String, ByVal ParamArray sourceFiles() As String)
Dim writer As System.IO.StreamWriter
Dim reader As System.IO.StreamReader
Try
writer = New System.IO.StreamWriter(resultFile,_ True)
Dim fpath As String
For Each fpath In sourceFiles
' write the header string, with #FilePath# replaced with
' the path of the source file being processed
writer.WriteLine(header.Replace("#FilePath#", fpath))
Try
' open the source file in read mode
reader = New System.IO.StreamReader(fpath)
' append all the content of the current file in the destination
' file
writer.Write(reader.ReadToEnd() & Environment.NewLine)
Finally
If Not reader Is Nothing Then
reader.Close()
reader = Nothing
End If
End Try
Next
Finally
' close the writer stream
If Not writer Is Nothing Then writer.Close()
End Try
End Sub
I am new to VB and am trying to write a script that will append several text files to one single file. I imagine this should be fairly easy, and have searched far and wide for a simple example, but can't quite seem to get it working. The text files will be in different directories.
Most examples I find do not seem to work because of the "As" used in defining objects. Is there anything I need to put at the top of the script to get this to work? I am simply using a .vbs file in Windows XP to run the script.
Any help would be most appreciated!
Josh
Sub ConcatenateFiles(ByVal resultFile) As String, ByVal header As String, ByVal ParamArray sourceFiles() As String)
Dim writer As System.IO.StreamWriter
Dim reader As System.IO.StreamReader
Try
writer = New System.IO.StreamWriter(resultFile,_ True)
Dim fpath As String
For Each fpath In sourceFiles
' write the header string, with #FilePath# replaced with
' the path of the source file being processed
writer.WriteLine(header.Replace("#FilePath#", fpath))
Try
' open the source file in read mode
reader = New System.IO.StreamReader(fpath)
' append all the content of the current file in the destination
' file
writer.Write(reader.ReadToEnd() & Environment.NewLine)
Finally
If Not reader Is Nothing Then
reader.Close()
reader = Nothing
End If
End Try
Next
Finally
' close the writer stream
If Not writer Is Nothing Then writer.Close()
End Try
End Sub