Batch-Script for formatting XML-files | Search for Strings and comment them out











up vote
0
down vote

favorite












So I have a XML-File, which isn't formatted, meaning it only has one line. The problem I have is, I want to delete two lines or comment them out.



Is there a way to format the XML-file using a batch-script, so if i have:



<xml><tag><othertag></othertag></tag></xml>


the output would be



<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>


Alternatively, how can I replace/add Strings, when I only have a pattern, and can't use findstr /v, because I only have one line?



Thanks in advance!










share|improve this question






















  • Are you limited to batch as a scripting language ?
    – Wald
    Oct 12 '15 at 9:17










  • Unfortunately yes.
    – user5417542
    Oct 12 '15 at 9:29






  • 2




    Use xsl and an identity transform with indenting.
    – Kevin Brown
    Oct 12 '15 at 9:31










  • The thing is, I dont need to do this for only one file. I need to do this for many subdirectories. And I have not much knowledge about xsl.
    – user5417542
    Oct 12 '15 at 9:52















up vote
0
down vote

favorite












So I have a XML-File, which isn't formatted, meaning it only has one line. The problem I have is, I want to delete two lines or comment them out.



Is there a way to format the XML-file using a batch-script, so if i have:



<xml><tag><othertag></othertag></tag></xml>


the output would be



<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>


Alternatively, how can I replace/add Strings, when I only have a pattern, and can't use findstr /v, because I only have one line?



Thanks in advance!










share|improve this question






















  • Are you limited to batch as a scripting language ?
    – Wald
    Oct 12 '15 at 9:17










  • Unfortunately yes.
    – user5417542
    Oct 12 '15 at 9:29






  • 2




    Use xsl and an identity transform with indenting.
    – Kevin Brown
    Oct 12 '15 at 9:31










  • The thing is, I dont need to do this for only one file. I need to do this for many subdirectories. And I have not much knowledge about xsl.
    – user5417542
    Oct 12 '15 at 9:52













up vote
0
down vote

favorite









up vote
0
down vote

favorite











So I have a XML-File, which isn't formatted, meaning it only has one line. The problem I have is, I want to delete two lines or comment them out.



Is there a way to format the XML-file using a batch-script, so if i have:



<xml><tag><othertag></othertag></tag></xml>


the output would be



<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>


Alternatively, how can I replace/add Strings, when I only have a pattern, and can't use findstr /v, because I only have one line?



Thanks in advance!










share|improve this question













So I have a XML-File, which isn't formatted, meaning it only has one line. The problem I have is, I want to delete two lines or comment them out.



Is there a way to format the XML-file using a batch-script, so if i have:



<xml><tag><othertag></othertag></tag></xml>


the output would be



<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>


Alternatively, how can I replace/add Strings, when I only have a pattern, and can't use findstr /v, because I only have one line?



Thanks in advance!







xml string batch-file format






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 12 '15 at 9:01









user5417542

5612726




5612726












  • Are you limited to batch as a scripting language ?
    – Wald
    Oct 12 '15 at 9:17










  • Unfortunately yes.
    – user5417542
    Oct 12 '15 at 9:29






  • 2




    Use xsl and an identity transform with indenting.
    – Kevin Brown
    Oct 12 '15 at 9:31










  • The thing is, I dont need to do this for only one file. I need to do this for many subdirectories. And I have not much knowledge about xsl.
    – user5417542
    Oct 12 '15 at 9:52


















  • Are you limited to batch as a scripting language ?
    – Wald
    Oct 12 '15 at 9:17










  • Unfortunately yes.
    – user5417542
    Oct 12 '15 at 9:29






  • 2




    Use xsl and an identity transform with indenting.
    – Kevin Brown
    Oct 12 '15 at 9:31










  • The thing is, I dont need to do this for only one file. I need to do this for many subdirectories. And I have not much knowledge about xsl.
    – user5417542
    Oct 12 '15 at 9:52
















Are you limited to batch as a scripting language ?
– Wald
Oct 12 '15 at 9:17




Are you limited to batch as a scripting language ?
– Wald
Oct 12 '15 at 9:17












Unfortunately yes.
– user5417542
Oct 12 '15 at 9:29




Unfortunately yes.
– user5417542
Oct 12 '15 at 9:29




2




2




Use xsl and an identity transform with indenting.
– Kevin Brown
Oct 12 '15 at 9:31




Use xsl and an identity transform with indenting.
– Kevin Brown
Oct 12 '15 at 9:31












The thing is, I dont need to do this for only one file. I need to do this for many subdirectories. And I have not much knowledge about xsl.
– user5417542
Oct 12 '15 at 9:52




The thing is, I dont need to do this for only one file. I need to do this for many subdirectories. And I have not much knowledge about xsl.
– user5417542
Oct 12 '15 at 9:52












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










@echo off
setlocal EnableDelayedExpansion

rem Create a variable with spaces
set "spaces= "
for /L %%i in (1,1,5) do set "spaces=!spaces!!spaces!"

rem Read the line
for /F "delims=" %%a in (input.txt) do set "line=%%a"

set level=0
:nextTag
rem Separate first tag from line
for /F "tokens=1* delims=<" %%a in ("!line!") do (
set "tag=%%a"
set "line=%%b"
)
rem Show first tag in one separate line
if "%tag:~0,1%" equ "/" set /A level-=5
echo !spaces:~0,%level%!^<!tag!
if "%tag:~0,1%" neq "/" set /A level+=5
if defined line goto nextTag


Output:



<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>


Note that the maximum length of a line that can be processed this way is 8150 characters approx. Also, this solution remove any exclamation-marks from the file. Both points can be fixed, if required.






share|improve this answer





















  • Great! On the commandline it works, only need to add, that it writes the output in a new file. The character-limitation is no problem, but they exclamation-mark would be a deal, since the files will be used for mocking, so there should be no change.
    – user5417542
    Oct 12 '15 at 13:15


















up vote
0
down vote













try this :



@echo off

call ::beautifyXml c:some.xml

exit /b %errorlevel%

::::::::::::::::::
:beautifyXml
@echo off
powershell "function fx($xml, $i=2){$SW=New-Object System.IO.StringWriter;$XW=New-Object System.XMl.XmlTextWriter $SW; $XW.Formatting='indented';$XW.Indentation=$i;([xml]$xml).WriteContentTo($XW);$XW.Flush();$SW.Flush();Write-Output $SW.ToString();};FX (gc -path """%~f1""") -i 4">"%~dp1~"
move /y "%~dp1~" "%~f1" >nul 2>nul
goto :eof





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33077104%2fbatch-script-for-formatting-xml-files-search-for-strings-and-comment-them-out%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    @echo off
    setlocal EnableDelayedExpansion

    rem Create a variable with spaces
    set "spaces= "
    for /L %%i in (1,1,5) do set "spaces=!spaces!!spaces!"

    rem Read the line
    for /F "delims=" %%a in (input.txt) do set "line=%%a"

    set level=0
    :nextTag
    rem Separate first tag from line
    for /F "tokens=1* delims=<" %%a in ("!line!") do (
    set "tag=%%a"
    set "line=%%b"
    )
    rem Show first tag in one separate line
    if "%tag:~0,1%" equ "/" set /A level-=5
    echo !spaces:~0,%level%!^<!tag!
    if "%tag:~0,1%" neq "/" set /A level+=5
    if defined line goto nextTag


    Output:



    <xml>
    <tag>
    <othertag>
    </othertag>
    </tag>
    </xml>


    Note that the maximum length of a line that can be processed this way is 8150 characters approx. Also, this solution remove any exclamation-marks from the file. Both points can be fixed, if required.






    share|improve this answer





















    • Great! On the commandline it works, only need to add, that it writes the output in a new file. The character-limitation is no problem, but they exclamation-mark would be a deal, since the files will be used for mocking, so there should be no change.
      – user5417542
      Oct 12 '15 at 13:15















    up vote
    1
    down vote



    accepted










    @echo off
    setlocal EnableDelayedExpansion

    rem Create a variable with spaces
    set "spaces= "
    for /L %%i in (1,1,5) do set "spaces=!spaces!!spaces!"

    rem Read the line
    for /F "delims=" %%a in (input.txt) do set "line=%%a"

    set level=0
    :nextTag
    rem Separate first tag from line
    for /F "tokens=1* delims=<" %%a in ("!line!") do (
    set "tag=%%a"
    set "line=%%b"
    )
    rem Show first tag in one separate line
    if "%tag:~0,1%" equ "/" set /A level-=5
    echo !spaces:~0,%level%!^<!tag!
    if "%tag:~0,1%" neq "/" set /A level+=5
    if defined line goto nextTag


    Output:



    <xml>
    <tag>
    <othertag>
    </othertag>
    </tag>
    </xml>


    Note that the maximum length of a line that can be processed this way is 8150 characters approx. Also, this solution remove any exclamation-marks from the file. Both points can be fixed, if required.






    share|improve this answer





















    • Great! On the commandline it works, only need to add, that it writes the output in a new file. The character-limitation is no problem, but they exclamation-mark would be a deal, since the files will be used for mocking, so there should be no change.
      – user5417542
      Oct 12 '15 at 13:15













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    @echo off
    setlocal EnableDelayedExpansion

    rem Create a variable with spaces
    set "spaces= "
    for /L %%i in (1,1,5) do set "spaces=!spaces!!spaces!"

    rem Read the line
    for /F "delims=" %%a in (input.txt) do set "line=%%a"

    set level=0
    :nextTag
    rem Separate first tag from line
    for /F "tokens=1* delims=<" %%a in ("!line!") do (
    set "tag=%%a"
    set "line=%%b"
    )
    rem Show first tag in one separate line
    if "%tag:~0,1%" equ "/" set /A level-=5
    echo !spaces:~0,%level%!^<!tag!
    if "%tag:~0,1%" neq "/" set /A level+=5
    if defined line goto nextTag


    Output:



    <xml>
    <tag>
    <othertag>
    </othertag>
    </tag>
    </xml>


    Note that the maximum length of a line that can be processed this way is 8150 characters approx. Also, this solution remove any exclamation-marks from the file. Both points can be fixed, if required.






    share|improve this answer












    @echo off
    setlocal EnableDelayedExpansion

    rem Create a variable with spaces
    set "spaces= "
    for /L %%i in (1,1,5) do set "spaces=!spaces!!spaces!"

    rem Read the line
    for /F "delims=" %%a in (input.txt) do set "line=%%a"

    set level=0
    :nextTag
    rem Separate first tag from line
    for /F "tokens=1* delims=<" %%a in ("!line!") do (
    set "tag=%%a"
    set "line=%%b"
    )
    rem Show first tag in one separate line
    if "%tag:~0,1%" equ "/" set /A level-=5
    echo !spaces:~0,%level%!^<!tag!
    if "%tag:~0,1%" neq "/" set /A level+=5
    if defined line goto nextTag


    Output:



    <xml>
    <tag>
    <othertag>
    </othertag>
    </tag>
    </xml>


    Note that the maximum length of a line that can be processed this way is 8150 characters approx. Also, this solution remove any exclamation-marks from the file. Both points can be fixed, if required.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 12 '15 at 12:50









    Aacini

    50.5k75173




    50.5k75173












    • Great! On the commandline it works, only need to add, that it writes the output in a new file. The character-limitation is no problem, but they exclamation-mark would be a deal, since the files will be used for mocking, so there should be no change.
      – user5417542
      Oct 12 '15 at 13:15


















    • Great! On the commandline it works, only need to add, that it writes the output in a new file. The character-limitation is no problem, but they exclamation-mark would be a deal, since the files will be used for mocking, so there should be no change.
      – user5417542
      Oct 12 '15 at 13:15
















    Great! On the commandline it works, only need to add, that it writes the output in a new file. The character-limitation is no problem, but they exclamation-mark would be a deal, since the files will be used for mocking, so there should be no change.
    – user5417542
    Oct 12 '15 at 13:15




    Great! On the commandline it works, only need to add, that it writes the output in a new file. The character-limitation is no problem, but they exclamation-mark would be a deal, since the files will be used for mocking, so there should be no change.
    – user5417542
    Oct 12 '15 at 13:15












    up vote
    0
    down vote













    try this :



    @echo off

    call ::beautifyXml c:some.xml

    exit /b %errorlevel%

    ::::::::::::::::::
    :beautifyXml
    @echo off
    powershell "function fx($xml, $i=2){$SW=New-Object System.IO.StringWriter;$XW=New-Object System.XMl.XmlTextWriter $SW; $XW.Formatting='indented';$XW.Indentation=$i;([xml]$xml).WriteContentTo($XW);$XW.Flush();$SW.Flush();Write-Output $SW.ToString();};FX (gc -path """%~f1""") -i 4">"%~dp1~"
    move /y "%~dp1~" "%~f1" >nul 2>nul
    goto :eof





    share|improve this answer

























      up vote
      0
      down vote













      try this :



      @echo off

      call ::beautifyXml c:some.xml

      exit /b %errorlevel%

      ::::::::::::::::::
      :beautifyXml
      @echo off
      powershell "function fx($xml, $i=2){$SW=New-Object System.IO.StringWriter;$XW=New-Object System.XMl.XmlTextWriter $SW; $XW.Formatting='indented';$XW.Indentation=$i;([xml]$xml).WriteContentTo($XW);$XW.Flush();$SW.Flush();Write-Output $SW.ToString();};FX (gc -path """%~f1""") -i 4">"%~dp1~"
      move /y "%~dp1~" "%~f1" >nul 2>nul
      goto :eof





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        try this :



        @echo off

        call ::beautifyXml c:some.xml

        exit /b %errorlevel%

        ::::::::::::::::::
        :beautifyXml
        @echo off
        powershell "function fx($xml, $i=2){$SW=New-Object System.IO.StringWriter;$XW=New-Object System.XMl.XmlTextWriter $SW; $XW.Formatting='indented';$XW.Indentation=$i;([xml]$xml).WriteContentTo($XW);$XW.Flush();$SW.Flush();Write-Output $SW.ToString();};FX (gc -path """%~f1""") -i 4">"%~dp1~"
        move /y "%~dp1~" "%~f1" >nul 2>nul
        goto :eof





        share|improve this answer












        try this :



        @echo off

        call ::beautifyXml c:some.xml

        exit /b %errorlevel%

        ::::::::::::::::::
        :beautifyXml
        @echo off
        powershell "function fx($xml, $i=2){$SW=New-Object System.IO.StringWriter;$XW=New-Object System.XMl.XmlTextWriter $SW; $XW.Formatting='indented';$XW.Indentation=$i;([xml]$xml).WriteContentTo($XW);$XW.Flush();$SW.Flush();Write-Output $SW.ToString();};FX (gc -path """%~f1""") -i 4">"%~dp1~"
        move /y "%~dp1~" "%~f1" >nul 2>nul
        goto :eof






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 21:45









        npocmaka

        41.1k1083126




        41.1k1083126






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33077104%2fbatch-script-for-formatting-xml-files-search-for-strings-and-comment-them-out%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."