Remove quote in xml file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Remove quote in xml file

#1 Post by darioit » 28 Sep 2014 02:05

Hello everybody

I have this file C:\Meniscus\Xml\cc_debug.txt that contain

Code: Select all

<msg><src>CC128-v0.11</src><dsb>01598</dsb><time>09:56:48</time><tmpr>23.1</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00271</watts></ch1></msg>

<msg><src>CC128-v0.11</src><dsb>01598</dsb><time>09:56:54</time><tmpr>23.1</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00269</watts></ch1></msg>

<msg><src>CC128-v0.11</src><dsb>01598</dsb><time>09:57:00</time><tmpr>23.1</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00270</watts></ch1></msg>


My goal is to create a new file with put head each record a string "<MenTime>28/09/2014 09:41:04</MenTime>" and get this result

Code: Select all

<MenTime>28/09/2014 09:56:48</MenTime><msg><src>CC128-v0.11</src><dsb>01598</dsb><time>09:56:48</time><tmpr>23.1</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00271</watts></ch1></msg>


Here my code

Code: Select all

echo off
set /a record=0
call :routine_ora

:start
echo %file_upd%
for /F %%a in ('tail -n1 C:\Meniscus\Xml\cc_debug.txt') do set MENISCUS="%%a"

echo ^<MenTime^>%date% %DateTime2:~9,2%:%smm1%:%sss1%^</MenTime^>%MENISCUS%
rem echo ^<MenTime^>%date% %DateTime2:~9,2%:%smm1%:%sss1%^</MenTime^>%MENISCUS%
echo ^<MenTime^>%date% %DateTime2:~9,2%:%smm1%:%sss1%^</MenTime^>%MENISCUS% >> "C:\Meniscus\Ftp\CurrentCost\%file_upd%"
set /a record=%record%+1
if "%record%"=="20" call :routine_ora
sleep 6

goto:start
goto:eof

:routine_ora
SET start=%time%
SET shh1=%start:~0,2%
SET smm1=%start:~3,2%
SET sss1=%start:~6,2%
FOR /f "tokens=1-7 delims=/.: " %%a in ("%date% %shh1%:%smm1%:%sss1%") do (set "H=0%%d" &call set "DateTime2=%%a%%b%%c_%%H:~-2%%%%e%%f")
set file_upd=ixwphysn_%DateTime2%
goto:eof


But my result is this (with quote)
<MenTime>28/09/2014 09:56:48</MenTime>"<msg><src>CC128-v0.11</src><dsb>01598</dsb><time>09:56:48</time><tmpr>23.1</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00271</watts></ch1></msg>"


But if I remove quote here

Code: Select all

 set MENISCUS="%%a"
my batch crash because of special redirection chars > and <

How can I resolve this problem?

Thanks in advance

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Remove quote in xml file

#2 Post by penpen » 28 Sep 2014 03:06

I haven't tested it, but i think the following may work:

Code: Select all

set "MENISCUS=%%a"

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove quote in xml file

#3 Post by foxidrive » 28 Sep 2014 03:39

A robust solution using a helper batch file:

Edited again:

The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.

Code: Select all


@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

type "C:\Meniscus\Xml\cc_debug.txt"| repl "(<msg><src>.*)" "<MenTime>%DD%/%MM%/%YYYY% %HH%:%Min%:%Sec%</MenTime>$1" I >"newfile.txt"
pause




This uses a helper batch file called `repl.bat` (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Remove quote in xml file

#4 Post by dbenham » 28 Sep 2014 08:08

When using normal expansion, poison characters like < and > must be either quoted or escaped. You don't want quotes in your output, and escaping the line you read might seem tricky. It is actually quite doable, but there is an easier way.

FOR variable expansion and delayed variable expansion both avoid the problem. In your case, all you need to enable delayed expansion at the top, make the change that penpen suggested, and then use delayed expansion ( !MENISCUS! instead of %MENISCUS% ).

Code: Select all

echo off
setlocal enableDelayedExpansion
set /a record=0
call :routine_ora

:start
echo %file_upd%
for /F %%a in ('tail -n1 C:\Meniscus\Xml\cc_debug.txt') do set "MENISCUS=%%a"

echo ^<MenTime^>%date% %DateTime2:~9,2%:%smm1%:%sss1%^</MenTime^>!MENISCUS!
rem echo ^<MenTime^>%date% %DateTime2:~9,2%:%smm1%:%sss1%^</MenTime^>!MENISCUS!
echo ^<MenTime^>%date% %DateTime2:~9,2%:%smm1%:%sss1%^</MenTime^>!MENISCUS! >> "C:\Meniscus\Ftp\CurrentCost\%file_upd%"
set /a record=%record%+1
if "%record%"=="20" call :routine_ora
sleep 6

goto:start
goto:eof

:routine_ora
SET start=%time%
SET shh1=%start:~0,2%
SET smm1=%start:~3,2%
SET sss1=%start:~6,2%
FOR /f "tokens=1-7 delims=/.: " %%a in ("%date% %shh1%:%smm1%:%sss1%") do (set "H=0%%d" &call set "DateTime2=%%a%%b%%c_%%H:~-2%%%%e%%f")
set file_upd=ixwphysn_%DateTime2%
goto:eof


It is possible to simplify your code quite a bit. In this solution I use the modulo operator and a division by 0 error to detect when I've reached a multiple of 20. I convert the infinite GOTO loop into an infinite FOR loop, and I use the FOR variable that holds the tail directly in the output. I simplified the computation of your time stamps, and I eliminate the need for any escaping by creating a variable to hold your MenTime element coupled with delayed expansion.

Code: Select all

echo off
setlocal enableDelayedExpansion
set /a record=-1
for /l %%# in () do (
  set /a "1/((record+=1)%%20)" 2>nul || (
    for /f "tokens=1-6 delims=/,.: " %%A in ("!date! !time: =0!") do (
      set "MenTime=<MenTime>%%A/%%B/%%C %%D:%%E:%%F</MenTime>"
      set "file_upd=C:\Meniscus\Ftp\CurrentCost\ixwphysn_%%A%%B%%C_%%D%%E%%F"
    )
  )
  for /f %%A in ('tail -n1 C:\Meniscus\Xml\cc_debug.txt') do (
    echo !MenTime!
    >>"!file_upd!" echo !MenTime!%%A
  )
  sleep 6
)


Dave Benham
Last edited by dbenham on 29 Sep 2014 04:30, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove quote in xml file

#5 Post by foxidrive » 28 Sep 2014 09:10

I thought the time stamp has to come from the file, at least the sample has the same timestamp as the line being processed, and not the same as the example text.

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Remove quote in xml file

#6 Post by darioit » 28 Sep 2014 10:57

Mr. dbenham, as Shakespeare says in one of his tragedy, you write code "by the book!" Many thanks!

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Remove quote in xml file

#7 Post by darioit » 29 Sep 2014 02:24

Hello, I try your code, but there's a little problem with time, it is show as "10:21:09,64", but I not use millisecond, so I try this command "%%F:~0,2" to gen only second "09" but it doesn't work, could you please help me?

Thanks and Regards

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Remove quote in xml file

#8 Post by dbenham » 29 Sep 2014 04:34

You just need to add a comma to the FOR /F "DELIMS" option. I've edited my 2nd set of code appropriately.

Note that the substring notation that you attempted only works with environment variables. It does not work with FOR variables or batch parameters.


Dave Benham

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Remove quote in xml file

#9 Post by darioit » 30 Sep 2014 02:35

Hello everybody,

Now I must merge this script with another one, because does not make sense have 2 script for same purpose
This is new script that update a rrd db

Code: Select all

for /F %%a in ('tail -n1 C:\Meniscus\Xml\cc_debug.txt ^| find "<watts>"') do call set row="%%a"
set TEMP=%row:~71,4%
set WATT=%row:~140,5%
rrdtool update powertemp.rrd N:%WATT%:%TEMP%


I thought to insert it after reading file cc_debug.txt, but I want read only raw with inside word "<watts>"

I wrote this code, it's a good code, or could be improved?

Code: Select all

echo off
setlocal enableDelayedExpansion
set /a record=-1
for /l %%# in () do (
  set /a "1/((record+=1)%%20)" 2>nul || (
    for /f "tokens=1-6 delims=/,.: " %%A in ("!date! !time: =0!") do (
      set "MenTime=<MenTime>%%A/%%B/%%C %%D:%%E:%%F</MenTime>"
      set "file_upd=C:\Meniscus\Ftp\CurrentCost\ixwphysn_%%A%%B%%C_%%D%%E%%F"
    )
  )
  for /f %%A in ('tail -n1 C:\Meniscus\Xml\cc_debug.txt') do (
    echo !MenTime!
    >>"!file_upd!" echo !MenTime!%%A
   rem start new script
   for /F %%a in ('tail -n1 C:\Meniscus\Xml\cc_debug.txt ^| find "<watts>"') do call set row="%%a"
   set TEMP=%row:~71,4%
   set WATT=%row:~140,5%
   rrdtool update powertemp.rrd N:%WATT%:%TEMP%
   rem end new script
  )
  sleep 6
)


Thanks in advance

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Remove quote in xml file

#10 Post by darioit » 01 Oct 2014 02:47

Hello, I find a problem, in this cicle of 20, variable "time" never change, it change only when a cicle is finished

Code: Select all

for /l %%# in () do (
  set /a "1/((record+=1)%%20)" 2>nul || (
    for /f "tokens=1-6 delims=/,.: " %%A in ("!date! !time: =0!") do (
      set "MenTime=<MenTime>%%A/%%B/%%C %%D:%%E:%%F</MenTime>"
      set "file_upd=C:\Meniscus\Ftp\CurrentCost\ixwphysn_%%A%%B%%C_%%D%%E%%F"
    )
  )

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Remove quote in xml file

#11 Post by darioit » 01 Oct 2014 09:07

I mean the complete code

Code: Select all

echo off
setlocal enableDelayedExpansion
set /a record=-1
for /l %%# in () do (
  set /a "1/((record+=1)%%20)" 2>nul || (
    for /f "tokens=1-6 delims=/,.: " %%A in ("!date! !time: =0!") do (
      set "MenTime=<MenTime>%%A/%%B/%%C %%D:%%E:%%F</MenTime>"
      set "file_upd=C:\Meniscus\Ftp\CurrentCost\ixwphysn_%%A%%B%%C_%%D%%E%%F"
    )
  )
  for /f %%A in ('tail -n1 C:\Meniscus\Xml\cc_debug.txt') do (
    echo !MenTime!
    >>"!file_upd!" echo !MenTime!%%A
  )
  sleep 6
)

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Remove quote in xml file

#12 Post by darioit » 29 Feb 2016 13:24

Hello, I have a new issue about this script, very easy

This code

Code: Select all

:Start
for /f %%A in ('tail -n1 C:\Meniscus\Data\cc_debug.txt') do (
   for /f "tokens=1-6 delims=/,.: " %%A in ("!date! !time: =0!") do (
      set "MenTime=<MenTime>%%A/%%B/%%C %%D:%%E:%%F</MenTime>"
   )
>>"!file_upd!" echo !MenTime!%%A
>>"!file_upd!" echo.

produce this resuls:
<ch1><watts>00732</watts></ch1></msg>CRLF
CRLF


CRLF is 0D0A

I want obtain this result, removing only LF(0A) on first raw
<ch1><watts>00732</watts></ch1></msg>CR
CRLF


How can I do, any Idea?
Thanks in advance

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Remove quote in xml file

#13 Post by darioit » 29 Feb 2016 23:55

I try this solution adding

Code: Select all

tr -d "\n" 
but doesn't work

Code: Select all

for /f %%A in ('tail -n1 C:\Meniscus\Data\cc_debug.txt ^| tr -d "\n"') do (

Post Reply