What is wrong with this batch file?

September 1, 2010 at 06:52:59
Specs: Windows XP, 3GB
First here is the batch file:

@echo off
for /F "skip=3 delims=<cr><lf>" %%V IN (TASKS.TXT) DO (
@echo %%V
set _var=%%V
set _time=%_var:~64%
@echo %_time%)

This is the contents of tasks.txt:

Process information for USTPCITL3CFP0B:

Name Pid Pri Thd Hnd Priv CPU Time Elapsed Time
EXCEL 5780 8 2 227 9004 0:00:00.843 2:13:14.011

What I expect is to have environment variables set such as these:

_var = EXCEL 5780 8 2 227 9004 0:00:00.843 2:13:14.011
_time=2:13:14.011

My goal is to pull the time value from the file and put assign it to a variable.

What's strange is, that when I first run the batch file, I end up with this for variables:

_time=~64
_var=EXCEL 5780 8 2 227 9004 0:00:00.843 2:13:14.011

When I run the batch file a second time, I get what I want:

_time=2:13:14.011
_var=EXCEL 5780 8 2 227 9004 0:00:00.843 2:13:14.011

Any idea why it will not work on the first run?


See More: What is wrong with this batch file?

Report •


#1
September 1, 2010 at 13:48:40
@echo off & setLocal EnableDELAYedeXpansion

for /F "skip=3 tokens=* delims= " %%V IN (TASKS.TXT) DO (
set S=%%V
set T=!S:~36!
)
echo.!S!
echo.!T!


=====================================
Helping others achieve escape felicity

M2


Report •

#2
September 1, 2010 at 15:44:29
Many Thanks!!!

I'll have to research that syntax. I searched through a number of documents via Google. That's how I got what I did. Not sure why it worked on second run.

But if I make one change to yours, and change the 36 to 64, it prints what I expect. I presume the S and T are variables I can further work with in the BAT file.

Thanks Again!


Report •

#3
September 8, 2010 at 08:32:43
I have made progress, but have one issue. Here is the batch file now:
PSLIST EXCEL > TASKS.TXT
for /F "skip=3 tokens=* delims= " %%V IN (TASKS.TXT) DO (
set S=%%V
set P=!S:~19,4!
set T=!S:~64!
set M=!S:~66,2!
if !M! GTR 5 PSkill !P! & echo Date: [%mm%]/[%dd%]/[%yy%]  TIME: %_hh%:%_min%:%_ss% Process !P! Elapsed Time !T! >> Taskil.log
)

Here is the text file lit is reading in:

Process information for USTPCITL3CFP0B:

Name Pid Pri Thd Hnd Priv CPU Time Elapsed Time
EXCEL 4152 8 2 194 7692 0:00:00.421 0:01:14.311
EXCEL 5896 8 2 195 7692 0:00:00.500 0:01:12.452

This works, except, it runs one extra round, and sometimes more, and sometimes not at all. For example if there are 4 Excel processes running that fall into > 5 min running, it may cycle through 6 times. So 2 of the processes it attempts to kill a second time, but then there will be an error because the process does not exist, as it was killed on the first pass.

Any thoughts?

[EDIT]
I was assuming the issue was related to something with my FOR, so I had not included everything in the batch file. But I did find a problem earlier in the file, and this is resolved.


Report •

Related Solutions


Ask Question