用VBS中的FSO比较文件不同,相同部分写入新文件

应群内一位网友的需求,特写一段VBS代码,实现把两个文件的内容比较,其中相同的部分另存出来。一行一行比对。
 
ASP/Visual Basic代码
  1. Dim a,b,c,i,j,k   
  2. a = GetFile("c:\a.txt"'待比较的文件a   
  3. b = GetFile("c:\b.txt"'待比较的文件b   
  4. c = "c:\c.txt"          '相同的内容写到这里   
  5. k = 0   
  6.   
  7. For i = 0 To UBound(a)   
  8.     For j = 0 To UBound(b)   
  9.         If a(i) = b(j) Then Call AppendFile(c,a(i)) : k = k + 1 : End if   
  10.     Next  
  11. Next  
  12.   
  13.  MsgBox "共找出" & k & "行相同的记录",vbOKOnly,"haha"  
  14.   
  15.   
  16.   
  17. '=============☆★★●◎◇◆□=========清风的漂亮分隔线=============☆★★●◎◇◆□=========   
  18.   
  19. '获取总行数 ,这个不用了,放这里看看   
  20. Function GetFileNum(strPath)   
  21.     Dim i : i = 0   
  22.     Const ForReading = 1   
  23.     Set objFSO = CreateObject("Scripting.FileSystemObject")    
  24.     If Not objFSO.fileExists(strPath) Then Exit Function    
  25.     Set objTextFile = objFSO.OpenTextFile(strpath, ForReading)    
  26.     While objTextFile.AtEndOfStream = False  
  27.         objTextFile.skipline   
  28.         i=i+1   
  29.     Wend   
  30.     objTextFile.Close   
  31.     Set objFSO = Nothing  
  32.     GetFileNum = i   
  33. End Function  
  34.   
  35.   
  36. '获取文本内容,返回数组   
  37. Function GetFile(strPath)   
  38.     Dim tmpData   
  39.     Const ForReading = 1   
  40.     Set objFSO = CreateObject("Scripting.FileSystemObject")   
  41.     If Not objFSO.fileExists(strPath) Then Exit Function    
  42.     Set objTextFile = objFSO.OpenTextFile(strpath, ForReading)   
  43.     tmpData = objTextFile.ReadAll   
  44.     GetFile = Split(tmpData,Chr(13)&Chr(10))   
  45.     objTextFile.Close   
  46.     Set objFSO = Nothing  
  47. End Function  
  48.   
  49.   
  50. '在文件中添加新行   
  51. Function AppendFile(strPath,Linecontent)   
  52.     Dim fso,f   
  53.     Set fso = CreateObject("Scripting.FileSystemObject")   
  54.     set f = fso.opentextfile(strPath,8,1)   
  55.     f.write Linecontent&Chr(13)&Chr(10)   
  56.     f.close   
  57.     set f = Nothing  
  58. End Function  

 

« office软件中的无法打开修补程序包 修补程序包是否存在?华擎775I65G主板会自检两次启动 »

最新评论及回复

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

网站分类

最近发表

最新评论及回复

Powered By Z-Blog 1.8 Arwen Build 90619

Copyright © 2005 - 2008 风易博客 1982Y.Net.Cn All Rights Reserved.  风易在线提供优质主机空间
沪ICP备05006417号