Need a simple batch file windows 2003 server

February 3, 2011 at 10:33:26
Specs: Windows 2003 server
I have these two lines in WF.txt file:

Integration Service status: [Running]
Start time: [Fri Jan 21 17:35:44 2011]

----------------------------------------------------------------------------------------------------------------------------------
If status = Running
then
compare start time with system time
Run command CMD1 if comparision is more than an hour else exit
else exit.
----------------------------------------------------------------------------------------------------------------------------------

Script I wrote doesnt seem to be working on 2003 server, would appreciate your help:

@echo off
CLS
find "Workflow run status: [Running]" wf.txt > nul || goto not_running
echo Status = Running
find "Start time:" wf.txt > start_time.txt
for /f "tokens=2 delims=[]" %%a in (start_time.txt) do (

set cur_date=%%a
echo Date from File: %cur_date%

set cur_hr=%cur_date:~11,2%
echo Hr from file: %cur_hr%

echo today's Time %time%
set sys_hr=%time:~0,2%
::echo %time:~0,2%
echo System Hr: %sys_hr%
)

if %sys_hr% gtr %cur_hr% goto old
echo Script is running less than 1 hr
goto delete
:old
echo Script is running MORE than 1 hr
goto delete
:delete
echo deleting start_time.txt
del start_time.txt
goto end
:not_running
echo String not found
:end
pause


See More: Need a simple batch file windows 2003 server

Report •


#1
February 3, 2011 at 11:07:38
What causes this script to keep running? Is it scheduled in Task Scheduler?

I rewrote your script without gotos because I hate spaghetti looping...

@echo off
CLS

find "Workflow run status: [Running]" wf.txt > nul 
If %errorlevel%==1 (
  echo String not found
  goto :EOF
)

echo Status = Running

find "Start time:" wf.txt > start_time.txt
for /f "tokens=2 delims=[]" %%a in (start_time.txt) do (
  set cur_date=%%a
  echo Date from File: %cur_date%

  set /a cur_hr=%cur_date:~11,2%
  echo Hr from file: %cur_hr%

  echo today's Time %time%
  set /a sys_hr=%time:~0,2%
  ::echo %time:~0,2%
  echo System Hr: %sys_hr%
)

if %sys_hr% gtr %cur_hr% (
  Script is running MORE than 1 hr
  echo deleting start_time.txt
  del start_time.txt
) else (
  echo Script is running less than 1 hr
)

I also used "/a" on your times so that they are save as integers so that you can do a number comparison. This code is not tested, take it for what it is worth.

P.S. This site should help you out...

http://www.robvanderwoude.com/batch...


Report •

#2
February 4, 2011 at 06:44:00
Thanks for that bro, that was quick....Both scripts work however it doesn't work in first run....when I run it once, it doesn't show proper results...when i run it 2nd time, it shows partial correct results and when I run it 3rd time then it shows correct results.

Report •

#3
February 4, 2011 at 07:48:29
"it shows partial correct results"

Can you give us an example of what that means?


Report •

Related Solutions

#4
February 4, 2011 at 14:53:49
Variable expansion issue, http://www.computing.net/howtos/sho...

Close the loop earlier and some of the problems will go away.

%date% is not a reliable source, this picture may give the idea.
http://flickcabin.com/public/view/f...


This will only fix the expansion issue!:

find "Start time:" wf.txt > start_time.txt
for /f "tokens=2 delims=[]" %%a in (start_time.txt) do (
  set cur_date=%%a
)
  echo Date from File: %cur_date%

  set /a cur_hr=%cur_date:~11,2%
  echo Hr from file: %cur_hr%

  echo today's Time %time%
  set /a sys_hr=%time:~0,2%
  ::echo %time:~0,2%
  echo System Hr: %sys_hr%


Set /a will however error out if any of the substrings end up being "09".


Report •


Ask Question