0

Sample input:

<document> 
 <filename>Admin Training Manual.pdf</filename>
 <type>0</type>
</document>
<document>
 <filename>Document Manual.pdf</filename>
 <type>0</type>
</document>

Condition: If the filename is = Admin Training Manual, change the type to 1.

Output:

<document> 
 <filename>Admin Training Manual.pdf</filename>
 <type>1</type>
</document>
<document>
 <filename>Document Manual.pdf</filename>
 <type>0</type>
</document>

I have tried everything on my disposal and cant find any answer.

I have not yet attempted but the concept is there:

if header = <document> (
  loop to count line inside <document>
    if line is equal to 1 
    then check the string
      if match
      then replace line 2 with 1.     
)
  • Please [edit] to include your best attempt so far so we have something to start with. – Edward Nov 18 '13 at 21:53
  • 2
    Doing this properly in batch is quite complicated. Can you rely on your input being formatted like this, and do you need to parse the XML hierarchy? *ie* is it sufficient to assume that there's one tag per line? – paddy Nov 18 '13 at 21:58
  • I have placed my algorithm. Does that seem valid? – user3006461 Nov 18 '13 at 22:20
  • There are the other questions? I'll add my own - are there any blank lines in the file? Do they need to be preserved? – foxidrive Nov 18 '13 at 23:25
  • No blank lines. They have a uniform format. – user3006461 Nov 19 '13 at 13:49

2 Answers2

1
@ECHO OFF &SETLOCAL
set "src=<filename>Admin Training Manual.pdf</filename>"
(for /f "delims=" %%a in (file.xml) do (
    set "line=%%a"
    SETLOCAL ENABLEDELAYEDEXPANSION
    if "!line!"=="!src!" set "flag=true"
    if not "!line:<type>=!"=="!line!" if defined flag (
        set "flag="
        set "line=<type>1</type>"
    )
    echo(!line!
    for /f %%b in ('set "flag" 2^>nul') do (if "!"=="" endlocal)&set "%%b"
))>out.xml
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Not tested and maybe i'm wrong, but, will not `endlocal` cancel the assignment of flag, so it is always undefined when `` line is readed? Also, no need for delayed expansion in `!src!` – MC ND Nov 19 '13 at 07:08
  • The script did place an output but it did not check correctly the condition thus skipping the tag – user3006461 Nov 19 '13 at 15:53
  • What does the ! before the !line! and the !src! do? – user3006461 Nov 19 '13 at 15:55
  • @MCND - concerning the flag: why do you think so about my code? And delayed expansion makes no harm to !src! and I always get nervous with so much poison characters :) – Endoro Nov 20 '13 at 21:27
  • @user3006461 and moreover `what does the fox say` :) – Endoro Nov 20 '13 at 21:28
  • @Endoro: The sequence `setlocal`, `set "flag=true"`, `endlocal` does not make changes to `flag` variable vanish between passes of the `for` loop? So, when the `src` line is seen, flag is set, but then endlocal is executed and changes to flag are lost. When the `type` line is readed, flag is undefined and `type` line is not changed. – MC ND Nov 21 '13 at 07:03
  • @MCND omg: you are right! and usually I know this :facepalm: /made an edit. THX. – Endoro Nov 21 '13 at 09:16
1
@echo off
setlocal EnableDelayedExpansion

for /F "delims=:" %%a in ('findstr /N /C:"<filename>Admin Training Manual.pdf</filename>" input.txt') do set /A num=%%a+1

(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" input.txt') do (
   set "line=%%b"
   if %%a equ %num% set "line=!line:0=1!"
   echo !line!
)) > output.txt
Aacini
  • 65,180
  • 12
  • 72
  • 108