vbs刪除文本文件的行的函數(shù)
更新時(shí)間:2008年10月24日 00:57:24 作者:
有時(shí)候我們需要?jiǎng)h除文本文件的指定的一行內(nèi)容,下面是具體的函數(shù)
Delete Line Function
Function DeleteLine(strFile, strKey, LineNumber, CheckCase)
'DeleteLine Function by TomRiddle 2008
'Remove line(s) containing text (strKey) from text file (strFile)
'or
'Remove line number from text file (strFile)
'or
'Remove line number if containing text (strKey) from text file (strFile)
'Use strFile = "c:\file.txt" (Full path to text file)
'Use strKey = "John Doe" (Lines containing this text string to be deleted)
'Use strKey = "" (To not use keyword search)
'Use LineNumber = "1" (Enter specific line number to delete)
'Use LineNumber = "0" (To ignore line numbers)
'Use CheckCase = "1" (For case sensitive search )
'Use CheckCase = "0" (To ignore upper/lower case characters)
Const ForReading=1:Const ForWriting=2
Dim objFSO,objFile,Count,strLine,strLineCase,strNewFile
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.OpenTextFile(strFile,ForReading)
Do Until objFile.AtEndOfStream
strLine=objFile.Readline
If CheckCase=0 then strLineCase=ucase(strLine):strKey=ucase(strKey)
If LineNumber=objFile.Line-1 or LineNumber=0 then
If instr(strLine,strKey) or instr(strLineCase,strkey) or strKey="" then
strNewFile=strNewFile
Else
strNewFile=strNewFile&strLine&vbcrlf
End If
Else
strNewFile=strNewFile&strLine&vbcrlf
End If
Loop
objFile.Close
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.OpenTextFile(strFile,ForWriting)
objFile.Write strNewFile
objFile.Close
End Function
使用方法:
DeleteLine "c:\1.txt", "", 1, 0
復(fù)制代碼 代碼如下:
Function DeleteLine(strFile, strKey, LineNumber, CheckCase)
'DeleteLine Function by TomRiddle 2008
'Remove line(s) containing text (strKey) from text file (strFile)
'or
'Remove line number from text file (strFile)
'or
'Remove line number if containing text (strKey) from text file (strFile)
'Use strFile = "c:\file.txt" (Full path to text file)
'Use strKey = "John Doe" (Lines containing this text string to be deleted)
'Use strKey = "" (To not use keyword search)
'Use LineNumber = "1" (Enter specific line number to delete)
'Use LineNumber = "0" (To ignore line numbers)
'Use CheckCase = "1" (For case sensitive search )
'Use CheckCase = "0" (To ignore upper/lower case characters)
Const ForReading=1:Const ForWriting=2
Dim objFSO,objFile,Count,strLine,strLineCase,strNewFile
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.OpenTextFile(strFile,ForReading)
Do Until objFile.AtEndOfStream
strLine=objFile.Readline
If CheckCase=0 then strLineCase=ucase(strLine):strKey=ucase(strKey)
If LineNumber=objFile.Line-1 or LineNumber=0 then
If instr(strLine,strKey) or instr(strLineCase,strkey) or strKey="" then
strNewFile=strNewFile
Else
strNewFile=strNewFile&strLine&vbcrlf
End If
Else
strNewFile=strNewFile&strLine&vbcrlf
End If
Loop
objFile.Close
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.OpenTextFile(strFile,ForWriting)
objFile.Write strNewFile
objFile.Close
End Function
使用方法:
DeleteLine "c:\1.txt", "", 1, 0
相關(guān)文章
通過vbs修改目錄名稱與快捷方式名稱的實(shí)現(xiàn)代碼
這篇文章主要是分享了如果用vbs修改目錄名與快捷方式名稱的代碼,需要的朋友可以參考下2013-07-07VBS讀取配置文件配置項(xiàng)的實(shí)現(xiàn)代碼
今天寫代碼的時(shí)候需要用到vbs獲取文件配置項(xiàng),不用放數(shù)據(jù)庫里面,要不增刪改查都要做一篇,本身功能就簡單,這里就為大家分享一下實(shí)現(xiàn)代碼,需要的朋友可以參考下2019-09-09VBS教程:函數(shù)-CreateObject 函數(shù)
CreateObject 函數(shù)創(chuàng)建并返回對(duì) Automation 對(duì)象的引用,在vbscirpt經(jīng)常用到,這里就為大家簡單介紹一下需要的朋友可以參考一下啊2006-11-11VBS教程:函數(shù)-派生數(shù)學(xué)函數(shù)
VBS教程:函數(shù)-派生數(shù)學(xué)函數(shù)...2006-11-11Rcmd.vbs [Remote Cmd with wmi]遠(yuǎn)程腳本
此腳本支持回顯2009-07-07