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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining Text files

Status
Not open for further replies.

SysDupe123

Technical User
Joined
Dec 17, 2003
Messages
74
Location
US
I'm going a little nuts. I want to write some code in Access 2002 to take a text file and append another text file if it exists. The thing is, I'd have 3 main files, and up to 3 additional files that need to be combined with them, like file1.txt, file2.txt, file3.txt and file1a.txt, file2a.txt, and file3a.txt. I need to combine file1 & 1a, etc. so I don't think I could use Dir(). I'd really appreciate the help.
 
Your question isn't clear. Are you asking how to append one text file to another or how to loop thru the available files using Dir()?
 
The original DOS 'copy' command does file concatenation on text files, using the + operator, if you wanted to do it that way with a shell command. e.g.

if exist file1a.txt copy file1.txt+file1a.txt out1.txt

as a batch language command. Type 'copy /?' for help on syntax.

Otherwise you can do it in VBA by checking the existence of the second file, then I guess opening each file and writing them out in turn to a single output file with gets and puts or whatever.

Cheers,
Sean

 
Here's what I'm trying to do:
If file1a.txt exists, append to file1.txt
if file2a.txt exists, append to file2.txt
if file3a.txt exists, append to file3.txt

I've tried to use the CopyFile in FileSystemObject scripting, but I keep getting different errors. I'm not familiar with the scripting so I don't know how to set it up.

the code I have is:
Set fs1 = CreateObject("Scripting.FileSystemObject")
If fs1.FileExists("g:\file1a.txt") Then
fs1.CopyFile "g:\file1.txt","fileout.txt"
' this works fine, but I cannot append from here.
End If

Any help is appreciated.
The DOS command looks good, however, I'm not familiar with it, so I don't know how to set that up.
 
CopyFile overwrites files, and will not work for what you are doing.

Your Access code would look like this:

dim retval as variant

Set fs1 = CreateObject("Scripting.FileSystemObject")

If fs1.FileExists("g:\file1a.txt") Then
'concantenate two files together using a DOS command
retval=shell("copy file1.txt+file1a.txt out1.txt")
end if

You state that you keeping getting errors. We could help more if you told us what they are.



 
I tried the shell command with the DOS but got a File Not Found error.
 
I got it! I created a bat file first and shelled that.

Here's what I did.
Dim fs1,f
Dim strBatch As String
Dim strCR As String
Dim intFH As Integer

strCR = Chr$(13) & Chr$(10)

Set fs1 = CreateObject("Scripting.FileSystemObject")
If fs1.FileExists("g:\file1a.txt") Then
strBatch = "copy g:\file1.txt + g:\file1a.txt g:\fileout.txt" & strCR
intFH = FreeFile
Open "g:\copy.bat" For Output As intFH
Print #intFH, strBatch
Close intFH
f = Shell("g:\copy.bat", 6)
End If

Cribbed the idea from some code I had inherited.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top