Search code examples
xmlwindowsbatch-filecommand

Parsing XML for tag attributes in Windows batch


I've search continuously for this. And seen quite a few SO pages on this but none of them seem to be working for me. Many of them have tokens which I can't use because my attribute "filename" could be anywhere in the tag.

<?xml version="1.0" encoding="UTF-8" ?>
<thisprog>
<sessions>
    <main>
        <File fvl="0" offst="0" filename="c:\here\there.txt" backupFilePath="" />
        <File filename="c:\here\there2.txt" mapWrapIndentMode="-1" wrappie="no" />
        <File wrapCount="1" filename="c:\here\folder1\oops.txt" wrappie="no" />
        <File lang="None" encd="-1" ro="no" filename="c:\here\folder 2\foop.txt" />
    </main>
</sessions>
</thisprog>

I'm trying to get the batch to get the "filename" attributes and concatenate them together. So end up with (and with the quotes, since they are directories and could have spaces):

"c:\here\there.txt" "c:\here\there2.txt" "c:\here\folder1\oops.txt" "c:\here\folder 2\foop.txt"

I tried the code here Attribute ripping but I am not able to concatenate the filename strings. I must be missing something.

@echo off&setlocal

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "XML=%~1"
set "TAG=%~2"
set "PAR=%~3"
set "ALLFILES="

rem // Define defaults here:
if not defined XML set "XML=presentation.xml"
if not defined TAG set "TAG=slideshow"
if not defined PAR set "PAR=date"

set "FLAG="
for /F usebackq^ delims^=^ eol^= %%L in ("%XML%") do (
    set "LINE=%%L"
    setlocal EnableDelayedExpansion
    set "REST=!LINE:*<%TAG%=!"
    if not defined REST (
        set "FLAG=#"
    ) else (
        set "REST=!LINE:*<%TAG% =!"
        if not "!REST!"=="!LINE!" (
            set "FLAG=#"
        ) else (
            set "REST=!LINE:*<%TAG% =!"
            if not "!REST!"=="!LINE!" (
                set "FLAG=#"
            )
        )
    )
    for /F "tokens=1,2 delims=>" %%E in ("!REST!/") do (
        if defined FLAG (
            endlocal
            set "FLAG=#"
        ) else (
            endlocal
        )
        set "REST=%%E"
        if defined FLAG (
            call :GET_ATTR "REST:~,-1" "%PAR%"
        )
        if not "%%F"=="" (
            set "FLAG="
        )
        setlocal EnableDelayedExpansion
    )
    endlocal
)
echo "the end"
echo %ALLFILES%
endlocal
exit /B


:GET_ATTR var_string param_name
setlocal DisableDelayedExpansion
set "PAR=%~2"
setlocal EnableDelayedExpansion
set "STR=!%~1!"
set "ALLFILESFUNC=%%~S "
set "NEXT="
for %%S in (!STR!) do (
    if defined NEXT (
        endlocal
        echo the filename - "%%~S"
        rem //This is the filename attribute but I can't get it to concatenate to an accumulative string
        rem echo(%%~S
        set "NEXT="
        rem //It's not collecting the strings.
        set "ALLFILESFUNC=!ALLFILESFUNC! "^%%~S"^ "
        echo Accumulative - %ALLFILESFUNC%
        setlocal EnableDelayedExpansion
    )
    set "STR=!STR:*%%S=!"
    if "%%S"=="%PAR%" (
        if defined STR (
            if "!STR:~,1!"=="=" (
                if "!STR:~1,1!"==" " (
                    echo(
                ) else if "!STR:~1,1!"=="   " (
                    echo(
                ) else (
                    set "NEXT=#"
                )
            )
        )
    )
)
if defined NEXT echo(
endlocal
endlocal
exit /B

Any help would be appreciated.

Updated: I have it getting the filename but I won't collect all the filenames into one variable, ALLFILESFUNC.


Solution

  • I am afraid I don't understand your code...

    This is the way I would do it:

    @echo off
    setlocal EnableDelayedExpansion
    
    set "AllFiles="
    (for /F "tokens=1*" %%a in ('findstr "filename=" test.txt') do (
       set "line=%%b"
       for /F "delims=" %%c in (^"!line:" ="^
    
    !^") do set %%c
       set "AllFiles=!AllFiles! !filename!"
    )) 2> NUL
    echo AllFiles=%AllFiles%
    

    Output:

    AllFiles= "c:\here\there.txt" "c:\here\there2.txt" "c:\here\folder1\oops.txt" "c:\here\folder 2\foop.txt"