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
HBruijn
  • 77,029
  • 24
  • 135
  • 201
Shlomi
  • 331
  • 2
  • 9
  • 19

1 Answers1

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.

twconnell
  • 902
  • 5
  • 13