Concatenate text files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gwarning!

    #1

    Concatenate text files

    Goal: Have multiple text files, each of various lengths, be
    concatenated together into one final consolidated text file.

    Problem: Since the names of the files to be concatenated can change
    from day to day, I need to be able to dynamically generate a batch
    file to concatenate these files. So far, I have looked at using
    FileSearch.Find Files.Count to determine the number of files so that I
    can loop through the files that exist in a directory, and I am
    considering using PUT statement to generate the file. Before I try
    this method I thought that I would ask if others have hints, tricks,
    or especially code that I could be pointed to.

    Example: In DOS, it would take the form:
    copy a.txt + b.txt + c.txt + d.txt final.txt


    Thanks in advance for your help. Greg

  • MGFoster

    #2
    Re: Concatenate text files

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    gwarning! ?? -- any relation to the band GWAR (God, What A Racket)?

    Too bad we don't use the Un*x o/s - then could use the 'cat' command;
    otherwise...

    If all the *.txt files in one folder where what you're after you could
    do this (from VBA):

    shell("type ""c:\my documents\*.txt "" >> final.txt",0)

    The TYPE command displays the contents of the indicated file(s) to the
    screen. If we use the append symbol ">>" it will redirect the
    contents of the indicated file(s) to the last file name, "final.txt. "

    You can use partial file names with wildcards (* or ?) in the command:

    shell("Type ""C:\My Documents\Accou nt*.txt"" >> AllAccounts.txt ",0)

    This will concat all files beginning with "Account" into the file
    "AllAccounts.tx t."

    Put quotes around any path that has a file/folder name with spaces.
    If inside the Shell() function use 2 double-quotes, as shown above.

    There are other ways - I'll let others tell of those.

    HTH,

    MGFoster:::mgf
    Oakland, CA (USA)

    -----BEGIN PGP SIGNATURE-----
    Version: PGP for Personal Privacy 5.0
    Charset: noconv

    iQA/AwUBP5XTJoechKq OuFEgEQJqRwCfTa y5Nxx9vS+Tw6nLH aovm9k1szcAoKbY
    KUNcGKgTqD3x6It Xx71C6yKx
    =bFhU
    -----END PGP SIGNATURE-----


    gwarning! wrote:
    [color=blue]
    > Goal: Have multiple text files, each of various lengths, be concatenated
    > together into one final consolidated text file.
    >
    > Problem: Since the names of the files to be concatenated can change from
    > day to day, I need to be able to dynamically generate a batch file to
    > concatenate these files. So far, I have looked at using
    > FileSearch.Find Files.Count to determine the number of files so that I
    > can loop through the files that exist in a directory, and I am
    > considering using PUT statement to generate the file. Before I try this
    > method I thought that I would ask if others have hints, tricks, or
    > especially code that I could be pointed to.
    >
    > Example: In DOS, it would take the form:
    > copy a.txt + b.txt + c.txt + d.txt final.txt
    >
    >
    > Thanks in advance for your help. Greg
    >[/color]

    Comment

    • Fred

      #3
      Re: Concatenate text files

      I know U want to use dos, but if U know a little VB, U can open a project
      and create a .exe with no forms. Then just code in simple basic commands to
      open file, read rec, string fields,,,, and write rec...to create new file. U
      would have all the string functions available.

      Fred

      "MGFoster" <me@privacy.com > wrote in message
      news:Cqklb.526$ wc3.287@newsrea d3.news.pas.ear thlink.net...[color=blue]
      > -----BEGIN PGP SIGNED MESSAGE-----
      > Hash: SHA1
      >
      > gwarning! ?? -- any relation to the band GWAR (God, What A Racket)?
      >
      > Too bad we don't use the Un*x o/s - then could use the 'cat' command;
      > otherwise...
      >
      > If all the *.txt files in one folder where what you're after you could
      > do this (from VBA):
      >
      > shell("type ""c:\my documents\*.txt "" >> final.txt",0)
      >
      > The TYPE command displays the contents of the indicated file(s) to the
      > screen. If we use the append symbol ">>" it will redirect the
      > contents of the indicated file(s) to the last file name, "final.txt. "
      >
      > You can use partial file names with wildcards (* or ?) in the command:
      >
      > shell("Type ""C:\My Documents\Accou nt*.txt"" >> AllAccounts.txt ",0)
      >
      > This will concat all files beginning with "Account" into the file
      > "AllAccounts.tx t."
      >
      > Put quotes around any path that has a file/folder name with spaces.
      > If inside the Shell() function use 2 double-quotes, as shown above.
      >
      > There are other ways - I'll let others tell of those.
      >
      > HTH,
      >
      > MGFoster:::mgf
      > Oakland, CA (USA)
      >
      > -----BEGIN PGP SIGNATURE-----
      > Version: PGP for Personal Privacy 5.0
      > Charset: noconv
      >
      > iQA/AwUBP5XTJoechKq OuFEgEQJqRwCfTa y5Nxx9vS+Tw6nLH aovm9k1szcAoKbY
      > KUNcGKgTqD3x6It Xx71C6yKx
      > =bFhU
      > -----END PGP SIGNATURE-----
      >
      >
      > gwarning! wrote:
      >[color=green]
      > > Goal: Have multiple text files, each of various lengths, be concatenated
      > > together into one final consolidated text file.
      > >
      > > Problem: Since the names of the files to be concatenated can change from
      > > day to day, I need to be able to dynamically generate a batch file to
      > > concatenate these files. So far, I have looked at using
      > > FileSearch.Find Files.Count to determine the number of files so that I
      > > can loop through the files that exist in a directory, and I am
      > > considering using PUT statement to generate the file. Before I try this
      > > method I thought that I would ask if others have hints, tricks, or
      > > especially code that I could be pointed to.
      > >
      > > Example: In DOS, it would take the form:
      > > copy a.txt + b.txt + c.txt + d.txt final.txt
      > >
      > >
      > > Thanks in advance for your help. Greg
      > >[/color]
      >[/color]


      Comment

      • gwarning!

        #4
        Re: Concatenate text files

        MGF,

        Thanks for your response. I have the following:

        Private Sub cmdConcatenate_ Click()
        On Error GoTo Err_cmdConcaten ate_Click
        Dim TargetFile As String
        Dim FileToCopy As String

        TargetFile = "o:\production\ rawdata\final.e xt"

        Set fs = Application.Fil eSearch
        With fs
        .LookIn = "o:\production\ rawdata\"
        .filename = "*.0ck"
        If .Execute > 0 Then
        For i = 1 To .FoundFiles.Cou nt
        If i = 1 Then
        MsgBox .FoundFiles(i)
        FileToCopy = .FoundFiles.Ite m(i)
        Call Shell("type " & FileToCopy & " > " & TargetFile, 0)

        End If
        If i > 1 Then
        MsgBox .FoundFiles(i)
        FileToCopy = .FoundFiles.Ite m(i)
        Call Shell("type " & FileToCopy & " >> " & TargetFile, 0)
        End If
        Next i

        Else
        MsgBox "There were no files found. Batch consolidation will
        be cancelled.", vbOKOnly, "Batch Consolidation Cancelled"
        End If
        End With

        Exit_cmdConcate nate_Click:
        Exit Sub

        Err_cmdConcaten ate_Click:
        MsgBox Err.Description
        Resume Exit_cmdConcate nate_Click

        End Sub

        Since The first iteration i need to initialize the final.ext file,
        then append to it in subsequent iterations. What I think is my
        problem is that the quotes might be off in the shell function, because
        when i step through the task in the debugger, the vars resolve to the
        correctly, but i killed in the shell statement. maybe i am quoting
        wrong? yours and others help is appreciated.
        thanks, greg

        MGFoster wrote:[color=blue]
        > -----BEGIN PGP SIGNED MESSAGE-----
        > Hash: SHA1
        >
        > gwarning! ?? -- any relation to the band GWAR (God, What A Racket)?
        >
        > Too bad we don't use the Un*x o/s - then could use the 'cat' command;
        > otherwise...
        >
        > If all the *.txt files in one folder where what you're after you could
        > do this (from VBA):
        >
        > shell("type ""c:\my documents\*.txt "" >> final.txt",0)
        >
        > The TYPE command displays the contents of the indicated file(s) to the
        > screen. If we use the append symbol ">>" it will redirect the
        > contents of the indicated file(s) to the last file name, "final.txt. "
        >
        > You can use partial file names with wildcards (* or ?) in the command:
        >
        > shell("Type ""C:\My Documents\Accou nt*.txt"" >> AllAccounts.txt ",0)
        >
        > This will concat all files beginning with "Account" into the file
        > "AllAccounts.tx t."
        >
        > Put quotes around any path that has a file/folder name with spaces.
        > If inside the Shell() function use 2 double-quotes, as shown above.
        >
        > There are other ways - I'll let others tell of those.
        >
        > HTH,
        >
        > MGFoster:::mgf
        > Oakland, CA (USA)
        >
        > -----BEGIN PGP SIGNATURE-----
        > Version: PGP for Personal Privacy 5.0
        > Charset: noconv
        >
        > iQA/AwUBP5XTJoechKq OuFEgEQJqRwCfTa y5Nxx9vS+Tw6nLH aovm9k1szcAoKbY
        > KUNcGKgTqD3x6It Xx71C6yKx
        > =bFhU
        > -----END PGP SIGNATURE-----
        >
        >
        > gwarning! wrote:
        >[color=green]
        >> Goal: Have multiple text files, each of various lengths, be
        >> concatenated together into one final consolidated text file.
        >>
        >> Problem: Since the names of the files to be concatenated can change
        >> from day to day, I need to be able to dynamically generate a batch
        >> file to concatenate these files. So far, I have looked at using
        >> FileSearch.Find Files.Count to determine the number of files so that I
        >> can loop through the files that exist in a directory, and I am
        >> considering using PUT statement to generate the file. Before I try
        >> this method I thought that I would ask if others have hints, tricks,
        >> or especially code that I could be pointed to.
        >>
        >> Example: In DOS, it would take the form:
        >> copy a.txt + b.txt + c.txt + d.txt final.txt
        >>
        >>
        >> Thanks in advance for your help. Greg
        >>[/color]
        >[/color]

        Comment

        • MGFoster

          #5
          Re: Concatenate text files

          -----BEGIN PGP SIGNED MESSAGE-----
          Hash: SHA1

          Thoughts:

          I've found that sometimes the Shell() function ignores the DOS append
          symbol (>>). I've got around this by creating a batch file (.bat)
          with the append commands I wanted to run & running the batch file from
          the Shell() function.

          ==

          I believe (not sure) you can use this command to concat all files in
          one fell swoop:

          type o:\production\r awdata\*.0ck >> final.ext

          That way you won't have to loop thru each individual file name.

          ==

          You only need to use the append symbol (>>), you don't have to first
          redirect (> [which deletes any previous file of the target's name]) &
          then append. If there already exists a file w/ the target's name, you
          can delete it before the append command.

          ==

          In VBA you may wish to concat the path to the FileToCopy: e.g.:

          dim CopyPath As String
          CopyPath = "o:\production\ rawdata\"

          ' Set up the TargetFile as a concat of the CopyPath & the
          ' final file name.
          TargetFile = CopyPath & "final.ext"

          Then in the Shell call do this:

          Call Shell("type " & CopyPath & FileToCopy & " > " & TargetFile, 0)

          ==

          When doing a Shell call I usually prefer to create the complete
          command string & then put it in the Shell. E.g.:

          Dim strCommand as String
          strCommand = "type " & CopyPath & FileToCopy & " >> " & TargetFile

          Call Shell(strComman d,0)

          That way, during Debug, I can see if the command string is constructed
          correctly. Also, I can copy the command string & paste it into a DOS
          window to see if it runs. Sometimes the command will run in the DOS
          window but not in the Shell! Sad, but true.

          ==

          You're VBA commands may be running faster than the DOS type command,
          which means you're code must wait until the Shell function call
          completes. See this URL for a code that waits for the Shell function
          to complete:




          HTH,

          MGFoster:::mgf
          Oakland, CA (USA)

          -----BEGIN PGP SIGNATURE-----
          Version: PGP for Personal Privacy 5.0
          Charset: noconv

          iQA/AwUBP5cDKYechKq OuFEgEQLXoQCgyj DstEDEd2Aa9rqEv fbIFNmwAUoAoNqz
          1S4lOQI6S0tvlOR t0BhFjp52
          =f7ZR
          -----END PGP SIGNATURE-----


          gwarning! wrote:
          [color=blue]
          > MGF,
          >
          > Thanks for your response. I have the following:
          >
          > Private Sub cmdConcatenate_ Click()
          > On Error GoTo Err_cmdConcaten ate_Click
          > Dim TargetFile As String
          > Dim FileToCopy As String
          >
          > TargetFile = "o:\production\ rawdata\final.e xt"
          >
          > Set fs = Application.Fil eSearch
          > With fs
          > .LookIn = "o:\production\ rawdata\"
          > .filename = "*.0ck"
          > If .Execute > 0 Then
          > For i = 1 To .FoundFiles.Cou nt
          > If i = 1 Then
          > MsgBox .FoundFiles(i)
          > FileToCopy = .FoundFiles.Ite m(i)
          > Call Shell("type " & FileToCopy & " > " & TargetFile, 0)
          >
          > End If
          > If i > 1 Then
          > MsgBox .FoundFiles(i)
          > FileToCopy = .FoundFiles.Ite m(i)
          > Call Shell("type " & FileToCopy & " >> " & TargetFile, 0)
          > End If
          > Next i
          >
          > Else
          > MsgBox "There were no files found. Batch consolidation will be
          > cancelled.", vbOKOnly, "Batch Consolidation Cancelled"
          > End If
          > End With
          >
          > Exit_cmdConcate nate_Click:
          > Exit Sub
          >
          > Err_cmdConcaten ate_Click:
          > MsgBox Err.Description
          > Resume Exit_cmdConcate nate_Click
          >
          > End Sub
          >
          > Since The first iteration i need to initialize the final.ext file, then
          > append to it in subsequent iterations. What I think is my problem is
          > that the quotes might be off in the shell function, because when i step
          > through the task in the debugger, the vars resolve to the correctly, but
          > i killed in the shell statement. maybe i am quoting wrong? yours and
          > others help is appreciated.
          > thanks, greg
          >
          > MGFoster wrote:
          >[color=green]
          >> -----BEGIN PGP SIGNED MESSAGE-----
          >> Hash: SHA1
          >>
          >> gwarning! ?? -- any relation to the band GWAR (God, What A Racket)?
          >>
          >> Too bad we don't use the Un*x o/s - then could use the 'cat' command;
          >> otherwise...
          >>
          >> If all the *.txt files in one folder where what you're after you could
          >> do this (from VBA):
          >>
          >> shell("type ""c:\my documents\*.txt "" >> final.txt",0)
          >>
          >> The TYPE command displays the contents of the indicated file(s) to the
          >> screen. If we use the append symbol ">>" it will redirect the
          >> contents of the indicated file(s) to the last file name, "final.txt. "
          >>
          >> You can use partial file names with wildcards (* or ?) in the command:
          >>
          >> shell("Type ""C:\My Documents\Accou nt*.txt"" >> AllAccounts.txt ",0)
          >>
          >> This will concat all files beginning with "Account" into the file
          >> "AllAccounts.tx t."
          >>
          >> Put quotes around any path that has a file/folder name with spaces.
          >> If inside the Shell() function use 2 double-quotes, as shown above.
          >>
          >> There are other ways - I'll let others tell of those.
          >>
          >> HTH,
          >>
          >> MGFoster:::mgf
          >> Oakland, CA (USA)
          >>
          >> -----BEGIN PGP SIGNATURE-----
          >> Version: PGP for Personal Privacy 5.0
          >> Charset: noconv
          >>
          >> iQA/AwUBP5XTJoechKq OuFEgEQJqRwCfTa y5Nxx9vS+Tw6nLH aovm9k1szcAoKbY
          >> KUNcGKgTqD3x6It Xx71C6yKx
          >> =bFhU
          >> -----END PGP SIGNATURE-----
          >>
          >>
          >> gwarning! wrote:
          >>[color=darkred]
          >>> Goal: Have multiple text files, each of various lengths, be
          >>> concatenated together into one final consolidated text file.
          >>>
          >>> Problem: Since the names of the files to be concatenated can change
          >>> from day to day, I need to be able to dynamically generate a batch
          >>> file to concatenate these files. So far, I have looked at using
          >>> FileSearch.Find Files.Count to determine the number of files so that I
          >>> can loop through the files that exist in a directory, and I am
          >>> considering using PUT statement to generate the file. Before I try
          >>> this method I thought that I would ask if others have hints, tricks,
          >>> or especially code that I could be pointed to.
          >>>
          >>> Example: In DOS, it would take the form:
          >>> copy a.txt + b.txt + c.txt + d.txt final.txt
          >>>
          >>>
          >>> Thanks in advance for your help. Greg
          >>>[/color]
          >>[/color]
          >[/color]

          Comment

          • gwarning!

            #6
            Re: Concatenate text files

            Here's my solution:

            Private Sub Command6_Click( )
            On Error GoTo Err_Command6_Cl ick

            Dim FileToCopy As String
            Dim RemLine As String
            Dim BatLine As String
            Dim File As String
            Dim BatchNumberOnly As String

            Set fs = Application.Fil eSearch

            RemLine = "REM v:\fits\consol. bat created from " _
            & "FITS.frmConsol idate.cmdConcat enate"

            DoCmd.RunSQL ("Delete * from tblFiles;")

            With fs
            .LookIn = "o:\production\ rawdata\"
            .filename = "*.0?k"
            If .Execute > 0 Then
            For i = 1 To .FoundFiles.Cou nt
            File = Mid(.FoundFiles (i), 23, 11)
            BatchNumberOnly = Mid(.FoundFiles (i), 26, 4)
            Path = .LookIn
            DoCmd.RunSQL ("INSERT INTO tblFiles (Name, File, Path,
            BatchNumberOnly ) VALUES (' " & .FoundFiles(i) & "','" & File & "','" &
            Path & "','" & BatchNumberOnly & "');")

            If i = 1 Then
            MsgBox .FoundFiles(i)
            FileToCopy = .FoundFiles.Ite m(i)
            BatLine = "type " & FileToCopy & " >
            o:\production\r awdata\final.ex t"
            Open "v:\fits\consol .bat" For Output As #1
            Print #1, RemLine
            Print #1, BatLine
            End If

            If i > 1 Then
            MsgBox .FoundFiles(i)
            FileToCopy = .FoundFiles.Ite m(i)
            BatLine = "type " & FileToCopy & " >>
            o:\production\r awdata\final.ex t"
            Print #1, BatLine
            End If

            If i = .FoundFiles.Cou nt Then
            Close #1
            Shell "v:\fits\consol .bat", 0

            End If
            Next i

            Else
            MsgBox "There were no files found. Batch consolidation will
            be cancelled.", vbOKOnly, "Batch Consolidation Cancelled"
            End If
            End With

            MsgBox "Batch consolidation is complete. Thanks!", vbOKOnly,
            "Batch Consolidation Complete"


            Exit_Command6_C lick:
            Exit Sub

            Err_Command6_Cl ick:
            MsgBox Err.Description
            Resume Exit_Command6_C lick

            End Sub


            gwarning! wrote:[color=blue]
            > Goal: Have multiple text files, each of various lengths, be concatenated
            > together into one final consolidated text file.
            >
            > Problem: Since the names of the files to be concatenated can change from
            > day to day, I need to be able to dynamically generate a batch file to
            > concatenate these files. So far, I have looked at using
            > FileSearch.Find Files.Count to determine the number of files so that I
            > can loop through the files that exist in a directory, and I am
            > considering using PUT statement to generate the file. Before I try this
            > method I thought that I would ask if others have hints, tricks, or
            > especially code that I could be pointed to.
            >
            > Example: In DOS, it would take the form:
            > copy a.txt + b.txt + c.txt + d.txt final.txt
            >
            >
            > Thanks in advance for your help. Greg
            >[/color]

            Comment

            Working...