Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

vbscript - VB script + read files (only files with "log" name) and copy content files into one file.txt

I have the following different files under C:logs3

log1.txt
log2.txt
log3.txt

how to create with VB script , relevant code that create one file called:

log.general.txt 

this VB script need to read all files content under log3 directory that have the name - log

as:  log1.txt  log2.test.txt log3.start.txt  mylog.txt  big.log.txt

But: not read the files as: test.txt  or ABBA.txt or CAR.txt

And copy the content of those files in to one file - log.general.txt - "also under log3 dir"

* remark in case the file is empty then need to escape (continue to the next files)

for example

log1.txt:

111111
222222

log2.txt:

333333
444444


log3.txt

555555
666666

file.test.txt

777777
888888


 log.general.txt content: -> (Expected results from the VB script)

 111111
 222222
 333333
 444444
 555555
 666666

other example from linux: (to in order to explain my question)

cat log1.txt >> log.general.txt  
cat log2.txt >> log.general.txt
cat log3.txt >> log.general.txt 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

@jon: Here's another solution in VBScript

Option Explicit

Const ForAppending = 8
Const ForReading   = 1
Const ForWriting   = 2

Dim oFS, oFolder, oFile, oRead, oWrite, oRegEx
Dim sContents, sDir, sFile
Dim iMatch

sDir  = "C:log3"
sFile = "log.general.txt"

Set oFS = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFS.GetFolder(sDir)
Set oRegEx = New RegExp
With oRegEx
    .Pattern    = "log"
    .IgnoreCase = True
    .Global     = True
End With

For Each oFile In oFolder.Files
    iMatch = oRegex.Test(oFile.Name)

    If oFile.Name <> sFile AND iMatch = -1 Then
        Set oRead = oFS.OpenTextFile(oFile, ForReading)
        sContents = sContents & oRead.ReadAll & vbCrLf
        oRead.Close
    End If
Next

Set oWrite = oFS.OpenTextFile(sDir & sFile, ForAppending, True)
oWrite.WriteLine sContents
oWrite.Close

Set oRegEx = Nothing
Set oFS = Nothing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...