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

2013-11-01 工作筆記

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

> >
comments powered by Disqus
© 2017 by Lednerb
Bilberry Hugo Theme