Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append Text Files Using VB

Status
Not open for further replies.

jdubya2000

IS-IT--Management
Sep 7, 2003
1
NZ
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
 
Well this sounds like VBScript, not Visual Basic. Variables aren't typed in VBS, they're all variants. Just remove the "As Type"

Sub ConcatenateFiles(resultFile, header, sourceFiles)

Dim writer
Dim reader

You can't set project references (early bind to objects) and hence can't use New to instantiate them. Must use the CreateObject function instead.

Paul Bent
Northwind IT Systems
 
Beaten to it by strongm but yes it is .NET. Statements such as Try are used by .NET to catch errors (a bit like On Error in VB6).

For a simple example of merging files try something like: (you will need a form with a command button on it name Command1)
Code:
Option Explicit
Dim dirpath As String
Dim outf As Integer

Private Sub Command1_Click()
ConvertFiles
End Sub

Sub ConvertFiles()
 dirpath = "C:\temp\"
 outf = FreeFile
 Open dirpath & "combinedfiles.txt" For Output As #outf
   ReadFile "C:\temp\directory1\1.txt"
   ReadFile "C:\temp\directory2\2.txt"
   'etc.
 Close #outf
End Sub

Private Sub ReadFile(filename As String)
 Dim inf As Integer
 Dim nextline As String
 inf = FreeFile

 Open dirpath & filename For Input As #inf
 ' read in each line of file and output
 Do Until EOF(inf)
   Line Input #inf, nextline$
   Print #outf, nextline$
 Loop
 Close #inf
 
 MsgBox "Completed"
End Sub



----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top