@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
Batch file to search a string and then edit it
-
31-07-2022 - |
Question
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.
)
No correct solution
OTHER TIPS
@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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow