VB脚本编程之2.0——加入正则表达式查找替换功能

编程是在学习中不断进步,这个不假,这两天一直在折腾VB脚本编程,原因可以追溯到我关于VB脚本编程的第一篇文章简单的VB小脚本之——文件处理脚本,里面提到了上帝,额不,是我的客户提到的一个简单的小要求,就是希望我写个VB的脚本来处理文件,于是我就现学现用了,完成了1.0的版本。客户就是上帝,上帝说要写VB脚本,于是就有了VB脚本。这不,提交了之后,上帝说,改进一下,在命令行里实现调用,并且传参数,比如,xx.vbs -normal 22.log 66 Eric.Tang这个用来把22.log文件中的“66”字符串儿替换为“Eric.Tang”,同时呢还要加入正则表达式的验证,比如xx.vbs -regex 22.log “<.*>" Eric.Tang这个用来把22.log文件中<>括号中的内容替换为Eric.Tang,折腾了大半天之后就有了以下的VB脚本2.0版本,内容源码如下:

currentPath=CreateObject("Scripting.FileSystemObject").GetFolder(".").Path
Set objArgs = WScript.Arguments
If objArgs.count < 4 Then
	Msgbox("Missing parameters!")
Else
Set objFSO = CreateObject("Scripting.FileSystemObject")
	If objFSO.FileExists(currentPath & "\" & objArgs(1)) Then 'if file exists or not
		Set objFile=objFSO.OpenTextFile(currentPath & "\" & objArgs(1))
		str=objFile.ReadAll  'get content of the file
		objFile.close
		If objArgs(0) = "-normal" Then
			oldStr=objArgs(2) 'the old string that you want to find
			newStr=objArgs(3) 'the new string that you want to replace with
			If oldStr <> "" Then
				str=replace(str,oldStr,newStr)
				tmpArray=split(objArgs(1),".")
				Set newFile=objFSO.CreateTextFile(currentPath & "\" & tmpArray(0) & "_Normal" & ".txt")
				newFile.write(str)
				newFile.close
				If objFSO.FileExists(currentPath & "\" & tmpArray(0) & "_Normal" & ".txt") Then
					Msgbox("Normal handled file has been created successfully! Please check " & currentPath & "\" & tmpArray(0) & "_Normal" & ".txt")
				Else
					Msgbox("Errors happened while Normal-Handling!")
				End If
			End If
		ElseIf objArgs(0) = "-regex" Then
			Set regEx=New RegExp 'create a regex
			patern=objArgs(2)
			regEx.Pattern=patern
			regEx.IgnoreCase = True 'ingnore the Upper/Lower case
			regEx.Global = True     'if can be used Globally
			If str <> "" Then
				Set Matches = regEx.Execute(str)   'excute search
				If Matches.count <> 0 Then
					For Each Match in Matches
						str=replace(str,Match.Value,objArgs(3))
					Next
					tmpArray=split(objArgs(1),".")
					Set newFile=objFSO.CreateTextFile(currentPath & "\" & tmpArray(0) & "_Regex" & ".txt")
					newFile.write(str)
					newFile.close
					If objFSO.FileExists(currentPath & "\" & tmpArray(0) & "_Regex" & ".txt") Then
						Msgbox("Regex handled file has been created successfully! Please check " & currentPath & "\" & tmpArray(0) & "_Regex" & ".txt")
					Else
						Msgbox("Errors happened while Regex-Handling!")
					End If
				Else
					Msgbox("No matches found!")
				End If
			End If
		End If
	Else
		Msgbox("File not exist!")
	End If
End If
因为我是初次接触VB脚本,写的一般,仅仅是能完成要求的功能而已,可扩展性什么的就几乎没什么考虑的,所以在我看来,程序猿可能会在职业生涯中临时要去学很多东西,虽然不是你感兴趣的东西,但工作所需,我们都可能要及时学习并运用,在学习中前进,在学习中进步。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值