应群内一位网友的需求,特写一段VBS代码,实现把两个文件的内容比较,其中相同的部分另存出来。一行一行比对。
ASP/Visual Basic代码
- Dim a,b,c,i,j,k
- a = GetFile("c:\a.txt") '待比较的文件a
- b = GetFile("c:\b.txt") '待比较的文件b
- c = "c:\c.txt" '相同的内容写到这里
- k = 0
- For i = 0 To UBound(a)
- For j = 0 To UBound(b)
- If a(i) = b(j) Then Call AppendFile(c,a(i)) : k = k + 1 : End if
- Next
- Next
- MsgBox "共找出" & k & "行相同的记录",vbOKOnly,"haha"
- '=============☆★★●◎◇◆□=========清风的漂亮分隔线=============☆★★●◎◇◆□=========
- '获取总行数 ,这个不用了,放这里看看
- Function GetFileNum(strPath)
- Dim i : i = 0
- Const ForReading = 1
- Set objFSO = CreateObject("Scripting.FileSystemObject")
- If Not objFSO.fileExists(strPath) Then Exit Function
- Set objTextFile = objFSO.OpenTextFile(strpath, ForReading)
- While objTextFile.AtEndOfStream = False
- objTextFile.skipline
- i=i+1
- Wend
- objTextFile.Close
- Set objFSO = Nothing
- GetFileNum = i
- End Function
- '获取文本内容,返回数组
- Function GetFile(strPath)
- Dim tmpData
- Const ForReading = 1
- Set objFSO = CreateObject("Scripting.FileSystemObject")
- If Not objFSO.fileExists(strPath) Then Exit Function
- Set objTextFile = objFSO.OpenTextFile(strpath, ForReading)
- tmpData = objTextFile.ReadAll
- GetFile = Split(tmpData,Chr(13)&Chr(10))
- objTextFile.Close
- Set objFSO = Nothing
- End Function
- '在文件中添加新行
- Function AppendFile(strPath,Linecontent)
- Dim fso,f
- Set fso = CreateObject("Scripting.FileSystemObject")
- set f = fso.opentextfile(strPath,8,1)
- f.write Linecontent&Chr(13)&Chr(10)
- f.close
- set f = Nothing
- End Function