从文本文件中读取然后在同一个经典ASP脚本中写入文本文件

时间:2016-05-20 00:32:40

标签: asp-classic

<%
    dim narrationCounter,logDateString,logDate,      logTime,folderName,folderPath,logFolder2,month,day,year,txtFilePath


    logDateString = Request.Form("logDateString")
    logDate = Date()
    folderName = "NarrationClickLog" & "_" & logDate
    month = DatePart("m",Now())
    day = DatePart("d", Now())
    year = DatePart("yyyy",Now())


    folderPath = "D:\iisroot\casecomments\CaseNarrationClickLog" + "_" & month & "_" & day & "_" & year
    txtFilePath = folderPath + "\CaseNarrationClickLog.txt"

    visitorIP=Request.ServerVariables("REMOTE_ADDR")
    visitorUserName = Request.ServerVariables("REMOTE_USER")
    visitorHost = Request.ServerVariables("remote_host")

    set oFs = server.createobject("Scripting.FileSystemObject")

    If  Not oFs.FolderExists(folderPath) Then      


        set logFolder=oFs.CreateFolder(folderPath)

    End If

    ' Extract last updated narration button click value from text file

    if oFs.FileExists(txtFilePath) then
                if oFs.GetFile(txtFilePath).size <> 0 then  'Get narrationCounterVal from log file
                  ' open the file and Read it
                    set oTextFile = oFs.OpenTextFile(txtFilePath, 1 , true) 'forreading
                    ReadMe = oTextFile.ReadAll
                    Tab = split(ReadMe,vbcrlf)

                    i = ubound(Tab) - 2
                    paragraph=paragraph & Tab(i) & ""
                    Response.Write("Paragraph is : " + paragraph)

                    paragraphReverse = StrReverse(paragraph)
                    Response.Write( paragraphReverse )

                    ' get the narration counter
                    narrationCounterPos = Split(paragraphReverse) 
                    'Default delimiter is the space character..in this case Split at the Space before colon SO first element in array is narration counter number 
                    narrationCounterVal = StrReverse(narrationCounterPos(0))
                    'Response.Write("Latest Narration Counter from text file is :-  " + narrationCounterVal)

                    narrationCounter = narrationCounterVal +  1

                    oTextFile.Close
                    set oTextFile = nothing

                    WriteToLogFile(narrationCounter)    

                else  ' Set narration Counter Value  = 1 , A NEW FIRST Record entry.

                                narrationCounter = 1
                                WriteToLogFile(narrationCounter)

                end if

                Response.Write("Latest Narration Counter value :-  " + narrationCounterVal)

                Response.Write("file exists")

    else

        Response.Write("File does not exist")
    end if

    function WriteToLogFile(narrationCounter)

                set oTextFileWrite = oFs.OpenTextFile(txtFilePath, 8, True)   '  "D:\IISROOT\CaseComments\CaseNarrationClickLog.txt"

                oTextFileWrite.WriteLine " User IP Address: " + visitorIP  +  " User Name: " + visitorUserName + "  Clicks on DATE : " +  logDateString + " are : " + narrationCounter

                Response.Write("Text file path : -" + txtFilePath & " visitor IP :  " & visitorIP & " User name: " & visitorUserName & " visitor host: " & visitorHost)

                oTextFileWrite.Close
                set oTextFileWrite = nothing


    end function

    oTextFile.Close
    set oTextFile = nothing
    oTextFileWrite.Close
    set oTextFileWrite = nothing
    set oFS = nothing


    %>

此代码必须执行以下操作:

  1. 检查文件夹是否存在,如果不存在,请创建

  2. 检查文本文件是否存在,如果是,则以上述方式从文本文件中读取内容,提取narrationCounterVal变量值,然后使用WritetoLogFile(narrationCounter)函数将其写入文本文件

  3. 问题:卡住了。文件夹和文件被创建但没有任何内容被写入。我也确保在调用Writetologfile函数之前关闭用于在Read模式下打开文件的oTextFile对象。

0 个答案:

没有答案