让我们举个例子。我有一个包含20个文件的文件夹。假设所有文件都超过1 mb ..我的目标是先复制500 kb(即占用前500kb的字符串)并将其写入另一个文件。然后循环它并为20个文件中的每个文件执行相同的操作。文件的写入必须在20个单独的文件中进行。
例如我有
1.doc1.txt
2.doc2.txt
3.doc.exe
4.doc.jpg
so on
我想要
first 500kb of doc1.txt to be saved in dup1.txt,
first 500kb of doc2.txt in dup2.txt and so on.
是否可以使用vbscript执行此操作?如果是的如何???看起来真的很复杂.. ..请帮忙
答案 0 :(得分:2)
您可以使用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
输入文件可以使用2个Stream
对象分成几个块,如下所示:
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
答案 1 :(得分:0)
使用读取打开的.Read的TextStream方法读取输入文件中的块和写入打开的TextStream的.Write方法,将其写入输出文件
示例代码:
>> Dim tsIn : Set tsIn = goFS.OpenTextFile("00.vbs")
>> Dim tsOut : Set tsOut = goFS.CreateTextFile("00.head")
>> tsOut.Write tsIn.Read(10)
>> tsIn.Close
>> tsOut.Close
>> WScript.Echo goFS.OpenTextFile("00.head").ReadAll()
>>
Option Exp