0

I'm trying to write a simple batch file to check the cpu of the computer every 30 seconds and output to txt file with the current time. I want each line to be like:

Line1: %time% %cpu%
Line2: %time% %cpu%

This is what I came up with so far, didn't work:

:loop

echo %TIME% > C:\test.txt

wmic cpu get loadpercentage > C:\test.txt

timeout 30

goto loop
  • Is there a reason you're not using the native Windows performance monitoring tools for this? – Ryan Bolger Jun 12 '17 at 14:28
1

A big issue with your sample is the redirection operator '>' - This will always overwrite the data in test.txt with the new data being entered. Use '>>' to append the output to the end of the file without deleting the information that is already in the file.

Instead of fixing your batch script, this PowerShell one-liner will give you the same result with much less effort.

While($true){Get-WmiObject win32_processor | Select-Object @{L="Date";E={Get-Date}},LoadPercentage,SystemName | Export-Csv -NoTypeInformation -Append CPU.csv; Start-Sleep -Seconds 5}

You can even target a remote computer with:

Get-WmiObject -ComputerName <computer>

Sorry for not answering your specific question. I hope you will find what I have provided to be useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.