Home > Archive > Visual Basic > September 2004 > Concatenating text files
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Concatenating text files
|
|
|
| Hello all
Fairly newbie here so please bear with me.
I have a batch file that concatenates about 13 text files into one text
file. Pretty simple using the COPY command. I know there are probably
numerous ways to do it in VB6 but what is the simplest way to do it?
FileCopy seems to be made for one file to one file. Open (for append) 13
times seems to be awkward or cumbersome.
Any thoughts?
Dale
| |
| Ken Halter 2004-09-22, 3:55 pm |
| Dale wrote:
> Hello all
>
> Fairly newbie here so please bear with me.
>
> I have a batch file that concatenates about 13 text files into one text
> file. Pretty simple using the COPY command. I know there are probably
> numerous ways to do it in VB6 but what is the simplest way to do it?
>
> FileCopy seems to be made for one file to one file. Open (for append) 13
> times seems to be awkward or cumbersome.
That's about the only option... here's a sample that shows one way to go
about it, if interested.
Subject: Re: Appending Text Files
http://groups.google.com/groups?hl=...FTNGP12.phx.gbl
--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..
| |
| Tim Baur 2004-09-22, 3:55 pm |
| You could always shell your DOS copy commands. Of course, you would have
to know when the first was complete before firing the second. Probably not
with the effort, but I'm guessing it would at least be a little faster.
"Dale" <daljones@accudirect.com> wrote in
news:uE39U2KoEHA.3668@TK2MSFTNGP15.phx.gbl:
> Hello all
>
> Fairly newbie here so please bear with me.
>
> I have a batch file that concatenates about 13 text files into one
> text file. Pretty simple using the COPY command. I know there are
> probably numerous ways to do it in VB6 but what is the simplest way to
> do it?
>
> FileCopy seems to be made for one file to one file. Open (for
> append) 13 times seems to be awkward or cumbersome.
>
> Any thoughts?
>
> Dale
>
>
| |
|
| Tim,
Well that brings up another sort of question. How are using batch files to
reach your needs viewed by the programming community? I could simply shell
to the current batch file and pass the neccessary file information for it to
do the work. The batch file takes care of identifying the necessary files
to concatenate. It works well now (for what it does), its just a little
unsightly.
Dale
"Tim Baur" <trbo20@disregard_yahoo.com> wrote in message
news:Xns956C6C72FC1B5trbo20hotmailcom@20
7.46.248.16...
> You could always shell your DOS copy commands. Of course, you would have
> to know when the first was complete before firing the second. Probably
> not
> with the effort, but I'm guessing it would at least be a little faster.
>
>
> "Dale" <daljones@accudirect.com> wrote in
> news:uE39U2KoEHA.3668@TK2MSFTNGP15.phx.gbl:
>
>
| |
| Tim Baur 2004-09-22, 3:55 pm |
| "Dale" <daljones@accudirect.com> wrote in news:#pNtRPLoEHA.3820
@TK2MSFTNGP09.phx.gbl:
> How are using batch files to reach your needs viewed by the programming
community?
Nobody can speak for the programming community, but I would say - in
general - sloppy.
You can't really ask that question out of context, though. If you're
writing an app for a thousand end users, then avoid it. It's better to
stay tightly in control of your process when faced with that many
variables.
If you're writing a small tool for a handful of users and you know their
computer setups, then go ahead. It's a handy shortcut.
| |
|
| Tim
Thanx for your input. I am doing it for a small group (under 10 people) so
I guess I'll procede with the batch file option. If time permits, I might
try my luck at coding it better.
Dale
"Tim Baur" <trbo20@disregard_yahoo.com> wrote in message
news:Xns956C701385D9Ctrbo20hotmailcom@20
7.46.248.16...
> "Dale" <daljones@accudirect.com> wrote in news:#pNtRPLoEHA.3820
> @TK2MSFTNGP09.phx.gbl:
>
> community?
>
>
> Nobody can speak for the programming community, but I would say - in
> general - sloppy.
>
> You can't really ask that question out of context, though. If you're
> writing an app for a thousand end users, then avoid it. It's better to
> stay tightly in control of your process when faced with that many
> variables.
>
> If you're writing a small tool for a handful of users and you know their
> computer setups, then go ahead. It's a handy shortcut.
>
| |
| Larry Serflaten 2004-09-22, 3:55 pm |
|
"Dale" <daljones@accudirect.com> wrote
> Thanx for your input. I am doing it for a small group (under 10 people) so
> I guess I'll procede with the batch file option. If time permits, I might
> try my luck at coding it better.
You can have a look at this as a starting point...
HTH
LFS
Option Explicit
Private Sub Form_Load()
Dim OK As Boolean
' Test routine:
' ppclo.txt is in dst directory,
' 2nd file did not exist
' last file uses relative name
OK = AppendFiles("d:\Temp\append.txt", "d:\temp\ppclo.txt", "d:\temp\pppclo.txt", "ppclo.txt")
If Not OK Then MsgBox "Errors detected!", , "AppendFiles test"
End Sub
Private Function AppendFiles(ByRef Destination As String, ParamArray Source()) As Boolean
' Appends all files to end of Destination file (file is created if not present)
' Destination path sets working directory - files from other directories need full path
' (You might want the first Source file to set current directory; use Source(0) instead of Destination...)
Dim bad As New Collection
Dim src As Long, dst As Long
Dim file As Variant, Data() As Byte
On Error GoTo BadTarget
' Open destination / point to next character in file (append)
dst = FreeFile
Open Destination For Binary As dst
S dst, LOF(dst) + 1
If Err.Number = 0 Then
' Change working Drive
If Mid(Destination, 2, 1) = ":" Then
ChDrive Left$(Destination, 2)
End If
' Change working directory
src = Len(Destination)
Do While Mid(Destination, src, 1) <> "\"
src = src - 1
If src < 4 Then Exit Do
Loop
ChDir Left$(Destination, src)
End If
' Working with files always needs to be error trapped....
On Error Resume Next
src = FreeFile
For Each file In Source
' Test for valid file name
Open file For Input As src
If Err.Number Then
bad.Add "Source file: " & file
Err.Clear
Else
' Process valid file
Close src
Open file For Binary As src
ReDim Data(1 To LOF(src))
Get src, 1, Data
Put dst, , Data
End If
Close src
Next
Err.Clear
BadTarget:
' Handle target error
If Err.Number Then
bad.Add "Target file could not be opened: " & Destination
End If
Close dst
' Report errors
If bad.Count Then
file = "The following file(s) caused errors and were not included in the output:" & vbCrLf & vbCrLf
For src = 1 To bad.Count
file = file & bad(src) & vbCrLf
Next
MsgBox file, vbExclamation, "Errors"
Else
' Report all OK
AppendFiles = True
End If
End Function
|
|
|
|
|