2

我对 vbs 不是很熟悉,但我在批处理文件方面做得不错。我正在为我的工作地点创建一个简单的网站,作为销售和事物的公告。基本上我有一个文本文件,它是待售新商品的模板。我有类似 <-!Insert Item Name Here!-> 之类的东西,以方便我们办公室的非技术人员使用。我正在寻找一个脚本来编辑主 HTML 文件以将新的 .html 文件添加到文件夹中,并使用模板创建一个新的 .html 文件,并提示选项(价格、描述、部门等)

我知道这听起来很多,但我希望它可以轻松完成。

基本上,我需要它:

- 提示更改标题、描述、价格和联系方式 - 将此文件保存为项目的标题 .html - 将新的 .html 文件添加到索引文件中,格式为日期、html 位置、链接标题。

到目前为止,这是我的 index.html,只是为了让您了解这个网站的简单程度......

<HTML>
    <HEAD>

        <TITLE>Bulletin</TITLE>
    </HEAD>

<BODY>
    <CENTER><IMG SRC="logo.jpg">
    <CENTER><IMG SRC="bulletinboardbest.jpg" width=200 height=150></CENTER>
<H1>Bulletin Board</H1>
<font color="666666">To add an item please email <a href="mailto:email@email.com">email@email.com</a></font></CENTER>

<HR>



<!-Insert Items Below-!>

3/25/2013 - <A HREF="1999malibu.html">1999 Chevrolet Malibu For Sale</A>
<BR><BR>

3/28/2013 - <A HREF="orangescrewdriver.html">Orange Screw Driver For Sale</A>

<BR><BR><BR><BR><BR><BR>



</BODY>
</HTML>

项目网站模板

<HTML>
    <HEAD>
        <TITLE>Bulletin</TITLE>
    </HEAD>

<BODY>
    <CENTER><H1>Bulletin</H1></CENTER>






<!-Item Name-!>
    <H1>!!ITEM NAME HERE!!</H1> 





<!-Item Price-!>
    <H2><U>!!ITEM PRICE HERE!!</U></H2>





<!-Contact Info-!>
    <b><Font Color="Blue">!!CONTACT INFO HERE!!</b></font>
        <BR><BR><BR>






<!-Item Description-!>
    !!DESCRIPTION HERE!!



</BODY>
</HTML>

希望这不是太难......试图找出最简单的方法来为非编码类型的人做到这一点。

4

1 回答 1

1

干得好:

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()

笔记:

  1. 在 collectData 子中,您可以定义要保存文件的路径(当前位于桌面上的 Scratch Files 文件夹中)。您还可以定义主网页的名称(当前为 Bulletin.html)
  2. 任何提示都没有用户验证(InputBox 在函数 getInput 中),但可以随意添加一些。您可能希望包含一个默认值“$”,这将需要对 getInput 函数的参数进行一些重组(为默认值添加另一个参数并在 inputbox(..) 调用中包含第三个参数;请关注此页面以获取更多详细信息:http://msdn.microsoft.com/en-us/library/3yfdhzk5%28v=vs.84%29.aspx
  3. 我在 createFile() sub 中模拟了您作为模板提供的 HTML 代码。如果您想更改它(即添加行),您还需要更新靠近 sub 顶部的 objArgs 变量声明。
  4. 主网页中的附加模板也是如此(参见#3),但它位于 appendFile() 子中。

用法:

  1. 要添加新内容,用户需要双击您计算机上的 *.vbs 文件。
  2. 提示将引导他们完成详细信息以添加他们的项目。
  3. 脚本完成后,它会自动为他们创建 *.html,更新您的主页,并用一个漂亮的 MsgBox 通知他们(您的项目名称已添加)。

希望这可以帮助。

于 2013-09-19T22:23:02.580 回答