Re: Appending text files

From: Björn Holmgren (bjohol_at_hotmail.com)
Date: 06/09/04


Date: Wed, 9 Jun 2004 15:32:28 +0200


"softrock" <anonymous@discussions.microsoft.com> wrote in message
news:62E81C85-DBAC-4685-8548-20F6FD0D3F32@microsoft.com...
> Does anyone know how to append text files using VB? I am outputting three
text files from SQL Server using DTS. The first file contains header data,
the second contains the base data and the third contains trailer data. I
need to be able to append the header data at the beginning of the base data
and the trailed data on the end. I've never appended text files before and
would really appreciate some help. I am using VB6 and SQL Server 2000 on an
NT4 box.

Here's one way:

When you call the JoinFiles subroutine you must pass it an array of files to
join. The array can contain one or more filenames. You also pass it the name
of an output file which will receive the joined contents.

Example:

Call JoinFiles(Array("File1.txt", "File2.txt", "File3.txt"), "File123.txt")

(joins File1.txt, File2.txt and File3.txt, outputs result to File123.txt)

'----------
' Joins two or more files
' Note: First parameter expected to be an array of filenames
Sub JoinFiles(sFiles As Variant, sFileNew As String)
    Dim Index As Long
    Dim FileIn As Long
    Dim FileOut As Long
    Dim sBuf As String

    ' Create the new output file
    FileOut = FreeFile
    Open sFileNew For Binary Access Write As #FileOut
    ' For each file in the sFiles array...
    For Index = LBound(sFiles) To UBound(sFiles)
        ' Open the input file
        FileIn = FreeFile
        Open sFiles(Index) For Binary Access Read As FileIn
        ' Create a string buffer to hold the entire file contents
        sBuf = Space(FileLen(sFiles(Index)))
        ' Read the contents of the file to the string buffer
        Get #FileIn, , sBuf
        ' Close the input file
        Close #FileIn
        ' Write the string buffer to the output file
        Put #FileOut, , sBuf
    Next
    ' Close the output file
    Close #FileOut
End Sub

'----------

--
Björn Holmgren
Guide Konsult AB


Relevant Pages

  • Re: Connecting to SQL Server via ADO
    ... your Append queries. ... connection to the SQL Server, I only have this connection when at home, ... Dim Conn As ADODB.Connection ...
    (microsoft.public.access.modulesdaovba)
  • Appending text to an array of textboxes
    ... I want to be able to append text using a For loop to each textbox in an ... want to add a number of spaces to each textbox in the array. ... Dim i as integer ...
    (microsoft.public.dotnet.languages.vb)
  • Re: emptying array contents
    ... Dim MyArray ... 'code which assigns values to the array ... append to the ned of the file instead, ... > what would be better would be to dump the contents of the ...
    (microsoft.public.access.modulesdaovba)
  • Re: Detecting a running process.
    ... Private Declare Function EnumProcessModules Lib "psapi.dll" _ ... (ByVal dwProcessID As Long, _ ... Dim nProcesses As Long ... 'fill an array of longs with the ...
    (microsoft.public.vb.winapi)
  • Re: Updated datestamp doesnt work
    ... Public Sub StoreMyOldVals ... ' store values of current row in array ... Dim dbs As DAO.Database, rst As DAO.Recordset ... Dim var As Variant ...
    (microsoft.public.access.gettingstarted)