Announcement

Collapse
No announcement yet.

Help required to edit multiple htm files using VBScript

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help required to edit multiple htm files using VBScript

    Hi,

    I got an output package from an authoring tool, it contains lot of htm file in it. Some htm file has a code as "scrollbars=yes" in different lines in different files.

    I an opening each htm file using notepad or text editor to change the code "scrollbar = yes" to "scrollbar = no, fullscreen= yes" manually (using find and replace).

    But I wanted to automate this process by running a script file to do the code change in all the file as one shot.

    And I tried vbscript code to do that automation. The code as follows:
    Code:
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
    
    Dim oFSO, oFolder, oFile, oTextStream, strText
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder("D:\Suresh\MyFolder")
    
    For Each oFile In oFolder.Files
        If LCase(oFSO.GetExtensionName(oFile.Name)) = "htm" Then
            Set oTextStream = oFile.OpenAsTextStream(ForReading, TristateUseDefault)
            strText = oTextStream.ReadAll
            oTextStream.Close
            ' Do something with strText '
            Set oTextStream = oFile.OpenAsTextStream(ForWriting, TristateUseDefault)
            Response.oTextStream.Write Replace(strText, "scrollbars = yes", "scrollbars = no, fullscreen = yes") 
            'oTextStream.Write strText
            oTextStream.Close
        End If
    Next

    I saved this code as "sample.vbs" file. And to execute the code I just double clicked the "sample.vbs". But this code is not working as expected, I am not getting the change after the execution.

    Please guide me to fix the code, and also suggest me the way I am excuting the script (by double clicking) is right or not.

    Thanks.
    Last edited by Rems; 6th August 2010, 12:44. Reason: mod edit: added [code]-tags around the code

  • #2
    Re: Help required to edit multiple htm files using VBScript

    'oTextStream.Write strText

    remove the '
    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Re: Help required to edit multiple htm files using VBScript

      Double clicking would be OK.

      Note that your script will only replace "scrollbars = yes"
      scrollbar=yes
      scrollbars=yes
      scrollbars = yes
      Scrollbars = Yes
      will be interpret by your script as different phrases!

      It would be better to define a pattern and to replace with a new string using Regular Expression.


      \Rems

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: Help required to edit multiple htm files using VBScript

        Hi Rems,

        Thanks for your reply. But the code does not work for me, eventhough I have used as "scrollbars=yes" in the .vbs file as well as in the htm file. I meant I have used the same htm file code string in the .vbs file.

        But after I run the script file(double clicked), I am not getting any change in my htm files.

        Please suggest me, or provide some examples, that would be helpful to me to undersatand.

        Thanks much.

        Comment


        • #5
          Re: Help required to edit multiple htm files using VBScript

          Probably you didn't show us the acual script you are using? - anyways don't use On Error Resume Next, remove it from your scrpt, then you'd have seen were the problem is,,

          this line:
          Response.oTextStream.Write Replace(strText, "scrollbars = yes", "scrollbars = no, fullscreen = yes")
          it should not start with response. as the object does not exist in vbs.

          replace the line with:
          strText = Replace(strText, "scrollbars = yes", "scrollbars = no, fullscreen = yes", 1,-1,1)
          oTextStream.Write strText



          considerations
          - It is only nesserary to overwrite the file if the mathing string actuallt was found.
          - Use Regular-Expression instead of the wsh Replace() function.


          \Rems

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment


          • #6
            Re: Help required to edit multiple htm files using VBScript

            Hi Rems,

            I have shown you my actual complete code what I using to perform the task.

            Again I would like to show the updated (complete) code according to your suggestion.

            File name : sample.vbs
            Code:
            Const ForReading = 1, ForWriting = 2, ForAppending = 8
            Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
            
            Dim oFSO, oFolder, oFile, oTextStream, strText
            
            Set oFSO = CreateObject("Scripting.FileSystemObject")
            Set oFolder = oFSO.GetFolder("D:\Suresh\batchTest\myFiles")
            
            For Each oFile In oFolder.Files
                If LCase(oFSO.GetExtensionName(oFile.Name)) = "htm" Then
            	Set oTextStream = oFile.OpenAsTextStream(ForReading, TristateUseDefault)
            	strText = oTextStream.ReadAll
            	oTextStream.Close
            	Set oTextStream = oFile.OpenAsTextStream(ForWriting, TristateUseDefault)
            	strText = Replace(strText, "scrollbars=yes", "scrollbars=no, fullscreen=yes", 1,-1,1)
            	oTextStream.Write strText 
            	oTextStream.Close
                End If
            And I have placed some htm files in the path : D:\Suresh\batchTest\myFiles

            My sample.vbs file is placed in : D:\Suresh\batchTest


            After the updations as per you suggestions, then I try execute the sample.vbs file (by double clicking).


            I am getting an error as:

            Script : D:\Suresh\batchTest\sampl.vbs
            Line: 15 => 18 (adjusted by mod after re-formatting the code above)
            char: 7 => 11 (adjusted too)
            Error : Expected 'Next'
            Code: 800A03FC
            Source : Microsoft VBSCript compilation Error


            Pls suggest..

            Thanks.
            Last edited by Rems; 6th August 2010, 19:08. Reason: mod edit: added [code]-tags around the code

            Comment


            • #7
              Re: Help required to edit multiple htm files using VBScript

              You have a "for" in line 6 without a corresponding "next"
              Add it at the end of the script, just after the "endif"
              Tom Jones
              MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
              PhD, MSc, FIAP, MIITT
              IT Trainer / Consultant
              Ossian Ltd
              Scotland

              ** Remember to give credit where credit is due and leave reputation points where appropriate **

              Comment


              • #8
                Re: Help required to edit multiple htm files using VBScript

                Hi Rems & Ossian,

                Thanks for your suggestions. Thecode is working fine. I was facing some problems with the code because once of my htm file does not have any content (0 KB). After I removed that file the code working fine.

                Thanks much.

                Comment


                • #9
                  Re: Help required to edit multiple htm files using VBScript

                  Probably better to have a check for the length of the file
                  (something like: IF oFile.length > 0 THEN.... process file)

                  Note this is not the exact syntax but will give you a clue where to look

                  that will catch any similar problems in the future
                  Tom Jones
                  MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
                  PhD, MSc, FIAP, MIITT
                  IT Trainer / Consultant
                  Ossian Ltd
                  Scotland

                  ** Remember to give credit where credit is due and leave reputation points where appropriate **

                  Comment


                  • #10
                    Re: Help required to edit multiple htm files using VBScript

                    Originally posted by Ossian View Post
                    Probably better to have a check for the length of the file
                    (something like: IF oFile.length > 0 THEN.... process file)

                    Note this is not the exact syntax but will give you a clue where to look

                    that will catch any similar problems in the future
                    The syntax would be: oFile.size > 0
                    as in,
                    Code:
                    For Each oFile In oFolder.Files
                       If Left(oFile.type,3) = "HTM" AND oFile.size > 0 Then
                         With oFile.OpenAsTextStream(ForReading)
                           strText = .ReadAll : .Close
                         End With 
                         strText = Replace(strText, "scrollbars=yes", _
                                     "scrollbars=no, fullscreen=yes", 1,-1,1)
                         With oFile.OpenAsTextStream(ForWriting)
                           .Write strText : .Close
                         End With
                       End If
                    Next

                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    __________________

                    ** Remember to give credit where credit's due **
                    and leave Reputation Points for meaningful posts

                    Comment

                    Working...
                    X