Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: change text in XML files  (Read 8952 times)

0 Members and 2 Guests are viewing this topic.

wbrost

    Topic Starter


    Intermediate
  • Thanked: 11
    change text in XML files
    « on: March 22, 2010, 12:12:23 PM »
    I am having trouble getting this to work correctly. Hopefully some one can tell me where I messed up on this. it is working up till the section :AddText.

    Code: [Select]
    @ECHO OFF
    CLS
    :START

    SET CURRENT=
    SET CHANGE=

    SET /P CURRENT=PLEASE ENTER THE CURRENT TEXT:
    SET /P CHANGE=PLEASE ENTER THE NEW TEXT:

    CLS

    ECHO %CURRENT%
    ECHO %CHANGE%

    ECHO ARE THESE CORRECT?
    SET /P CORRECT= YES OR NO:

    IF '%CORRECT%' =='' ECHO %choice% is not a valid choice && GOTO START

    ECHO %CORRECT% |findstr /i /r "[n]" > NUL

    IF /I %errorlevel% EQU 0 ECHO you chose no && GOTO START

    CLS

    DIR /b /a-d |findstr /e /i /r ".xml">dir.txt


    FOR /f "tokens=* delims==" %%a in (dir.txt) do set A=%%a &call :filecall %%a

    IF EXIST dir.txt del dir.txt
    ECHO DONE!
    PAUSE
    GOTO EOF


    :filecall
    ECHO %A%
    COPy %A% newfile.xml
    pause
    FOR /f "tokens=*" %%a in ("newfile.xml") do call :AddText "%%a


    DEL %A%
    pause
    REN new%A% %A%
    pause
    GOTO END

    :AddText %1
    set Text=%~1%
    IF NOT "%Text%"==%CURRENT% ECHO %Text% >>new%A%
    IF "%Text%"==%CURRENT% ECHO %CHANGE% >>new%A%

    :END


    ghostdog74



      Specialist

      Thanked: 27
      Re: change text in XML files
      « Reply #1 on: March 22, 2010, 06:11:19 PM »
      the reason that is wrong is you are using batch to parse XML. Use a XML parser if possible.
      also i am not going to look at your code. I am more interested in what your input xml file looks like, what you actually want to change, and what your output looks like.

      Next time, please don't just post a long code and say something is wrong, without further indication of what's wrong. Frankly, i for one am not going to run the script and find out what's wrong for you. any error messages, etc is helpful for the people that helps you.

      wbrost

        Topic Starter


        Intermediate
      • Thanked: 11
        Re: change text in XML files
        « Reply #2 on: March 23, 2010, 06:34:44 AM »
        the reason that is wrong is you are using batch to parse XML. Use a XML parser if possible.
        also i am not going to look at your code. I am more interested in what your input XML file looks like, what you actually want to change, and what your output looks like.

        Next time, please don't just post a long code and say something is wrong, without further indication of what's wrong. Frankly, i for one am not going to run the script and find out what's wrong for you. any error messages, etc is helpful for the people that helps you.

        the out put file is only getting the variable of %A%. I am not using an XML parser is because I did not think that it would be required. You can edit XML files with out the parser.

        I am trying to do a find replace on XML files in the current directory. As I said the only section I am having an issue with is the following:

        :AddText %1
           set Text=%~1%
           IF NOT "%Text%"==%CURRENT% ECHO %Text% >>new%A%
           IF "%Text%"==%CURRENT% ECHO %CHANGE% >>new%A%

        this is creating a file with just the variable %A% in it. 

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: change text in XML files
        « Reply #3 on: March 23, 2010, 07:02:28 AM »
        There is an art to writing batch code, scripts and programs.  There is also the mechanics of doing the same. One thing for sure is not to write the entire set of code before testing starts. Batch code is especially notorious for issuing cryptic messages with no clue as to where the error occurred. Code should be written is segments so you can test as you go. You may not have all the bells and whistles of the final product, but you won't have start debugging from line 1 either.

        A couple of things I found:

        You use goto eof (this is an error should be goto :eof)

        You also use goto end; no error just curious why, in this script they accomplish the same thing.

        I could not use ctl-C to escape from the prompts.

        The input file ends up with zero bytes. Your users may not like having their original file destroyed.

        The code in the :AddText label turned up no errors, so look for a logic error or even whether the code is executed at all.

        Check out this thread. Both the Microsoft XML parser method within a VBScript and the brute force method of the file system object are shown. Personally I prefer the parser as XML files have a rigid structure and the parser can easily traverse the nodes.

        If you insist on using batch code,  turn echo on so you can see what values the variable take on as the script runs. You may have to insert a few well placed pause statements to stop the scrolling.

        Good luck.  8)
        « Last Edit: March 23, 2010, 07:43:12 AM by Sidewinder »
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        Helpmeh



          Guru

        • Roar.
        • Thanked: 123
          • Yes
          • Yes
        • Computer: Specs
        • Experience: Familiar
        • OS: Windows 8
        Re: change text in XML files
        « Reply #4 on: March 23, 2010, 08:45:50 AM »
        You use goto eof (this is an error should be goto :eof)

        You also use goto end; no error just curious why, in this script they accomplish the same thing.
        There is no eof label, causing an error. Goto :label and Goto label are interchangable (or at least, they are for me).

        Goto end works because the last line is :end , causing it to go there and end.
        Where's MagicSpeed?
        Quote from: 'matt'
        He's playing a game called IRL. Great graphics, *censored* gameplay.

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: change text in XML files
        « Reply #5 on: March 23, 2010, 08:59:03 AM »
        There is no eof label, causing an error. Goto :label and Goto label are interchangable (or at least, they are for me).

        ":eof" is a special label added with the NT command extensions that always refers to the end of the file.
        I was trying to dereference Null Pointers before it was cool.

        wbrost

          Topic Starter


          Intermediate
        • Thanked: 11
          Re: change text in XML files
          « Reply #6 on: March 23, 2010, 12:24:37 PM »
          I figured out what my issue was. I had spaces in the path to the file. the last for statement was having trouble locating the file. after moving the file location everything is working correctly now.

          Thanks for the responses.   :)

          greg



            Intermediate

            Thanked: 7
            Re: change text in XML files
            « Reply #7 on: March 23, 2010, 12:26:31 PM »
            the out put file is only getting the variable of %A%. I am not using an XML parser is because I did not think that it would be required. You can edit XML files with out the parser.

            I am trying to do a find replace on XML files in the current directory. As I said the only section I am having an issue with is the following:

            :AddText %1
               set Text=%~1%
               IF NOT "%Text%"==%CURRENT% ECHO %Text% >>new%A%
               IF "%Text%"==%CURRENT% ECHO %CHANGE% >>new%A%

            this is creating a file with just the variable %A% in it. 

            A file name with spaces must be enclosed with quotes. Example:

            IF NOT "%Text%"==%CURRENT% ECHO %Text% >>new%A%
               IF "%Text%"==%CURRENT% ECHO %CHANGE% >>new%A%

            IF NOT "%Text%"==%CURRENT% ECHO %Text% >> "new%A%"
               IF "%Text%"==%CURRENT% ECHO %CHANGE% >> "new%A%"



            Have a Nice Day

            wbrost

              Topic Starter


              Intermediate
            • Thanked: 11
              Re: change text in XML files
              « Reply #8 on: March 23, 2010, 12:33:57 PM »
              ok this is were my issue was:

              FOR /f "tokens=*" %%a in ("newfile.xml") do call :AddText "%%a

              I needed to change the file location to something similar to the following:

              FOR /f "tokens=*" %%a in (C:\newfile.xml) do call :AddText %%a

              this required me to change the last couple of commands but at least it is working now.

              thanks again for the replies.

              greg



                Intermediate

                Thanked: 7
                Re: change text in XML files
                « Reply #9 on: March 23, 2010, 01:44:10 PM »
                ok this is were my issue was:

                FOR /f "tokens=*" %%a in ("newfile.xml") do call :AddText "%%a

                I needed to change the file location to something similar to the following:

                FOR /f "tokens=*" %%a in (C:\newfile.xml) do call :AddText %%a

                this required me to change the last couple of commands but at least it is working now.

                thanks again for the replies.


                Please post your output.  I'm unable to follow your code.
                I cannot get a test of your code to work.  There is not a start brace ( or end brace with for loops. The many cls  removes previous output.
                The receiving :label ... should be :addTest arg not %1.
                %1 should refer to the arg in the body of the subroutine.
                :AddText %1
                        set Text=%~1%
                        IF NOT "%Text%"==%CURRENT% ECHO %Text% >>new%A%

                I cannot imagine the code ever working.  Good luck

                Thanks for your help
                Have a Nice Day

                wbrost

                  Topic Starter


                  Intermediate
                • Thanked: 11
                  Re: change text in XML files
                  « Reply #10 on: March 23, 2010, 02:17:16 PM »
                  The batch solution was not exactly what my user was looking for. With the batch solution it would pull the entire line and not just the individual text. I re-looked at the link sent by Sidewinder and then modified the code to work for my situation.

                  @greg:
                  Please give the following code a try and let me know if it is still not working for you.

                  Code: [Select]
                  @ECHO OFF
                  :START
                  CLS

                  SET CURRENT=
                  SET CHANGE=

                  ECHO.
                  ECHO.
                  SET /P CURRENT=PLEASE ENTER THE CURRENT TEXT:
                  CLS
                  ECHO.
                  ECHO.
                  SET /P CHANGE=PLEASE ENTER THE NEW TEXT:

                  :CORRECT
                  CLS

                  ECHO.
                  ECHO.
                  ECHO %CURRENT%
                  ECHO %CHANGE%

                  ECHO.
                  ECHO.
                  ECHO.
                  ECHO ARE THESE CORRECT?
                  SET CORRECT=
                  SET /P CORRECT= YES OR NO:

                  IF '%CORRECT%' =='' ECHO %choice% is not a valid choice && GOTO CORRECT

                  ECHO %CORRECT% |findstr /i /r "[n]" > NUL

                  IF /I %errorlevel% EQU 0 ECHO you chose no && GOTO START

                  CLS

                  DIR /b /a-d |findstr /e /i /r ".xml">dir.txt

                  FOR /f "tokens=* delims==" %%a in (dir.txt) do set A=%%a&call :filecall %%a

                  IF EXIST dir.txt del dir.txt
                  :FIN
                  CLS

                  ECHO Do YOU WISH TO CONTINUE REPLACEING WORDS?

                  SET CORRECT=
                  SET /P CORRECT= YES OR NO:

                  IF '%CORRECT%' =='' ECHO %choice% is not a valid choice && GOTO FIN

                  ECHO %CORRECT% |findstr /i /r "[y]" > NUL

                  IF /I %errorlevel% EQU 0 ECHO you chose yes && GOTO START

                  CLS
                  ECHO.
                  ECHO.
                  ECHO DONE!
                  PAUSE
                  GOTO :EOF


                  :filecall

                  REM make VBS to do a find and replace.

                  ECHO Const ForReading = ^1>FANDR.VBS
                  ECHO Const ForWriting = ^2>>FANDR.VBS

                  ECHO Set objFSO = CreateObject("Scripting.FileSystemObject")>>FANDR.VBS
                  ECHO Set objFile = objFSO.OpenTextFile(^"%CD%\%A%^", ForReading)>>FANDR.VBS

                  ECHO strText = objFile.ReadAll>>FANDR.VBS
                  ECHO objFile.Close>>FANDR.VBS
                  ECHO strNewText = Replace(strText, ^"%CURRENT%^", ^"%CHANGE%^")>>FANDR.VBS

                  ECHO Set objFile = objFSO.OpenTextFile(^"%CD%\%A%^", ForWriting)>>FANDR.VBS
                  ECHO objFile.WriteLine strNewText>>FANDR.VBS
                  ECHO objFile.Close>>FANDR.VBS

                  CSCRIPT /nologo FANDR.VBS

                  IF EXIST FANDR.VBS del FANDR.VBS
                   

                  greg



                    Intermediate

                    Thanked: 7
                    Re: change text in XML files
                    « Reply #11 on: March 23, 2010, 06:16:00 PM »
                    greg:
                    Please give the following code a try and let me know if it is still not working for you.

                    It looks like it works but I don't really understand what you are doing are why.

                    Changing the name of .xml  files?

                    good luck

                    p.s. :  you have several error checking routines which seem to work well.
                    Have a Nice Day

                    wbrost

                      Topic Starter


                      Intermediate
                    • Thanked: 11
                      Re: change text in XML files
                      « Reply #12 on: March 23, 2010, 08:15:29 PM »
                      It looks like it works but I don't really understand what you are doing are why.

                      Changing the name of .xml  files?
                      The purpose of this batch is to do a find and replace on every .XML file in the current directory. Basically my office has several hundred XML files that change about once or twice a quarter depending on how many counties we fly. But, we only need to change two or three things in each file. This batch will allow the users to simply type the current text then the text they want it to change to.

                      ghostdog74



                        Specialist

                        Thanked: 27
                        Re: change text in XML files
                        « Reply #13 on: March 23, 2010, 09:25:03 PM »
                        download sed from here, then

                        Code: [Select]
                        @echo off
                        for /r %1 %%a in (*.xml) do (
                          sed -i.bak "s/%2/%3/g" "%%a"
                        )

                        use it as
                        Code: [Select]
                        c:\>change.bat c:\xml_dir old_word replacement

                        all the vbscript portion of your code (which is super inefficient as for every file you find, you create it again and again and again ) is just equivalent of the above. AND, sed with -i backs up your file as well.

                        wbrost

                          Topic Starter


                          Intermediate
                        • Thanked: 11
                          Re: change text in XML files
                          « Reply #14 on: March 24, 2010, 06:31:39 AM »

                          all the vbscript portion of your code (which is super inefficient as for every file you find, you create it again and again and again ) is just equivalent of the above. AND, sed with -i backs up your file as well.

                          that is true but it would take the users just as much time to type out that command for every XML file we have. If I were only changing a couple of files then I would just set the VBS to accept user input.

                          Code: [Select]
                          Const ForReading = 1
                          Const ForWriting = 2

                          strFileName = Wscript.Arguments(0)
                          strOldText = Wscript.Arguments(1)
                          strNewText = Wscript.Arguments(2)

                          Set objFSO = CreateObject("Scripting.FileSystemObject")
                          Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

                          strText = objFile.ReadAll
                          objFile.Close
                          strNewText = Replace(strText, strOldText, strNewText)

                          Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
                          objFile.WriteLine strNewText
                          objFile.Close
                          code from the following:
                          http://blogs.technet.com/heyscriptingguy/archive/2005/02/08/how-can-i-find-and-replace-text-in-a-text-file.aspx


                          If there is a way to select every XML file in the current directory using sed please let me know. If not than this is the best solution for my issue that I know of.