Tags:
create new tag
, view all tags
I've almost forgotten how to write dos batch files, so I started this page for my recent adventures.

See AboutThesePages.

Contents:

Links

Execution Based on Time

Testif is a nifty free utility by Marc Perkel. It is free, AFAICT, but he sells it in a bundle with a bunch of other programs for $59.95. ("This program is part of the Computer Tyme DOS ToolBox. For $59.95 you can get the rest of the programs.", $495 per server site license).

Testif can test a wide variety of conditions, and boolean combinations of those conditions. See the testif documentation.

Some other things testif can test for (quoted from http://www.dosbin.com/batch01.htm#if):

<quote>
Tests for: VideoMode, VideoPage, ScreenWidth, Year, Month, Day,
DayOfWeek, Hour, Minute , Second , WriteVerify, DosVersion, 
MinorDosVersion, CurrentDrive, Drives, Floppies, SerialPorts, 
ParellelPorts, CapsLock, NumLock, ScrollLock, MemSize, Random, 
SectorsPerCluster(n) BytesPerSector(n), FreeClusters(n), TotalClusters
(n), FreeSpace(n), TotalSpace(n) , Exist <FileName> , FileSize 
<FileName>, FileAttr <FileName>, FileDate <FileName>, Mem(A:B), MemW
(A:B), Port(A).
</quote>

Below is a sample branching script. Testif returns errorlevel 1 if true and 0 if false.

I've attached the testif zip file (on my home TWiki) -- it will be interesting to see if it survives as I move to the web, etc.):

  • testif.zip (testif zip file) -- (Link works only for me, on my home LAN.)

(I won't attach it on the public WikiLearn site unless the original source disappears, or the author happens to write to me to grant permission.)

Oops, the following script does not work consistently, see the working script further down the page.

Non Working Script

testif Hour = 0 or Hour = 1
if errorlevel 0 goto false
@echo Do something if condition is true

goto done

:false 
@echo Else do something else if condition is false

:done
@echo done

Modified version, false condition is first (this is what I actually tested):

testif Hour = 8 or Hour = 9
if errorlevel 1 goto true

@echo Condition is false
goto done

:true 
@echo Condition is true

:done
@echo done

Oops, this does not work consistently. Oops 2, I was testing with the second version of this, modified so the false condition is first, yet, I'm quite certain this is what I used to test yesterday. When I get a chance, I'll try some testing with the first script.

OK (need to revise the page sometime). If the test is for errorlevel 0, I get bad results (in fact, today I haven't gotten any true results for any or combinations), if the test is for errorlevel 1, I've only seen one anomaly today, and maybe I was mistaken (??!!). OK (after further testing), I can get no true results at all if the test is for errorlevel 0 -- and, the more I think about it, I tested yesterday and today for the errorlevel 1 condition. After leaving the page like this for a few days in case Dann reads it, I'll delete the script that tests for errorlevel 0. I'm left with a fair degree of certainty that I there were anomalies in my testing yesterday.

When I test with a statement like "testif Hour = 0 or NumLock" and cycle through all the possible input combinations, it works as expected. But if I test with "testif Hour = 0 or Hour =1" and cycle through the possible input combinations (by changing the number in the Hour = test), I get inconsistent results.

Working Script

As a workaround, change the logic to run testif twice with two if statements, like the following:

testif hour = 0
if errorlevel 1 goto m_to_two
testif hour = 1 
if errorlevel 1 goto m_to_two

@rem Action if not midnight to 2 AM

goto done

:m_to_two
@rem Action if midnight to 2 AM

:done

Demonstrating the Problems with the First Script

Testing to try:

Assume the current clock time on your system is between 8:00 and 9:00 AM (so the hour is "8"), then run the nonworking script (#Non_Working_Script) with these lines substituted for the testif line:

For a test occurring between 8:00 and 9:00 AM 
Test statement:                  Result (condition is):
testif Hour = 8                  true
testif Hour = 9                  false
testif Hour = 8 or Hour = 9      !!false!!
testif Hour = 7 or Hour = 9      false
testif Hour = 8 or Hour = 9      true
testif Hour = 9 or Hour = 8      true
testif Hour = 8 or Hour = 9      true
testif Hour = 7 or Hour = 9      false
testif Hour = 8 or Hour = 9      true
testif Hour = 8                  true
testif Hour = 9                  false
testif Hour = 8 or Hour = 9      true !!
testif Hour = 7 or Hour = 9      false
testif Hour = 8 or Hour = 9      true
testif Hour = 9 or Hour = 8      true
testif Hour = 8 or Hour = 9      true
testif Hour = 7 or Hour = 9      false
testif Hour = 8 or Hour = 9      true

Yesterday, when I did similar testing between 3:00 and 4:00 PM, I got more anomalous results (unless I really had my head screwed on crooked). So, today, when I couldn't repeat the anomaly, I shut down the MS-Dos window that I was working in and repeated the tests in a new one. Still could not get the anomaly to repeat! Strange. Here are the results of the second set of tests:

For a test occurring between 8:00 and 9:00 AM 
Test statement:                  Result (condition is):
testif Hour = 8                  true
testif Hour = 9                  false
testif Hour = 8 or Hour = 9      true
testif Hour = 7 or Hour = 9      false
testif Hour = 8 or Hour = 9      true
testif Hour = 9 or Hour = 8      true
testif Hour = 8 or Hour = 9      true
testif Hour = 7 or Hour = 9      false
testif Hour = 8 or Hour = 9      true

Conclusions:

  1. I don't trust testif in the "Hour = X or Hour = Y" configuration.
  2. Test for the errorlevel 1 condition, not the errorlevel 0 condition.

-- RandyKramer - 08 Nov 2001

Edit | Attach | Watch | Print version | History: r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r4 - 2001-11-10 - RandyKramer
 
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by PerlCopyright 1999-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding WikiLearn? WebBottomBar">Send feedback
See TWiki's New Look