Merge files

In the old dos days you could combine 2 files as save to a third file:
copy file1 + file2 file3
It still works in a dos shell.
I did this:
Shell("copy " & file1 & " + " & file2 & " " & file3)

I got an error that file3 cannot be found. I know it doesn't exist I want to create it. So I created a bat file:
copy %1 + %2 %3
and called it as such:
Shell("ccopy " & file1 & " " & file2 & " " & file3)
I still get the error that file3 cannot be found.
First I don't see how it can tell from the shell that file3 is a file.
How do I do this otherwise?
Do I have to create a blank file before calling it?

[685 byte] By [WayneSpangler] at [2007-12-16]
# 1
It was not finding the ccopy.bat without the full path. Solves the file not found.
However, it will only copy the first file to the third file using either of the two shell commands. In dos the copy copies file1 and file2 to file3. In the shell command it doesn't.
WayneSpangler at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Here's the reason:

The copy command is an internal DOS command and can't be directly executed because there's no file named copy.exe or copy.com...

Call Shell("cmd /c Copy " & file1 & " + " & file2 & " " & file3,vbHide)

this will work for Windows Xp and copies the two files.

Note that: Command copies only the first file and doesn't copy the others , really i don't know why but cmd.exe does the whole task.

the argument /c executes a Dos Internal Command then terminates.

To know more about copy just open cmd.exe then type copy /?

Best regards,

Amr Ouf

AmrOuf at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

Use system.diagnostic.process.start and not shell.

Its a lot more flexible and a .net managed code class.

Process.Start("cmd", "/c Copy c:\test1.txt + c:\test2.txt c:\test3.txt")

spotty at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

You'll need to read both files in, combine them, and then write them back. An example might be:

Private Sub MergeFiles(ByVal file1 As String, ByVal file2 As String, ByVal outputfile As String)
FileIO.FileSystem.WriteAllText(outputfile, FileIO.FileSystem.ReadAllText(file1), True)
FileIO.FileSystem.WriteAllText(outputfile, FileIO.FileSystem.ReadAllText(file2), True)
End Sub

rkimble at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5

I think to use a .NET code is better than using shell command, because this wouldn't work in windows 98 as there's no cmd.exe!!!!

I think if you use a code like that whar rkimble written but in a binary format will be better.

AmrOuf at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

Here's a function that can append two files in binary mode you can do two calls to this function to append 2 files on a single file.

for example:

Call AppendFile("c:\1.txt", "C:\3.txt")

Call AppendFile("c:\2.ini", "C:\3.txt")

Private Sub AppendFile(ByVal strSource As String, ByVal strDest As String)

Const intBUFFER_SIZE = 4096 '4KB buffer

Dim stmReader As System.IO.FileStream = System.IO.File.OpenRead(strSource)

Dim stmWriter As System.IO.FileStream = System.IO.File.OpenWrite(strDest)

Dim abytBuffer(0 To intBUFFER_SIZE - 1) As Byte

Dim intBufferSize As Integer

stmWriter.Seek(0, IO.SeekOrigin.End) 'Seek to end of file to append

Do

intBufferSize = stmReader.Read(abytBuffer, 0, intBUFFER_SIZE) 'Read

If intBufferSize = 0 Then Exit Do 'End of file

stmWriter.Write(abytBuffer, 0, intBufferSize) 'Write

Loop

stmReader.Close()

stmReader.Dispose()

stmWriter.Close()

stmWriter.Dispose()

End Sub

Hope this can help!

Best Regards,

AmrOuf at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
The above function is more flexible and relibale.

Using external command to run is an operating system dependant and there's no guarantee for the file is generated or written successfull, so i recommend to use this funciton instead.

AmrOuf at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic General...