欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vbs删除文本文件的行的函数

程序员文章站 2022-11-14 10:31:36
delete line function复制代码 代码如下:function deleteline(strfile, strkey, linenumber, checkca...
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