3

I'd like to use eventcreate from a batch file to log the results of a file copy job (robocopy). What I'd really like to do is use the output of the file copy job as the description of the event (/D of createevent). The trouble is, there are multiple lines in the file copy output, and I've only been able to get one line into a local variable or a pipe command.

I've tried reading a local variable in from file, like

set /P myVar=<temp.txt

but it only gets the first line.

How can I write multiple lines to the description of an event from a batch file?

2 Answers2

2

You have to parse the log and and change and CRLF to just LF (ctrl-l).

Here is an example:

EVENTCREATE /T ERROR /ID 1000 /l application /d "This is text^L this is line 2"
Jim B
  • 24,276
0

You can construct a multi-line variable by yourself, called !LF!. This post offers detailed explanation of how the newline hack/the CMD parser works.

@echo off
SETLOCAL EnableDelayedExpansion

(set LF=^
%=--------DO NOT remove this line. Expands to nothing--------=%
)
set "lines=" reset LINES to 0
set "in=temp.txt"


call :readlines "%in%" lines

<"%in%" (FOR /L %%# in (1 1 %lines%) do (
    set "data="                                ::clear DATA
    set/p"data="                               ::read from IN
    set "fileContent=!fileContent!!data!!LF!"  ::manually construct multi-line variable
)) %= read file by lines via SET /P =%

EVENTCREATE /T ERROR /ID 1000 /l application /d "!fileContent!"
exit /b


:readlines FILE VAR
FOR /F %%L in ('
"findstr /N "^^" "%~1" %= do not skip empty lines =%"
') do set/a"%~2+=1" %= Get the # of lines =%

Screenshot:

ScriptKidd
  • 101
  • 3