1

让我们举个例子。我有一个包含 20 个文件的文件夹。让我们假设所有文件都超过 1 mb.. 我的目标是复制前 500 kb(即占用前 500 kb 的字符串)并将其写入另一个文件。然后循环它并对 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 做到这一点?如果是怎么办???对我来说看起来真的很复杂.. .. 请帮忙

4

2 回答 2

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

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
于 2013-08-01T10:59:08.177 回答
0

使用读取打开的TextStream的.Read方法从输入文件中读取块,使用写入打开的 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
于 2013-08-01T11:00:23.810 回答