home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / ONEADAY.CMD < prev    next >
OS/2 REXX Batch file  |  1995-03-28  |  5KB  |  105 lines

  1. @echo OFF
  2. REM *******************************************************************
  3. REM *** OneADay.cmd - Execute operations, but only if they have not ***
  4. REM *** ver.1         already been executed today.  This program    ***
  5. REM ***               uses CEnvi2 and the Cmm programming language   ***
  6. REM ***               to save commands and their execution dates    ***
  7. REM ***               in the file: C:\OneADay.dat.                  ***
  8. REM *******************************************************************
  9.  
  10. REM *****************************************************************
  11. REM *** Use shift to build the entire command into one big string ***
  12. REM *****************************************************************
  13. set THIS_CMD_NAME=%0
  14. set ONEADAY_COMMAND=
  15. :BUILD_COMMAND
  16.    if a%1z==az GOTO END_BUILD_COMMAND
  17.    set ONEADAY_COMMAND=%ONEADAY_COMMAND% %1%
  18.    SHIFT
  19.    GOTO BUILD_COMMAND
  20. :END_BUILD_COMMAND
  21.  
  22. REM *************************************************************************
  23. REM *** CEnvi2 will execute the following text, and return errorlevel 1 if ***
  24. REM *** the ONEADAY_COMMAND has not already been executed today           ***
  25. REM *************************************************************************
  26. CEnvi2 %THIS_CMD_NAME%.cmd
  27. if errorlevel 1 %ONEADAY_COMMAND%
  28. GOTO CENVI_EXIT
  29.  
  30. DataFile = "C:\\OneADay.dat"
  31.  
  32. // check if a command was entered; show instructions and return if not entered
  33. if ( !defined(ONEADAY_COMMAND)  ||  0 == strlen(ONEADAY_COMMAND) ) {
  34.    printf("\a\n")
  35.    printf("OneADay.cmd - Execute a command with its parameters, but not if it has already\n")
  36.    printf("              been executed today.\n")
  37.    printf("\n")
  38.    printf("SYNTAX: OneADay [Command] <parameters...>\n")
  39.    printf("\n")
  40.    printf("EXAMPLE: If you want to run a batch file that does virus checks and disk\n")
  41.    printf("         checks, called DISKCHCK.CMD, on hard drives C: and D: from your\n")
  42.    printf("         AUTOEXEC.CMD file when you reboot the computer, but not if you've\n")
  43.    printf("         already run AUTOEXEC.CMD today, then you may want the following\n")
  44.    printf("         lines in your AUTOEXEC.CMD:\n")
  45.    printf("\n")
  46.    printf("               CALL OneADay call DISKCHCK.CMD C:\n")
  47.    printf("               CALL OneADay call DISKCHCK.CMD D:\n")
  48.    printf("\n")
  49.    return(0)
  50. }
  51.  
  52. #define  RELEVANT_DATE_LEN    10  // only care about first ten digits of date
  53.  
  54. // Get command line, which is date followed by command
  55. (DateCommand = ctime(time()))[RELEVANT_DATE_LEN] = 0
  56. strcat(DateCommand,":")
  57. strcat(DateCommand,ONEADAY_COMMAND)
  58.  
  59. // open DataFile and TempFile, and copy each line to TempFile, always comparing
  60. // for a line with this command
  61. if NULL == (fp = fopen(DataFile,"r+"))  &&  NULL == (fp = fopen(DataFile,"w+")) {
  62.    printf("\aUnable to open file %s for writing.\n",DataFile)
  63.    return(0)
  64. }
  65.  
  66. // read in each line from data file, if it matches this exactly then the command
  67. // has already been run today.  If only the date is wrong then it hasn't run today
  68. // and so substitute this line in.  If no such file is found then write this new
  69. // command at the end of the file.
  70. for ( LineCount = 0; FilePosition = ftell(fp), NULL != (line = fgets(fp)); LineCount++ ) {
  71.    // get rid of newline character which may be at end of the line just read
  72.    if ( EndInNewLine = ('\n' == line[strlen(line)-1]) )
  73.       line[strlen(line)-1] = 0
  74.    // does this line compare, in part, to DateCommand
  75.    if ( RELEVANT_DATE_LEN < strlen(line) + 2
  76.      && 0 == strcmpi(DateCommand+RELEVANT_DATE_LEN+1,line+RELEVANT_DATE_LEN+1) ) {
  77.       // the command portions of these line are the same, so compare the date portion
  78.       if ( 0 == memicmp(DateCommand,line,10) ) {
  79.          // this line already exists, and so return not to run command
  80.          fclose(fp)
  81.          return(0)
  82.       } else {
  83.          // the date has changed, and so go back to the beginning of this line and
  84.          // write in this new data date
  85.          fseek(fp,FilePosition)
  86.          fprintf(fp,"%.*s",RELEVANT_DATE_LEN,DateCommand)
  87.          fclose(fp)
  88.          return(1)
  89.       }
  90.    }
  91. }
  92.  
  93. // if did not exit in the above for loop, then write this new command at the end
  94. // of the file.  If previous line did not end in newline, then add newline before
  95. // this command.
  96. if ( LineCount  &&  !EndInNewLine )
  97.    fprintf(fp,"\n")
  98. fprintf(fp,"%s",DateCommand)
  99. fclose(fp)
  100. return(1)
  101.  
  102. :CENVI_EXIT
  103. set ONEADAY_COMMAND=
  104. set THIS_CMD_NAME=
  105.