0

i searched & tried for hours to get a newline when using a Batchfile with output -> file.

I am new to batchprograming and need some help. So here is my problem:

What i want :

I have a Batch that ping a spezific Server and writes the result in a "xml" format. This format is not real xml - its a kind of XML that a second app (LCDHost) understand. Finaly the outfile should contain :

<host>myhost2ping.com</host> 
<date>22.12.2013</date> 
<time>17:03:25,55</time> 
<ping>36</ping>

When getting this LCDHost can read this File and show the result in a external LCD ( im my case a G19 Keyboard).

What i actually get :

<host>myhost2ping.com</host> <date>22.12.2013</date> <time>17:03:25,55</time> <ping>    Minimum = 36ms</ping>

My Batch:

:begin

for /f "delims=," %%a in ('ping -n 1 myhost2ping.com') do @echo ^<host^>myhost2ping.com^</host^> ^<date^>%DATE%^</date^> ^<time^>%TIME%^</time^> ^<ping^>%%a^</ping^> > ping.txt

TIMEOUT /T 60
goto :begin

What i already tried :

all versions of & echo. set ^ as Var /n, /c/n , /r/n

What i need :

each node ... in his own line last entry should be cutted to only ms :

From Minimum = 36ms to 36.

thanky you for your help.

2 Answers2

1

Here's another method using your code:

:begin

(for /f "delims=," %%a in ('ping -n 1 myhost2ping.com') do (
   echo ^<host^>myhost2ping.com^</host^>
   echo ^<date^>%DATE%^</date^>
   echo ^<time^>%TIME%^</time^>
   echo ^<ping^>%%a^</ping^>
)) > ping.txt

TIMEOUT /T 60
goto :begin
foxidrive
  • 37,659
  • 8
  • 47
  • 67
0

try this:

@echo off
setlocal EnableDelayedExpansion
set NL=^


REM the two empty lines above are required!

:begin

for /f "delims=," %%a in ('ping -n 1 www.google.de^|find "Minimum"') do ( set t=%%a )
rem it's easier to work with the variable outside the for-block:
set "t=%t: =%"         :: remove spaces
set "t=%t:Minimum=%"   :: remove Minimum
set "t=%t:ms=%"        :: remove ms
set "t=%t:~1%"         :: remove first char (equal sign)

@echo ^<host^>myhost2ping.com^</host^>!NL!^<date^>%DATE%^</date^>!NL!^<time^>%TIME

%^</time^>!NL!^<ping^>%t%^</ping^> > ping.txt
timeout /t 6
goto :begin
Stephan
  • 47,723
  • 10
  • 50
  • 81
  • Thank you very much - this is it! you save my day ;) –  Dec 22 '13 at 17:39
  • I just noticed: I reduced the timeout for testing. You might want to set it to "60" again. Also you might consider to accept the answer as correct. – Stephan Dec 22 '13 at 17:59