Announcement

Collapse
No announcement yet.

Help in editing code in multiple HTML files using a Batch file or using JS file

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

  • Help in editing code in multiple HTML files using a Batch file or using JS file

    Hi,

    Could you please guide me how to create a batch file which should open an html file and edit a code in that.

    For example:

    My HTML code:

    <p class="simulationinstruction">Click <a id="h13" class="flashvideolinktemplate655x1018" title="title" href="javascript:void(1326)" target="_self" onclick="window.open('1327.htm','','toolbar=no, status=no, resizable=no, scrollbars=yes, width=1018 ,height=655 ,top=0 ,left=0')">

    The batch file should change - the red colored code to "no" in the html file

    After the batch file executed the code should be:

    <p class="simulationinstruction">Click <a id="h13" class="flashvideolinktemplate655x1018" title="title" href="javascript:void(1326)" target="_self" onclick="window.open('1327.htm','','toolbar=no, status=no, resizable=no, scrollbars=no, width=1018 ,height=655 ,top=0 ,left=0')">


    Please guide me how to create a batch file in such a way. I do not have any idea about batch file.

    Thanks

  • #2
    Re: Help in editing code in multiple HTML files using a Batch file or using JS file

    Powershell will do it:
    http://weblogs.asp.net/efrancobisi/a...ple-files.aspx

    As will cmd scripts:
    http://www.umachandar.com/technical/cmdscripts/main.htm
    (item 11 on the list)
    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


    • #3
      Re: Help in editing code in multiple HTML files using a Batch file or using JS file

      ant will also do this - that would be my tool of choice.
      Gareth Howells

      BSc (Hons), MBCS, MCP, MCDST, ICCE

      Any advice is given in good faith and without warranty.

      Please give reputation points if somebody has helped you.

      "For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

      "Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

      Comment


      • #4
        Re: Help in editing code in multiple HTML files using a Batch file or using JS file

        Use gnu sed for Windows, free for download!
        ( currently most resent sed binary: gsed407x.zip - GNU sed v4.0.7, UnxUtils precompiled 32-bit binaries for DOS or CMD running under Windows )


        example export editing:
        Code:
        @echo off
        
        Set "InputFile=infile.txt"
        Set "OutputFile=outfile.txt"
        
        sed.exe -e "s/\<scrollbars[ ]*=[ ]*yes\>/scrollbars=no/gI" "%InputFile%" >"%OutputFile%"
        example inplace editing:
        Code:
        @echo off
        
        Set "InputFile=inoutfile.txt"
        
        sed.exe -i "s/\<scrollbars[ ]*=[ ]*yes\>/scrollbars=no/gI" "%InputFile%"

        Other sample (remove single line breaks from inputfile, before searching for the string):
        Code:
        @echo off
        
        Set "InputFile=inoutfile.txt"
        
        Call:Create-wrkFile %~0
        sed.exe -f "%wrkFile%" -i "%InputFile%"
        Del "%wrkFile%"
        
        goto:eof   comment out this line if the script should continue the next step
        
        rem Optional... (restore end-of-line as supported in notepad)
        setlocal enabledelayedexpansion & Set/a i = 0
        For /f "delims=" %%* in ('find.exe /n /v ""^<"%InputFile%"') do (
           (set Line="''%%*") & (Set/a i = !i! + 1)
           (Set Line=!Line:^<=^^^<!) & (Set Line=!Line:^>=^^^>!)
           (Set Line=!Line:^|=^^^|!) & (Set Line=!Line:^&=^^^&!)
           >>"%InputFile%$" call:fixEOL )
           Move /y "%InputFile%$" "%InputFile%"
        endlocal
        
        
        goto:EOF + + + Begin Of Subroutines + + +
        
        :Create-wrkFile
        Set "wrkFile=%temp%.\$$$.sed"
        sed.exe -e "x;/^SEDscript\>/!d" -e "s/SEDscript//g" "%*" >"%wrkFile%"
        exit /b 0
        SEDscript     # sed script to change all paragraphs to long lines
        SEDscript     /./{H; $!d;}             # Put each paragraph into hold space
        SEDscript     x;                       # Swap hold space and pattern space
        SEDscript     s/^\(\n\)\(..*\)$/\2\1/; # Move leading \n to end of PatSpace
        SEDscript     s/\n\(.\)/ \1/g;         # Replace all other \n with 1 space
        SEDscript     # Uncomment the following line to remove excess blank lines:
        SEDscript     # /./!d;
        SEDscript     s/\<scrollbars[ ]*=[ ]*yes\>/scrollbars=no/gI
        SEDscript     #---end of sed script---
        SEDscript
        
        :fixEOL
        (Set Line=!Line:''[%i%]=!)
        (echo.%Line:~1,-1%)
        exit /b 0
        If you store sed.exe in the script's folder that is on a networkshare, the command line for sed must start with:
        "%~0\..\sed.exe" <rest of command line>
        If also the Inputfile is in the same share then the path to inputfile should be:
        Set "InputFile=%~0\..\inputfile.txt"
        (also for the output file when export to a new file)


        \Rems
        Last edited by Rems; 5th February 2012, 13:18.

        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


        • #5
          Re: Help in editing code in multiple HTML files using a Batch file or using JS file

          Thanks Ossian & gforceindustries for your quick response & suggestions.

          Comment


          • #6
            Re: Help in editing code in multiple HTML files using a Batch file or using JS file

            Hi Rims,

            Thanks for your quick reply.

            I tried to copy your code from the examples but did not work out exactly, i think, the way i created the batch file is wrong i guess.

            So could you please provide some sample file, so that i can download and try executing the same?


            Thanks much.

            Suresh

            Comment


            • #7
              Re: Help in editing code in multiple HTML files using a Batch file or using JS file

              Originally posted by surmar View Post
              Hi Rims,

              Thanks for your quick reply.

              I tried to copy your code from the examples but did not work out exactly, i think, the way i created the batch file is wrong i guess.

              So could you please provide some sample file, so that i can download and try executing the same?


              Thanks much.

              Suresh
              The scripts do work. Make sure you have dowloaded the correct version of sed.exe (follow the link). Place sed.exe in the Windows folder OR place it in the script's folder.

              Is your test batch in a folder on a local drive or on the network?
              Test with 'example export editing'.
              Modify the line 'Set "InputFile=infile.txt"' where 'inputfile.exe' should be repaced with the full path and file name of your html file.

              For trouble shooting purpose, Create a shortcut to the batch file. Edit the shortcut properties; - add cmd /k in front of the target "fullpath\batchfilename.cmd" Now fire the shortcut - are there any errors shown?

              Show us your test batch.

              \Rems
              Last edited by Rems; 20th July 2010, 08:06.

              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


              • #8
                Re: Help in editing code in multiple HTML files using a Batch file or using JS file

                Hi Rems,

                Let me explain my structue of the sample files which I created.

                I created a folder called "batchTest" in my D drive and I pasted the sed.exe file(downloades from the suggested link) in to the batchTest folder.

                And I created the .bat file as "mybat.bat" in the batchTest folder itself.

                The code for "mybat.bat":
                __________________________________________________ ___________
                @echo off

                Set "InputFile=5242.htm"
                Set "OutputFile=2.txt"

                sed.exe -e "s/\<scrollbars[ ]*=[ ]*yes\>/scrollbars=no/gI" "%InputFile$" >
                __________________________________________________ ___________

                And then I have pasted my html file also in the batchTest foder.

                In my html file line number : 72 has the code "scrollbars=yes" but this code is not updating after the batchfile (mybat.bat) executed.

                Please help.

                Thanks

                Suresh

                Comment


                • #9
                  Re: Help in editing code in multiple HTML files using a Batch file or using JS file

                  Originally posted by surmar View Post


                  The code for "mybat.bat":
                  Code:
                  @echo off
                  
                  Set "InputFile=5242.htm"
                  Set "OutputFile=2.txt"
                  
                  sed.exe -e "s/\<scrollbars[ ]*=[ ]*yes\>/scrollbars=no/gI" "%InputFile$" >
                  In my html file line number : 72 has the code "scrollbars=yes" but this code is not updating after the batchfile (mybat.bat) executed.

                  That is the expected behavior when using: 'example export editing'. It'll create a new file containing the updated content, the original file will NOT be overwritten. However,,,
                  in your version of the batch the sed.exe command line is incomlpete the outputfile is missing.

                  This is how the code should be:
                  Code:
                  @echo off
                  
                  Set "InputFile=5242.htm"
                  Set "OutputFile=2.txt"
                  
                  sed.exe -e "s/\<scrollbars[ ]*=[ ]*yes\>/scrollbars=no/gI" "%InputFile%" >"%OutputFile%"


                  If you don't want the file 2.txt but like to overwrite 5242.htm instead - then use the batch "example inplace editing":
                  Code:
                  @echo off
                  
                  Set "InputFile=5242.htm"
                  
                  sed.exe -i "s/\<scrollbars[ ]*=[ ]*yes\>/scrollbars=no/gI" "%InputFile%"
                  Or the latter "Other sample".


                  \Rems

                  (Similar posts
                  http://forums.petri.com/showthread.p...225#post253225
                  http://forums.petri.com/showthread.p...230#post253230
                  http://forums.petri.com/showthread.p...052#post255052
                  http://forums.petri.com/showthread.p...802#post253802

                  http://forums.petri.com/showthread.p...211#post212211
                  )
                  Last edited by Rems; 9th March 2012, 19:05. Reason: typo

                  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


                  • #10
                    Re: Help in editing code in multiple HTML files using a Batch file or using JS file

                    @OP, try this using GNU sed

                    Code:
                    C:\test>sed -i.bak "s/scrollbars[ \t]*=[ \t]*yes/scrollbars=no/gI" file
                    use the -i option to edit the file in place. Take note that the above sed command changes ALL scrollbars. So if you have other scrollbars in your file, then all will be changed. If you do not want this behaviour, put in a specific search string instead.

                    Comment

                    Working...
                    X