2015-03-02 106 views
0

我已經進行了一些基本的編程,但從未將任何輸入保存到文本文件。如果任何人可以請幫助我,我將不勝感激。vbscript中的Msgbox輸入保存/添加到文本文件

我已經得到腳本運行在網頁上,它的工作原理,但現在我需要將信息保存到文本文件。我希望它在評論中的代碼末尾顯示的格式。

感謝

<script type="text/vbscript"> 
<!-- 
Option Explicit 

DIM newbook, title, author, startpage, stoppage, timeread, dateread 
DIM pagesread, pagesperminute 

'input 
newbook = inputbox("Is this the first time reading this book? y -or- n") 

'decision 
if newbook = "y" then 
     title = inputbox("What is the book title?") 
     author = inputbox("Who wrote the book?") 
     startpage = 1 
else 
     title = "******" 
     author = "******" 
     startpage = inputbox("What page did you start reading on") 
end if 

'input 
stoppage = inputbox("What page did you stop reading on?") 
timeread = inputbox("How long did you read?") 
dateread = inputbox("What was the date you read on? MM/DD") 

'calculation 
pagesread = stoppage - startpage 
pagesperminute = pagesread/timeread 

'output 
document.write "Date Read: " &(dateread) 
document.write "<br>Book Title: " &(title) 
document.write "<br>Author: " &(author) 
document.write "<br>Pages Read: " &(startpage) 
document.write " - " &(stoppage) 
document.write "<br>Time Read: " &(timeread) 
document.write "<br>Pages Read Per Minute: " &(pagesperminute) 



'output to text log 

進一步研究我想出了這個,它創建的文件或附加額外的數據文件幾乎像我想要的。我想改變的唯一事情是這樣的: filetxt.WriteLine(「的頁面閱讀:‘)&起始頁 filetxt.WriteLine(’ - 」)&停止 這些我寧願格式類似於在同一行: filetxt .WriteLine(「Pages Read:」)& startpage(「 - 」)& stoppage 產生於: 頁面閱讀:1 - 22 目前我無法弄清楚如何在不出錯的情況下完成此操作。

Const ForReading = 1, ForWriting = 2, ForAppending = 8 
Set filesys = CreateObject("Scripting.FileSystemObject") 
Set filetxt = filesys.OpenTextFile("readlog.txt", ForAppending, True) 
path = filesys.GetAbsolutePathName("C:\readlog\readlog.txt") 
getname = filesys.GetFileName(path) 

filetxt.WriteLine ("") 
filetxt.WriteLine ("Date Read: ") &dateread 
filetxt.WriteLine ("Book Title: ") &title 
filetxt.WriteLine ("Author: ") &author 
filetxt.WriteLine ("Pages Read: ") &startpage 
filetxt.WriteLine (" - ") &stoppage 
filetxt.WriteLine ("Time Read: ") &timeread 
filetxt.WriteLine ("Pages Read Per Minute: ") &pagesperminute 



' --> 
</script> 

回答

1

這裏是我寫的日誌到一個文本文件中的函數,你可能會發現它有用:

http://www.naterice.com/blog/template_permalink.asp?id=43

'---------LogToFile Configuration--------- 
'NOTE: Copy the configuration section To 
'the beginning of an existing script. The 
'values specified here must be set before 
'calling the LogToFile sub. 

'You can disable logging globally by 
'setting the bEnableLogging option to false. 
bEnableLogging = True 

'Setting this to true will time stamp Each 
'message that is logged to the log file 
'with the current date and time. 
bIncludeDateStamp = True 

'This will set the log file name to the 
'current date and time. You can use this 
'option to create incremental log files. 
bPrependDateStampInLogFileName = False 

'Specify the log file location here. Path 
'must contain a trailing backslash. If you 
'would like to log to the same location as 
'the currently running script, set this 
'value to "relative" or uncomment out the 
'line below. 
'sLogFileLocation = "C:\LogFiles\" 
sLogFileLocation = "relative" 

'Specify the log file name here. 
sLogFileName = "logtofiletest.txt" 

'You can set whether or not you would like 
'the script to append to an existing file, 
'or if you would like it to overwrite 
'existing copies. To overwrite set the 
'sOverWriteORAppend variable to "overwrite" 
sOverWriteORAppend = "append" 

'Here you can set the maximum number of 
'lines you like to record. If the maximum 
'is reached the beginning of the log file 
'will be pruned. Setting this to a value 
'of 0 will disable this function. 
vLogMaximumLines = 0 

'This is just like limiting the log file 
'to a number of lines but limits by the 
'total size of the log file. This value 
'is in bytes. Setting this to 0 will 
'disable this function. 
vLogMaximumSize = 0 
'-------END LogToFile Configuration------- 

Sub LogToFile(Message) 
    'LogToFile.vbs 10-18-07 
    'This script is provided under the Creative Commons license located 
    'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 
    'be used for commercial purposes with out the expressed written consent 
    'of NateRice.com 

    If bEnableLogging = False Then Exit Sub 

    Const ForReading = 1 
    Const ForWriting = 2 
    Const ForAppending = 8 

    Set oLogFSO = CreateObject("Scripting.FileSystemObject") 

    If sLogFileLocation = "relative" Then 
     Set oLogShell = CreateObject("Wscript.Shell") 
     sLogFileLocation = oLogShell.CurrentDirectory & "\" 
     Set oLogShell = Nothing 
    End If 

    If bPrependDateStampInLogFileName Then 
     sNow = Replace(Replace(Now(),"/","-"),":",".") 
     sLogFileName = sNow & " - " & sLogFileName 
     bPrependDateStampInLogFileName = False  
    End If 

    sLogFile = sLogFileLocation & sLogFileName 

    If sOverWriteORAppend = "overwrite" Then 
     Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForWriting, True) 
     sOverWriteORAppend = "append" 
    Else 
     Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForAppending, True) 
    End If 

    If bIncludeDateStamp Then 
     Message = Now & " " & Message 
    End If 

    oLogFile.WriteLine(Message) 
    oLogFile.Close 

    If vLogMaximumLines > 0 Then 
     Set oReadLogFile = oLogFSO.OpenTextFile(sLogFile, ForReading, True) 
     sFileContents = oReadLogFile.ReadAll 
     aFileContents = Split(sFileContents, vbCRLF) 
     If Ubound(aFileContents) > vLogMaximumLines Then 
     sFileContents = Replace(sFileContents, aFileContents(0) & _ 
     vbCRLF, "", 1, Len(aFileContents(0) & vbCRLF)) 
     Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForWriting, True) 
     oLogFile.Write(sFileContents) 
     oLogFile.Close 
     End If 
     oReadLogFile.Close 
    End If 

    If vLogMaximumSize > 0 Then 
     Set oReadLogFile = oLogFSO.OpenTextFile(sLogFile, ForReading, True) 
     sFileContents = oReadLogFile.ReadAll 
     oReadLogFile.Close 
     sFileContents = RightB(sFileContents, (vLogMaximumSize*2)) 
     Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForWriting, True) 
     oLogFile.Write(sFileContents) 
     oLogFIle.Close 
    End If 

    oLogFSO = Null 
End Sub 

這裏是你如何讓您登錄後,你想要什麼設置所有的配置選項:

LogToFile vbCRLF &_ 
"Date Read: " & dateread & vbCRLF &_ 
"Book Title: " & title & vbCRLF &_ 
"Author: " & author & vbCRLF &_ 
"Pages Read: " & startpage & vbCRLF &_ 
" - " & stoppage & vbCRLF &_ 
"Time Read: " & timeread & vbCRLF &_ 
"Pages Read Per Minute: " & pagesperminute 
+0

感謝彌敦道,我想知道除了我以外是否有人在看這個。你碰巧知道如何做格式化,所以它會像我想要的那樣保存到日誌文件中? – 2015-03-05 15:34:47

+0

我更新了我的答案。 – 2015-03-05 17:14:55

相關問題