findstr 2 arguments and 2 events
I create a XML file from PRTG status monitor. in this XML file are values >4< for warning and >5< for error.
With findstr
and if errorlevel
I can only check if one of the value is true or false, but I want to check both values.
the goal to achieve is:
- when only >4< is in XML file, a serial string "warning_found" is sent to COM port.
- when only >5< is in XML file, a serial string "error_found" is sent to COM port.
- when >4< and >5< is in XML file, a serial string "error_found" is sent to COM port.
- when none of the 2 values are in XML file, a serial string "noerrord" is sent to COM port.
At the moment i have a functional batch file for only 2 variables => error and noerror (see below) but I want to implement the warning parameter as well:
@echo off
setlocal enableextensions enabledelayedexpansion
del %temp%\tmp.xml>nul 2>&1
::Init Variables
set url="http://127.0.0.1/api/table.xml"
set parameter="content=sensors&columns=status&username=wey&passhash=3085906047"
set com=COM3
set baud=9600
set parity=n
set data=8
::Read XML
curl.exe --data %parameter% %url% > %temp%\tmp.xml
::Init COM Port
mode %com% BAUD=%baud% PARITY=%parity% DATA=%data%
::Check for errors in xml
findstr /C:">5<" %temp%\tmp.xml
if errorlevel 1 goto noerror
goto error
:error
echo Error found
echo %* > %com%
echo fehler > %com%
goto end
:noerror
echo No error found
echo behoben > %com%
goto end
:end
del %temp%\tmp.xml>nul 2>&1
endlocal
exit
XML File:
<?xml version="1.0" encoding="UTF-8"?>
<sensors totalcount="528" listend="0">
<prtg-version>19.2.50.2842</prtg-version>
</item>
<item>
<status>OK </status>
<status_raw>3</status_raw>
</item>
<item>
<status>OK </status>
<status_raw>3</status_raw>
</item>
<item>
<status>OK </status>
<status_raw>3</status_raw>
</item>
<item>
<status>OK </status>
<status_raw>3</status_raw>
</item>
<item>
<status>Fehler </status>
<status_raw>5</status_raw>
</item>
<item>
<status>Fehler </status>
<status_raw>5</status_raw>
</item>
<item>
<status>Pausiert (Pausiert durch überg. Objekt)</status>
<status_raw>7</status_raw>
</item>
<item>
<status>Pausiert (pausiert)</status>
<status_raw>7</status_raw>
</item>
<item>
<status>Pausiert (Pausiert durch überg. Objekt)</status>
<status_raw>7</status_raw>
</item>
<item>
<status>Pausiert (Pausiert durch überg. Objekt)</status>
<status_raw>7</status_raw>
</item>
<item>
<status>Pausiert (pausiert)</status>
<status_raw>7</status_raw>
</item>
</sensors>
Thank you very much for the solution. I managed to achieve the same goal but not that elegantly as you did.
@echo off
setlocal enableextensions enabledelayedexpansion
del %temp%\tmp.xml>nul 2>&1
::Init Variables
set url="http://127.0.0.1/api/table.xml"
set parameter="content=sensors&columns=status&username=wey&passhash=2476365312"
set com=COM4
set baud=9600
set parity=n
set data=8
::Read XML
curl.exe --data %parameter% %url% > %temp%\tmp.xml
::Init COM Port
mode %com% BAUD=%baud% PARITY=%parity% DATA=%data%
::Check for warnings and error in xml
findstr ">4< >5<" %temp%\tmp.xml
if errorlevel 1 goto noerror
goto Check_for_warnings_in_xml
:Check_for_warnings_in_xml
findstr /C:">5<" %temp%\tmp.xml
if errorlevel 1 goto warning
goto error
:warning
start "" "C:/curl/curl.exe" -d "off" http://10.20.0.20/api/prtg_alarm
start "" "C:/curl/curl.exe" -d "on" http://10.20.0.20/api/prtg_warning
echo Warning found
echo warning_found > %com%
goto end
:error
start "" "C:/curl/curl.exe" -d "off" http://10.20.0.20/api/prtg_warning
start "" "C:/curl/curl.exe" -d "on" http://10.20.0.20/api/prtg_alarm
echo Error found
echo error_found > %com%
goto end
:noerror
start "" "C:/curl/curl.exe" -d "off" http://10.20.0.20/api/prtg_alarm
start "" "C:/curl/curl.exe" -d "off" http://10.20.0.20/api/prtg_warning
echo No error found
echo no_error > %com%
goto end
:end
del %temp%\tmp.xml>nul 2>&1
endlocal
exit