Solved DOS Read Text String between two other strings

February 28, 2018 at 19:43:02
Specs: Windows 10
In a text file like:
<inv_start_date>5/1/2017</inv_start_date>
<tax_point_date>6/20/2017</tax_point_date>
<tkr_date_start>1/1/2006</tkr_date_start>
<charge_date>5/1/2017</charge_date>
<charge_date>12/1/2017</charge_date>
<charge_date>11/21/2017</charge_date>
<acca_task>L210</acca_task>
<acca_activity>A104</acca_activity>
<acca_task>L120</acca_task>
<acca_activity>A104</acca_activity>
<units>2.3</units>
<rate>175</rate>
<units>0</units>
I need to read the string between <charge_Date> and </Charge_Date>
It represents a date. Then change the format of the date to YYYMMDD and replace the M/D/YYYY.
Please help.

See More: DOS Read Text String between two other strings

Report •

#1
March 1, 2018 at 17:30:39
✔ Best Answer
maybe:

@echo off & setlocal
for /f "tokens=*" %%a in (test1) do (
for /f "tokens=1,2 delims=<>" %%b in ("%%a") do (
if "%%b" equ "charge_date" (
call :fix %%c
) else (
>>test2 echo %%a
)
)
)
move test2 test1
goto :eof

:fix
set d=%1
set yy=%d:~-2%
for /f "tokens=1-2 delims=/" %%d in ("%1") do (
set mm=%%d
set dd=%%e
if %%d lss 10 set mm=0%%d
if %%e lss 10 set dd=0%%e
)
>>test2 echo ^<charge_date^>%yy%%mm%%dd%^</charge_date^>


Report •

#2
March 1, 2018 at 19:11:59
Thank you!!!
It worked fine. I needed Four Character year so I edited:

:fix
set d=%1
set yyyy=%d:~-4%
for /f "tokens=1-2 delims=/" %%d in ("%1") do (
set mm=%%d
set dd=%%e
if %%d lss 10 set mm=0%%d
if %%e lss 10 set dd=0%%e
)
>>test2.txt echo ^<charge_date^>%yyyy%%mm%%dd%^</charge_date^>

Again, thank you very much.
Live long, & Prosper.


Report •
Related Solutions


Ask Question