View, Amend, Output the "titles" of all the HTML files in a folder and Save The HTML files

Author Message
AltNaMara

  • Total Posts : 2
  • Scores: 0
  • Reward points : 0
  • Joined: 1/4/2012
  • Status: offline
View, Amend, Output the "titles" of all the HTML files in a folder and Save The HTML files Wednesday, January 04, 2012 6:50 AM (permalink)
0
I would be grateful for any help anyone can give with this VBS script. Originally I was trying to come up with a script  which would cycle through a folder of HTML files and then extract and list the title text of each HTML file in an output file. By title I mean the text between the <title> and </title> tags within the HTML file. I've managed to do this but then realized that what I really required as well is the ability to amend the title text, save the amended HTML file and output the amended title text to the output file list.

I have managed, after a fashion,  to cobble up this rough and ready script which lets me view the text between the title tags [ie <title>text</title>], amend it as necessary and output the amended title text to a list in the outputfile. However, I'm now stuck because I can't figure out how to put the amended text between the "title" tags and save the amended HTML file in the folder while using their original file name.

I am a newbie so my knowledge as yet is still quite limited and I put the script together (don't laugh!)  using whatever samples I could find once I figured out what procedures were required. However, I have searched for scripts which will cover this final stage (saving the amended/ unamended HTML files in the folder once the titles/ amended titles have been extracted)  but have drawn a blank.

Any help, pointers, script directions would be very welcome

Thank you

AltnNaMara
 
{code}
Option Explicit
Dim Message, result
Dim Title, Text1, Text2
Dim strTitle
DIM objFSO
DIM strComputer
Dim objWMIService
Dim colFiles
Dim objFile
Dim strContents
Dim strStartText
Dim strEndText
Dim IntStart
Dim IntEnd
Dim intCharacters
Dim strCount
Dim strText
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='J:\VBS-Data_Analysis'} Where " _
        & "ResultClass = CIM_DataFile")
For Each objFile In colFiles
    Set objFile = objFSO.OpenTextFile(objFile.Name, ForReading)
    strContents = objFile.ReadAll
    objFile.Close
    strStartText = "<title>"

    strEndText = "</title>"
    intStart = InStr(strContents, strStartText)
    intStart = intStart + Len(strStartText)
    intEnd = InStr(strContents, strEndText)
    intCharacters = intEnd - intStart
    strCount =  Mid(strContents, intStart, intCharacters)
wscript.echo strCount
' Define dialog box variables.
Message = "Please Enter New Title"          
Title = "WSH user input "
Text1 = "User input canceled"
Text2 = "You entered:" & vbCrLf
' Ready to use the InputBox function
' InputBox(prompt, title, default, xpos, ypos)
' prompt:    The text shown in the dialog box
' title:     The title of the dialog box
' default:   Default value shown in the text box
' xpos/ypos: Upper left position of the dialog box
' If a parameter is omitted, VBScript uses a default value.
result = InputBox(Message, Title,  , 100, 100)
' Evaluate the user input.
If result = "" Then    ' Canceled by the user
    WScript.Echo Text1
Else
    WScript.Echo Text2 & result
End If

strCount = Result
strText = strtext & strCount & vbCrLf
Next
Set objFile = objFSO.CreateTextFile("J:\VBS-Data_Analysis\Totals.txt")
objFile.Write strText
objFile.Close
 
{/code}
 
<message edited by AltNaMara on Wednesday, January 04, 2012 7:47 AM>
 
#1
    AltNaMara

    • Total Posts : 2
    • Scores: 0
    • Reward points : 0
    • Joined: 1/4/2012
    • Status: offline
    Re:View, Amend, Output the "titles" of all the HTML files in a folder and Save The HTML fi Thursday, January 05, 2012 8:51 AM (permalink)
    0
    Have worked some more on this script and it now replaces the original text between the "title" tags with the amended text however I am now stumped as I can't get the script to save the amended HTML file. Running it produces an error message: 800A0186 - Object doesn't support this property or method; 'name'. Can anyone help point out where I am going wrong.
    Thank you
     
    AltNaMara
     
    {code}
    Option Explicit
    Dim Message, result
    Dim Title, Text1, Text2
    Dim strTitle
    Dim strFile
    DIM objFSO
    DIM strComputer
    Dim objWMIService
    Dim colFiles
    Dim objFile
    Dim strContents
    Dim newStrContents
    Dim strStartText
    Dim strEndText
    Dim IntStart
    Dim IntEnd
    Dim intCharacters
    Dim strCount
    Dim strText
    Dim ObjScriptingFile
    Dim strFilePAth
    Dim name
    Const ForReading = 1
    Const ForWriting = 2
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colFiles = objWMIService.ExecQuery _
        ("ASSOCIATORS OF {Win32_Directory.Name='F:\VBS-Data_Analysis'} Where " _
            & "ResultClass = CIM_DataFile")
    For Each objFile In colFiles
        Set objFile = objFSO.OpenTextFile(objFile.Name, ForReading)
        strContents = objFile.ReadAll
        objFile.Close
        strStartText = "<title>"

        strEndText = "</title>"
        intStart = InStr(strContents, strStartText)
        intStart = intStart + Len(strStartText)
        intEnd = InStr(strContents, strEndText)
        intCharacters = intEnd - intStart
        strCount =  Mid(strContents, intStart, intCharacters)
    wscript.echo strCount
    ' Define dialog box variables.
    Message = "Please Enter New Title"          
    Title = "WSH user input"
    Text1 = "User input canceled"
    Text2 = "You entered:" & vbCrLf
    ' Ready to use the InputBox function
    ' InputBox(prompt, title, default, xpos, ypos)
    ' prompt:    The text shown in the dialog box
    ' title:     The title of the dialog box
    ' default:   Default value shown in the text box
    ' xpos/ypos: Upper left position of the dialog box
    ' If a parameter is omitted, VBScript uses a default value.
    result = InputBox(Message, Title,  , 100, 100)
    ' Evaluate the user input.
    If result = "" Then    ' Cancelled by the user
        WScript.Echo Text1
    Else
        WScript.Echo Text2 & result
    End If
     
    strText = strtext & result & vbCrLf
    newStrContents = (Replace(strContents, strCount, result))
    'Checkpoint that amended Text is placed between "title" tags of HTML file
    wscript.echo newStrContents
      
    'Write all data back to HTML file
    strFilePath = objFile.Name
      
       
        Set objFile = objFSo.OpenTextFile(strFilePath, ForWriting)
       
        objFile.Write newstrContents
        objFile.Close
     
    Next
     

    Set objFile = objFSO.CreateTextFile("F:\VBS-Data_Analysis\Totals.txt")
    objFile.Write strText
    objFile.Close

    {/code}
     
    #2
      TNO

      • Total Posts : 2094
      • Scores: 36
      • Reward points : 0
      • Joined: 12/18/2004
      • Location: Earth
      • Status: offline
      Re:View, Amend, Output the "titles" of all the HTML files in a folder and Save The HTML fi Thursday, January 05, 2012 12:51 PM (permalink)
      0
      Perhaps this approach will be simpler to get you on the right track:
       
      https://gist.github.com/1568244
       
      It doesn't do all the work, but the general idea of how to get details of html files I think is pretty clear in this example
      To iterate is human, to recurse divine. -- L. Peter Deutsch
       
      #3

        Online Bookmarks Sharing: Share/Bookmark

        Jump to:

        Current active users

        There are 0 members and 1 guests.

        Icon Legend and Permission

        • New Messages
        • No New Messages
        • Hot Topic w/ New Messages
        • Hot Topic w/o New Messages
        • Locked w/ New Messages
        • Locked w/o New Messages
        • Read Message
        • Post New Thread
        • Reply to message
        • Post New Poll
        • Submit Vote
        • Post reward post
        • Delete my own posts
        • Delete my own threads
        • Rate post

        2000-2012 ASPPlayground.NET Forum Version 3.9