vbs刪除文本文件的行的函數(shù)
來源:易賢網(wǎng) 閱讀:3109 次 日期:2016-07-20 14:49:32
溫馨提示:易賢網(wǎng)小編為您整理了“vbs刪除文本文件的行的函數(shù)”,方便廣大網(wǎng)友查閱!

有時候我們需要刪除文本文件的指定的一行內(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

更多信息請查看腳本欄目
易賢網(wǎng)手機網(wǎng)站地址:vbs刪除文本文件的行的函數(shù)
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇剩?/div>
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點

版權(quán)所有:易賢網(wǎng)