MackMan
MackMan

Reputation: 129

Create a vbs/batch file to edit text file and create a new text file

I am not very familiar with vbs, but I am getting decent at batch files. I am creating a simple website for my place of work as a bulletin for sales and things. Basically I have a text file which is a template for a new item for sale. I have things like <-!Insert Item Name Here!-> to make it easy for the non-tech-savvy people we have here at the office. I'm looking for a script to edit the main HTML file to add the new .html file to the folder and also create a new .html file using the template with prompts for the options (price, desc, dept, etc.)

I know this sounds like a lot but I'm hoping it can be done easily.

Basically, I need it to:

-Prompt to change Title, Description, Price, and Contact - Save this file as the title of the item .html -Add the new .html file to the index file in the format date, html location, title for link.

Here's my index.html so far, just so you can get an idea of how simple this site is...

<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 protected]">[email protected]</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>

Item website template

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

Hope this isn't too difficult.... Trying to figure out the easiest way to do this for non coding type people.

Upvotes: 2

Views: 3331

Answers (1)

chewmewaba4
chewmewaba4

Reputation: 75

Here you go:

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

Notes:

  1. In the collectData sub, you can define the path where you wish to save the files (currently in Scratch Files folder on your Desktop). You can also define the name of your main webpage (currently Bulletin.html)
  2. There is no user validation on any prompts (the InputBox is in the Function getInput), but feel free to add some. You may want to include a default value of "$" which would require some retooling of the getInput Function's parameters (add another parameter for Default value and include a third parameter on the inputbox(..) call; follow this page for more details: http://msdn.microsoft.com/en-us/library/3yfdhzk5%28v=vs.84%29.aspx)
  3. I mocked up the HTML code you provided as the template in createFile() sub. If you want to change that (i.e. Add lines), you'll also need to update the objArgs variable declaration near the top of the sub.
  4. Same goes (see #3) for the appending template in the main webpage but that is in the appendFile() sub.

Usage:

  1. To add new content, users need to double-click on the *.vbs file on your computer.
  2. The prompts will guide them through the details to add their item.
  3. When the script is complete it will automatically create their *.html for them, update your main page, and inform them with a nice MsgBox (your item item name was added).

Hope this helps.

Upvotes: 1

Related Questions