Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
shashank.kaneria
- Posts: 10
- Joined: 07 Jan 2014 04:08
#1
Post
by shashank.kaneria » 24 Jan 2014 05:37
Hi Folks,
I am making a small DOS batch script.
Scenario: A script which can take the LoadPercentage.
if LoadPercentage is above 90% then it should run an executable(whatever it is).
My findings (just an command): Code: Select all
wmic cpu where "loadpercentage > 90" get loadpercentage
This will provide me the total cpu utilization(excluding Idle one)
I am stuck at if else loop and comparison operator.
Now we have to put an condition if CPU is more than 90 than execute "Test.EXE"
I will appreciate if anybody helps me out.
-
npocmaka_
- Posts: 517
- Joined: 24 Jun 2013 17:10
- Location: Bulgaria
-
Contact:
#2
Post
by npocmaka_ » 24 Jan 2014 05:49
rather you'll need logman with alerts
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#3
Post
by foxidrive » 24 Jan 2014 05:53
This should work:
Code: Select all
wmic cpu where "loadpercentage > 90" get loadpercentage | find "LoadPercentage" >nul && test.exe
-
shashank.kaneria
- Posts: 10
- Joined: 07 Jan 2014 04:08
#4
Post
by shashank.kaneria » 25 Jan 2014 05:05
foxidrive wrote:This should work:
Code: Select all
wmic cpu where "loadpercentage > 90" get loadpercentage | find "LoadPercentage" >nul && test.exe
AWESOME !!!
its working.
But in my scenario there is little bit more addition to be done, it should work only once whenever it find CPU more than 90%, it will run and then stop.
i am not able to handle if else condition so that i cant check if it worked once (means flag is 1) then do EXIT; like this
Yesterday I have tried d whole day to manage this small code to do this but all gone in vain.

-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#5
Post
by foxidrive » 25 Jan 2014 07:12
The test.exe will run and the batch file will quit, with the addition below.
Code: Select all
wmic cpu where "loadpercentage > 90" get loadpercentage | find "LoadPercentage" >nul && (test.exe & goto :EOF)
-
shashank.kaneria
- Posts: 10
- Joined: 07 Jan 2014 04:08
#6
Post
by shashank.kaneria » 26 Jan 2014 23:30
foxidrive wrote:The test.exe will run and the batch file will quit, with the addition below.
Code: Select all
wmic cpu where "loadpercentage > 90" get loadpercentage | find "LoadPercentage" >nul && (test.exe & goto :EOF)
AWESOME !!!
Worked well. I was using some variables, Flags, if-else... hahaha
I have just made some baby like changes in this code (just an infinite loop)
Code: Select all
:BEGIN
wmic cpu where "loadpercentage > 1" get loadpercentage | find "LoadPercentage" >nul && (test.exe & goto :EOF)
GOTO BEGIN
Thank you so much... learnt a lot
Regards,
Shashank
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#7
Post
by foxidrive » 27 Jan 2014 09:40
Glad it helped.
Your code will make constant checks and consume a fair bit of CPU by itself.
If you add a line after the WMIC line that delays a little bit then it will take far less CPU, such as this:
Code: Select all
ping -n 4 localhost >nul
or if you have Vista or later
timeout /t 3 /nobreak >nul
and it will delay for around 3 seconds between each check for CPU usage.
-
shashank.kaneria
- Posts: 10
- Joined: 07 Jan 2014 04:08
#8
Post
by shashank.kaneria » 28 Jan 2014 02:32
foxidrive wrote:If you add a line after the WMIC line that delays a little bit then it will take far less CPU, such as this:
Code: Select all
ping -n 4 localhost >nul
or if you have Vista or later
timeout /t 3 /nobreak >nul
Okay, I have added the same.
its working more smooth now.
GREAT.. !!!Regards,
Shashank