您可以ADODB.Stream
为此使用一个对象:
Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = 1 'binary
stream.LoadFromFile "doc1.txt"
chunk = stream.Read(512000)
stream.Close
stream.Open
stream.Type = 1 'binary
stream.Write chunk
stream.SaveToFile "dup1.txt", 2
stream.Close
Stream
一个输入文件可以使用 2 个对象分成几个块,如下所示:
Set iStream = CreateObject("ADODB.Stream")
Set oStream = CreateObject("ADODB.Stream")
iStream.Open
iStream.Type = 1 'binary
iStream.LoadFromFile "doc1.txt"
oStream.Open
oStream.Type = 1 'binary
oStream.Write iStream.Read(512000)
oStream.SaveToFile "dup.txt", 2
oStream.Close
oStream.Open
oStream.Type = 1 'binary
oStream.Write iStream.Read(512000)
oStream.SaveToFile "dup1.txt", 2
oStream.Close
oStream.Open
oStream.Type = 1 'binary
oStream.Write iStream.Read(512000)
oStream.SaveToFile "dup2.txt", 2
oStream.Close
...
iStream.Close
这可以通过在过程或函数中包装重复的代码段来简化:
Const chunksize = 512000
Sub WriteChunk(data, filename)
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1 'binary
oStream.Write data
oStream.SaveToFile filename, 2
oStream.Close
End Sub
Set iStream = CreateObject("ADODB.Stream")
iStream.Open
iStream.Type = 1 'binary
iStream.LoadFromFile "doc1.txt"
WriteChunk iStream.Read(chunksize), "dup.txt"
WriteChunk iStream.Read(chunksize), "dup1.txt"
WriteChunk iStream.Read(chunksize), "dup2.txt"
...
iStream.Close