[Vbs]_[过滤重复关键词]

本文介绍了一种使用VBS脚本快速从CSV文件中过滤重复关键词的方法,并提供了完整的脚本示例。该脚本能够读取指定的CSV文件,去除重复的关键词,并将结果保存到新的CSV文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


场景:

1.有些关键词以excel表格的形式存储,但是某列的关键词可能重复了,需要过滤掉它,如何做更快呢?用excel的vbs编程是一种方案。这里用我的是独立的vbs脚本。首先存为.csv格式,excel的另一种文本编辑模式,当然任意脚本语言都能做到,这里用vbs只是为了对vbs函数和语法有进一步认识。。


过滤关键词.vbs (ANSI编码)

' Author: sai
' The include sub is used to import some files like java.
Sub include(file)
 Set objFS = CreateObject("Scripting.FileSystemObject")
 Set logFile=objFS.OpenTextFile(file,1,false,TristateFalse)
 Dim all
 all=logFile.readAll
 logFile.close
 ExecuteGlobal all
End Sub
' Begin

Dim currentDirectory,fileName
Set WshShell = WScript.CreateObject("WScript.Shell")

Set objFS = CreateObject("Scripting.FileSystemObject")
currentDirectory = WshShell.CurrentDirectory
fileName=InputBox("请输入文件名:"&vbCr&vbCr&" ")
Dim currentFile
currentFile = currentDirectory & "/" & fileName

If Not objFS.FileExists(currentFile) Then
   Wscript.echo "文件不存在: " & currentFile & "  " & "已退出"
   WScript.Quit(1)
End If

Wscript.echo "过滤开始"
Set csvFile=objFS.OpenTextFile(currentFile,1,false,TristateFalse)

Dim today,newFileName, newFolderPath, newFilePath
today = DatePart("yyyy",Date) & DatePart("m",Date) & DatePart("d",Date)
newFileName = today & "_" & fileName
newFolderPath = currentDirectory & "/" & today
newFilePath = newFolderPath & "/" & newFileName

'1.创建新文件夹,复制Excel文件
If Not objFS.FolderExists(newFolderPath) Then 
	objFS.CreateFolder(newFolderPath)
End If

'创建以ANSI编码的文件,这样Excel打开CSV时能正确识别逗号
Set txtFile=objFS.CreateTextFile(newFilePath,True,False)

'是否记录日志
Set logFile=objFS.CreateTextFile(newFolderPath & "/" & "delete_" & newFileName,True,False)

Dim line,tempFix,keyword
Set map = CreateObject("Scripting.Dictionary")

While csvFile.AtEndOfStream <> True
	line = csvFile.readline()
	tempFix = Split(line,",",-1,1)
	keyword = tempFix(0)
	If map.Exists(keyword) Then
	   logFile.WriteLine(line)
	Else
	   map.Add keyword,line
	   txtFile.WriteLine(line)
	End If
	
Wend

csvFile.Close()
txtFile.Close()
logFile.Close()
Wscript.echo "过滤结束"

原文件.csv

第一列是关键词.

我们,大家
大像,1
我们,23
刘,ad
哦,asdf

过滤后的.csv

我们,大家
大像,1
刘,ad
哦,asdf



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白行微

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值