Making .bat file write a log

I have the next bat file, i want it to write a small log with time/date every time the process (dllhost.exe) is killed, so i know how oftet the bat is working and the dllhost is filling the memory.


:loop
@echo off
for /f "tokens=2 delims==" %%a in ('"wmic path Win32_PerfFormattedData_PerfProc_Process Where Name='dllhost.exe' get PercentProcessorTime /value 2>NUL | find /i "PercentProcessorTime""') do set usage=%%a
if %usage% GEQ 80 (
    taskkill /im dllhost.exe
) else (
    echo "usage at %usage% at %time%"
)
goto loop

Open in new window

LVL 1
celticianAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try it like this; I added a 10 seconds pause in each loop iteration.
@echo off
setlocal
set LogFile=C:\Temp\dllhost.log
:loop
@echo off
for /f "tokens=2 delims==" %%a in ('"wmic path Win32_PerfFormattedData_PerfProc_Process Where Name='dllhost.exe' get PercentProcessorTime /value 2>NUL | find /i "PercentProcessorTime""') do set usage=%%a
if %usage% GEQ 80 (
    taskkill.exe /im dllhost.exe
	>>"%LogFile%" echo [%Date%][%Time%] Killed dllhost.
) else (
    echo "usage at %usage% at %time%"
)
ping.exe -n 11 localhost >NUL
goto loop

Open in new window

celticianAuthor Commented:
by the way this script closes too quick i really dont know if this is being execued or not.... what parametir should i add to prevent the black cmd window from closing?
oBdACommented:
Instead of double-clicking the file, open a command prompt, enter "cd /d P:\ath\to\the\script", then run the script from there.
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

celticianAuthor Commented:
Oh okay, im now getting the next error:

"■@" is not recognized as an internal or external command operable program

Is there any strange character before the @ symbol?? I cant see it after opening the file with notepad.
oBdACommented:
This works for me:
@echo off
setlocal
set LogFile=C:\Temp\dllhost.log
:loopPerf
set Usage=0
for /f "tokens=2 delims==" %%a in ('wmic.exe path Win32_PerfFormattedData_PerfProc_Process Where Name^='dllhost' get PercentProcessorTime /value ^| find.exe /i "PercentProcessorTime"') do set usage=%%a
if %usage% GEQ 80 (
	taskkill.exe /im dllhost.exe
	>>"%LogFile%" echo [%Date%][%Time%] Killed dllhost.
) else (
	echo usage at %usage% at %time%
)
ping.exe -n 6 localhost >NUL
goto loopPerf

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
celticianAuthor Commented:
I keep getting the same error, however when i open it with notepad i dont find any strange characters. see attached capture with error (note it is in spanish)
bat.PNG
oBdACommented:
Open an empty Notepad window; click the "Select all" link under the code block in http:Q_28682495.html#a40804159, hit <Ctrl-C>.
Paste the content into Notepad, click "Save as", select a path, and make sure that "Encoding" at the bottom is set to ANSI.
celticianAuthor Commented:
oh thanks, now its working fine. it writing in the console the usage every 2 seconds.
thank you very much!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.