艾瑞克的 Hexo 空間

[筆記] ASP在檔案中找出特定的文字,在之後寫入新的字串

本文發表於1504天之前,文章內容可能已經過時,如有疑問,請聯繫作者。

Reference URL:http://forums.aspfree.com/code-bank-54/classic-asp-vbscript-write-into-specific-line-text-file-92390.html

Classic ASP/vbscript: write into specific line in text file



The code below get file path and string to be written, andwrites the string into the specified file, in the given linenumber.The text is “pushed” in that line, pushing allother lines below.If the text file does not exist, it’s being created and thetext written as the first line.If the line number exceeds the amount of lines in the file,the text is written as the last line.Happy Programming!



>
Code:

>
>
Sub WriteToFile(strFilePath, strData, iLineNumber)
>
> Dim objFSO, objFile, arrLines
>
> Dim strAllFile, x
>
> Set objFSO=Server.CreateObject(“Scripting.FileSystemObject”)
>
> strAllFile=””
>
> If objFSO.FileExists(strFilePath) Then
>
> Set objFile=objFSO.OpenTextFile(strFilePath)
>
> If Not(objFile.AtEndOfStream) Then
>
> strAllFile = objFile.ReadAll
>
> End If
>
> objFile.Close
>
> End If
>
> arrLines = Split(strAllFile, VBCrLf)
>
> Set objFile=objFSO.CreateTextFile(strFilePath)
>
> For x=0 To UBound(arrLines)
>
> If (iLineNumber-1)=x Then objFile.WriteLine(strData)
>
> objFile.WriteLine(arrLines(x))
>
> Next
>
> If iLineNumber>=UBound(arrLines) Then objFile.WriteLine(strData)
>
> objFile.Close
>
> Set objFile=Nothing




avatar
[筆記] AeroFS - 又一種跨平台同步軟體

  1. 1. Classic ASP/vbscript: write into specific line in text file