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

Append files together 1

Status
Not open for further replies.

Alan0568

Technical User
May 24, 2006
77
GB
Hello,

Using 2005/.NET Framework 2.0 I want to append complete files together. Simple with dos or unix but I'm not sure of the best way to do this in VB.

For example I want to copy c:\dir1\filename1 to c:\dir2\filename1. If the file does not exist in dir2 it would be a direct copy but if the file does exist I want one file which is the two files appended together. The files always have the same names.

I have looked at My.Computer.FileSystem.WriteAllText but it appears its designed to append text from variables to the end of a file. I have also read about using this and reading the entire contents into a string and appending but surely there is a better way as my files will be quite big.

The copy / move methods just have overwrite true/false so I'm a little stumped.

If have no problem looping through directories existance checking, I just need a handle on the best method to use.

Thanks in advance.

A
 
Use a StreamReader to read your file one line at a time, and write to the other file one line at a time.
 
Thanks for the suggestion. I was looking for a more simple approach as the files are flat file extract from a legacy system and can be up to 1gb in size. I guess I'll give it a go and so what the performance is like.

I can see me just droping to cmd and using 'copy /A file1 + file2' like when I were a lad.

A
 
So use .net to run the copy command from a command line. Take a look at the Process class. You can use it to start the command processor and do your file copying. I use this to copy and zip a number of folders/files.
 
I have not really delved into .NET all that much and am a VB6 programmer but I am pretty sure that the FileSystemObject is still available and you could do something like this:

Code:
fso.OpenTextFile(tarFile, ForAppending, True).Write (fso.OpenTextFile(InFile).ReadAll)

I would think that StreamReader/StreamWriter would have something very close to this.


Swi
 
Many thanks all. When I said I'd revert to cmd I had intended to find out how from .net so thanks PRPhx for the pointer to Process.Start which saved me time. I have used this and it works a treat.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top