资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

vbscript中怎么从文本文件中删除重复行-创新互联

本篇文章为大家展示了vbscript中怎么从文本文件中删除重复行,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

成都创新互联主要从事成都网站设计、网站制作、外贸营销网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务玛曲,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792


首先,假定您有一个文本文件,其中每一行都表示一条单独的记录。这似乎不太可能,但也许您的文件类似如下:

This is one of the lines in the text file.
This is one of the lines in the text file.
This is another line in the text file.
This is one of the lines in the text file.
This is yet another line in the text file.
This is another line in the text file.
This is another line in the text file.
This is one of the lines in the text file.

您需要一个可以除去所有重复行并提供类似以下输出的脚本:

This is one of the lines in the text file.
This is another line in the text file.
This is yet another line in the text file.

SW,您找对地方了:

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

strPathToTextFile = "C:\Scripts\"
strFile = "Test.txt"

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Data Source=" & strPathtoTextFile & ";" & _
          "Extended Properties=""text;HDR=NO;FMT=Delimited"""

objRecordSet.Open "Select DISTINCT * FROM " & strFile, _
    objConnection, adOpenStatic, adLockOptimistic, adCmdText

Do Until objRecordSet.EOF
    Wscript.Echo objRecordSet.Fields.Item(0).Value   
    objRecordSet.MoveNext
Loop

我们发现此脚本有些有趣,因为我们使用“ActiveX 数据对象”(ADO) 并将此文本文件当作数据库处理。我们不会花费过多的时间详细说明如何将文本文件当作数据库处理;如果您想了解有关于此的详细信息,我们的脚本诊所 专栏对此主题进行了深入阐述。现在,要说明的只是我们将使用文本文件 C:\Scripts\Test.txt,我们通过为变量 strPathToTextFile 和 strFile 赋予相应值来表示:

strPathToTextFile = "C:\Scripts\"
strFile = "Test.txt"

那么,这如何能让我们除去重复行呢?是这样的,有一种称为 Select DISTINCT 的数据库查询;利用 Select DISTINCT 可以选择表格中所有不同的(或的)记录。假设您有一个简单的数据库,其中有以下记录:


Red
Red
Blue
Red

如果使用 Select DISTINCT 查询,您将得到一个只包括记录的记录集:

Red
Blue

毫无疑问,您会想:“哇!返回记录与删除重复记录简直异曲同工。”我们承认确实如此 – 嗯,请等一下:您的想法绝对正确。我们的文本文件构建得就像一个数据库表,文本文件中的每行都表示一条记录中的一个字段。如果对此文本文件运行 Select DISTINCT 查询,我们将只得到的行。事实上,我们将得到如下所示的记录集:

This is one of the lines in the text file.
This is another line in the text file.
This is yet another line in the text file.

这刚好就是我们希望返回的信息。您为我们指出了这一点,这很好!

检索记录集后,我们再使用以下代码将的行回显到屏幕:

Do Until objRecordset.EOF
    Wscript.Echo objRecordset.Fields.Item(0).Value   
    objRecordset.MoveNext
Loop



上述内容就是vbscript中怎么从文本文件中删除重复行,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


分享名称:vbscript中怎么从文本文件中删除重复行-创新互联
当前地址:http://cdkjz.cn/article/jhedi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220