干得好:
Option Explicit
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Sub collectData()
Dim WshShell, sPath, sMain, sName, sDesc, sPrice, sContact
' Save it to a folder on the Desktop
set WshShell = WScript.CreateObject("WScript.Shell")
sPath = WshShell.SpecialFolders("Desktop")
sPath = sPath & "\Scratch Files\"
sMain = "Bulletin.html"
' Prompt for title, description, price, and contact
sName = getInput("Item Name")
sDesc = getInput("Item Description")
sPrice = getInput("Item Price")
sContact = getInput("Contact Information")
Call createFile(sPath, sName, sDesc, sPrice, sContact)
' Add new .html file to index file in the format: date, <a href=html location>title for link</a>
Call appendFile(sPath, sMain, sName)
set WshShell = Nothing
Call Msgbox("Your item (" & sName & ") was added")
End Sub
Function getInput(prompt)
getInput = inputbox(prompt,"Add New Item for Sale")
End Function
sub createFile(sPath, sName, sDesc, sPrice, sContact)
'Creates a new file, or appends to an existing file
Dim objFSO, objArgs(19), sTextFile, objFile, i
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check if folder path exists; if not, create folder
If objFSO.FolderExists(sPath) then
Else
Call objFSO.CreateFolder(sPath)
End If
' Save file as <title of item>.html
sTextFile = sPath & sName & ".html"
' If file exists, open; else, create it
If objFSO.FileExists(sTextFile) Then
Set objFile = objFSO.OpenTextFile(sTextFile, ForAppending)
Else
Set objFile = objFSO.CreateTextFile(sTextFile)
End If
objArgs(1) = "<HTML>"
objArgs(2) = " <HEAD>"
objArgs(3) = " <TITLE>Bulletin</TITLE>"
objArgs(4) = " </HEAD>"
objArgs(5) = ""
objArgs(6) = "<BODY>"
objArgs(7) = " <CENTER><H1>Bulletin</H1></CENTER>"
objArgs(8) = "<!-Item Name-!>"
objArgs(9) = " <H1>" & sName & "</H1> "
objArgs(10) = "<!-Item Price-!>"
objArgs(11) = " <H2><U>" & sPrice & "</U></H2>"
objArgs(12) = "<!-Contact Info-!>"
objArgs(13) = " <b><Font Color='Blue'>" & sContact & "</b></font>"
objArgs(14) = " <BR /><BR /><BR />"
objArgs(15) = "<!-Item Description-!>"
objArgs(16) = " " & sDesc
objArgs(17) = "</BODY>"
objArgs(18) = "</HTML>"
' Write the details to the file
For i = 1 To UBound(objArgs)
objFile.WriteLine objArgs(i) & " "
Next
' Append a newline character
objFile.WriteLine
' Close the file
objFile.Close
set objFile = Nothing
set objFSO = Nothing
End Sub
Sub appendFile(sPath, sMain, sName)
Dim objFSO, objArgs(3), sTextFile, objFile, file, i, lBody
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check if folder path exists; if not, create folder
If objFSO.FolderExists(sPath) then
Else
Call objFSO.CreateFolder(sPath)
End If
'Create filename
sTextFile = sPath & sMain
' If file exists, open; else, create it
If objFSO.FileExists(sTextFile) Then
Set objFile = objFSO.OpenTextFile(sTextFile, ForReading)
file = Split(objFile.ReadAll(), vbCrLf)
objFile.Close()
Set objFile = objFSO.OpenTextFile(sTextFile, ForWriting)
For i = Lbound(file) to Ubound(file)
If inStr(file(i), "</BODY>") then
lBody = i
Exit For
Else
objFile.WriteLine(file(i))
End If
Next
Else
Set objFile = objFSO.CreateTextFile(sTextFile)
file(1)=""
End If
objArgs(1) = Date() & " - <A HREF=""" & sName & ".html"">" & sName & " For Sale</A>"
objArgs(2) = "<BR /><BR />"
' Write the details to the file
For i = 1 To UBound(objArgs)
objFile.WriteLine objArgs(i) & " "
Next
For i = lBody to Ubound(file)
objFile.WriteLine(file(i))
Next
' Append a newline character
objFile.WriteLine
' Close the file
objFile.Close
set objFile = Nothing
set objFSO = Nothing
End Sub
collectData()
笔记:
- 在 collectData 子中,您可以定义要保存文件的路径(当前位于桌面上的 Scratch Files 文件夹中)。您还可以定义主网页的名称(当前为 Bulletin.html)
- 任何提示都没有用户验证(InputBox 在函数 getInput 中),但可以随意添加一些。您可能希望包含一个默认值“$”,这将需要对 getInput 函数的参数进行一些重组(为默认值添加另一个参数并在 inputbox(..) 调用中包含第三个参数;请关注此页面以获取更多详细信息:http://msdn.microsoft.com/en-us/library/3yfdhzk5%28v=vs.84%29.aspx)
- 我在 createFile() sub 中模拟了您作为模板提供的 HTML 代码。如果您想更改它(即添加行),您还需要更新靠近 sub 顶部的 objArgs 变量声明。
- 主网页中的附加模板也是如此(参见#3),但它位于 appendFile() 子中。
用法:
- 要添加新内容,用户需要双击您计算机上的 *.vbs 文件。
- 提示将引导他们完成详细信息以添加他们的项目。
- 脚本完成后,它会自动为他们创建 *.html,更新您的主页,并用一个漂亮的 MsgBox 通知他们(您的项目名称已添加)。
希望这可以帮助。