I have an XML file, containing the following:
<SubtitleTracks>
<SubtitleTrack>
<IsNotifying>true</IsNotifying>
<Burned>false</Burned>
<Default>false</Default>
<Forced>false</Forced>
<SourceTrack>
<SourceId>0</SourceId>
<TrackNumber>1</TrackNumber>
<Language>English [VOBSUB]</Language>
<LanguageCode>eng</LanguageCode>
<SubtitleType>VobSub</SubtitleType>
</SourceTrack>
<SrtOffset>0</SrtOffset>
<SubtitleType>VobSub</SubtitleType>
</SubtitleTrack>
<SubtitleTrack>
<IsNotifying>true</IsNotifying>
<Burned>false</Burned>
<Default>false</Default>
<Forced>false</Forced>
<SourceTrack>
<SourceId>0</SourceId>
<TrackNumber>2</TrackNumber>
<Language>English [VOBSUB]</Language>
<LanguageCode>eng</LanguageCode>
<SubtitleType>VobSub</SubtitleType>
</SourceTrack>
<SrtOffset>0</SrtOffset>
<SubtitleType>VobSub</SubtitleType>
</SubtitleTrack>
</SubtitleTracks>
The <SubtitleTrack>
section can repeat numerous times throughout the file.
However the structure is always the exact same. The only thing that changes in this section is this:
<SourceId>0</SourceId>
<TrackNumber>1</TrackNumber>
then the next one will be:
<SourceId>0</SourceId>
<TrackNumber>2</TrackNumber>
and so on...
Right now, <Default>false</Default>
is always the case.
What I would like to do is change <Default>false</Default>
to <Default>true</Default>
only when SourceId is 0 and TrackNumber is 1.
Can anyone please help translate these requirements into a batch file?
EDIT: I think I have almost got this, but it is choking on something, as the result I keep getting is <Default>false</Default> false
@ECHO OFF
SETLOCAL
SET "sourcedir=C:\Test"
SET "destdir=C:\Test"
SET "filename1=%sourcedir%\TestOutput.txt"
SET "outfile=%destdir%\TestOutputFixed.txt"
SET "hotsection="
CALL :clear$
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
rem if line contains `<SubtitleTrack>` we've entered hot section
ECHO "%%a"|FIND "<SubtitleTrack>">NUL
IF NOT ERRORLEVEL 1 SET "hotsection=y"
SET "saved="
IF DEFINED hotsection (
FOR /L %%r IN (100,1,999) DO IF NOT DEFINED saved IF NOT DEFINED $%%r SET "$%%r=%%a"&SET "saved=Y"
rem if line contains `</SubtitleTrack>` or `</SubtitleTracks>` then end-of-hotsection
SET "endhot="
SET "endtrack="
FOR /f "tokens=1delims= " %%w IN ("%%a") DO FOR %%x IN ("</SubtitleTrack>") DO IF "%%w"==%%x SET "endhot=Y"&IF
"</SubtitleTracks>"==%%x SET "endtrack=Y"
IF DEFINED endhot (
rem end-of-hotsection
rem check whether we have SourceID 0 and TrackNumber 1
SET "id0="&SET "track1="
FOR /f "tokens=1,*delims== " %%r IN ('SET $') DO (
IF "%%s"==""<SourceId>0" SET "id0=Y"
IF "%%s"==""<TrackNumber>1" SET "track1=Y"
)
rem found end-of-hotsection. now regurgitate saved lines and set `default` appropriately
FOR /f "tokens=2delims==" %%r IN ('SET $') DO (
echo "%%r"|FINDSTR /r /c:" *\<Default>" >NUL
IF ERRORLEVEL 1 (ECHO %%r) ELSE (
FOR /f "tokens=1delims=:" %%s IN ("%%r") DO (
IF defined track1 (IF DEFINED id0 (ECHO %%s true,) ELSE (ECHO %%s false,)) ELSE (ECHO %%s false,)
)
)
CALL :clear$
IF DEFINED endtrack SET "hotsection="
)
)
) ELSE (ECHO %%a)
)
)>"%outfile%"
GOTO :EOF
:clear$
:: remove variables starting $
FOR /F "delims==" %%z In ('set $ 2^>Nul') DO SET "%%z="
GOTO :EOF