473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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.FindFiles.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

Nov 12 '05 #1
5 23020
-----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\Account*.txt"" >> AllAccounts.txt",0)

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

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/AwUBP5XTJoechKqOuFEgEQJqRwCfTay5Nxx9vS+Tw6nLHaovm9 k1szcAoKbY
KUNcGKgTqD3x6ItXx71C6yKx
=bFhU
-----END PGP SIGNATURE-----
gwarning! wrote:
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.FindFiles.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


Nov 12 '05 #2
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:Cq***************@newsread3.news.pas.earthlin k.net...
-----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\Account*.txt"" >> AllAccounts.txt",0)

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

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/AwUBP5XTJoechKqOuFEgEQJqRwCfTay5Nxx9vS+Tw6nLHaovm9 k1szcAoKbY
KUNcGKgTqD3x6ItXx71C6yKx
=bFhU
-----END PGP SIGNATURE-----
gwarning! wrote:
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.FindFiles.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

Nov 12 '05 #3
MGF,

Thanks for your response. I have the following:

Private Sub cmdConcatenate_Click()
On Error GoTo Err_cmdConcatenate_Click
Dim TargetFile As String
Dim FileToCopy As String

TargetFile = "o:\production\rawdata\final.ext"

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

End If
If i > 1 Then
MsgBox .FoundFiles(i)
FileToCopy = .FoundFiles.Item(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_cmdConcatenate_Click:
Exit Sub

Err_cmdConcatenate_Click:
MsgBox Err.Description
Resume Exit_cmdConcatenate_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:
-----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\Account*.txt"" >> AllAccounts.txt",0)

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

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/AwUBP5XTJoechKqOuFEgEQJqRwCfTay5Nxx9vS+Tw6nLHaovm9 k1szcAoKbY
KUNcGKgTqD3x6ItXx71C6yKx
=bFhU
-----END PGP SIGNATURE-----
gwarning! wrote:
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.FindFiles.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


Nov 12 '05 #4
-----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\rawdata\*.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(strCommand,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:

http://www.mvps.org/access/api/api0004.htm
HTH,

MGFoster:::mgf
Oakland, CA (USA)

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

iQA/AwUBP5cDKYechKqOuFEgEQLXoQCgyjDstEDEd2Aa9rqEvfbIFN mwAUoAoNqz
1S4lOQI6S0tvlORt0BhFjp52
=f7ZR
-----END PGP SIGNATURE-----
gwarning! wrote:
MGF,

Thanks for your response. I have the following:

Private Sub cmdConcatenate_Click()
On Error GoTo Err_cmdConcatenate_Click
Dim TargetFile As String
Dim FileToCopy As String

TargetFile = "o:\production\rawdata\final.ext"

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

End If
If i > 1 Then
MsgBox .FoundFiles(i)
FileToCopy = .FoundFiles.Item(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_cmdConcatenate_Click:
Exit Sub

Err_cmdConcatenate_Click:
MsgBox Err.Description
Resume Exit_cmdConcatenate_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:
-----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\Account*.txt"" >> AllAccounts.txt",0)

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

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/AwUBP5XTJoechKqOuFEgEQJqRwCfTay5Nxx9vS+Tw6nLHaovm9 k1szcAoKbY
KUNcGKgTqD3x6ItXx71C6yKx
=bFhU
-----END PGP SIGNATURE-----
gwarning! wrote:
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.FindFiles.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


Nov 12 '05 #5
Here's my solution:

Private Sub Command6_Click()
On Error GoTo Err_Command6_Click

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

Set fs = Application.FileSearch

RemLine = "REM v:\fits\consol.bat created from " _
& "FITS.frmConsolidate.cmdConcatenate"

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

With fs
.LookIn = "o:\production\rawdata\"
.filename = "*.0?k"
If .Execute > 0 Then
For i = 1 To .FoundFiles.Count
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.Item(i)
BatLine = "type " & FileToCopy & " >
o:\production\rawdata\final.ext"
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.Item(i)
BatLine = "type " & FileToCopy & " >>
o:\production\rawdata\final.ext"
Print #1, BatLine
End If

If i = .FoundFiles.Count 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_Click:
Exit Sub

Err_Command6_Click:
MsgBox Err.Description
Resume Exit_Command6_Click

End Sub
gwarning! wrote:
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.FindFiles.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


Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Steve | last post by:
My application uses transfertext to create text files that are FTPed to a website and are processed by a webstore. The text files can be for one of three purposes: Add one or more items to the...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
3
by: Dave | last post by:
I need to concatenate serveral text files into a larger file. I borrowed some code and patched together a batch file that runs in the command processor and looks like this: echo off chdir...
0
by: richardkreidl | last post by:
I have the following hash script that I use to compare two text files. 'Class Public Class FileComparison Public Class FileComparisonException Public Enum ExceptionType U 'Unknown A 'Add...
1
by: svijay | last post by:
hi I have got a strange problem. May I know any solution for this. Here is the detailed description about the problem We have got a mainframe system and also production and development...
1
by: sham | last post by:
Hi to all, Is it possible with the .net framework to concatenate xml files? I have a number of xml files on disk and want to create one file which will have the format : <files> <file...
5
by: Alan Searle | last post by:
I am exporting ms-access data to XML files. This works fine. However, I need to insert one line at the top of each exported file (i.e. a reference to the XSL file) and am having a problem with...
5
by: praveenholal | last post by:
Hi Freinds , I want to convert the files that are in text format (.txt) to CSV file. I am working on Linux. So can anyone guide me. Here is my problem Description I have some result files...
14
by: rafal_ | last post by:
Is difference between interpretation of \n only difference ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.