一開始要接觸Watcom就是因為有在純DOS下完成long run test,並且有做次數的計數,但是,因為在純dos下的指令並不完全~所以,必需要自已寫一個在純DOS下的程式。
ExecBat.c#include "ExecBat.h"
int
main (
)
{
int CountValue = 0;
int DefaultCountValue = 10;
char SaveFile[] = "BatCount.txt";
char ActiveBat[] = "BatStart.bat";
char EndBat[] = "BatEnd.bat";
int WaitSecond = 10;
if (FileExistOrNot (SaveFile)) {
CountValue = ReadIntFromFile (SaveFile);
printf ("The residue conut is %d\n", CountValue);
if (CountValue == 0) {
//
// Execute
//
DelaySecond (WaitSecond);
ExecuteBat (EndBat);
} else if (CountValue < 0) {
DelaySecond (WaitSecond);
ExecuteBat (ActiveBat);
} else {
DelaySecond (WaitSecond);
if (FileExistOrNot (ActiveBat)) {
CountValue = CountValue - 1;
WriteIntIntoFile (SaveFile, CountValue);
}
ExecuteBat (ActiveBat);
}
} else {
printf ("Create %s and write default value into %s", SaveFile, SaveFile);
WriteIntIntoFile (SaveFile, DefaultCountValue);
}
return 0;
}
void
ExecuteBat (
char FileName[]
)
{
if (FileExistOrNot (FileName)) {
//
// execute the ActiveBat
//
printf ("Execute %s\n", FileName);
system (FileName);
} else {
printf ("The %s does not exist!!\n", FileName);
printf ("Can not execute %s!!", FileName);
}
}
void
DecreaseCountToFile (
char SaveFile[],
int CountValue,
bool DecreaseCount
)
{
//
// Decrease 1
//
CountValue = CountValue - 1;
if (DecreaseCount) {
WriteIntIntoFile (SaveFile, CountValue);
}
}
bool
FileExistOrNot (
char FileName[]
)
{
FILE *fp = NULL;
fp = fopen (FileName, "r");
if (fp == NULL) {
return false;
} else {
fclose (fp);
return true;
}
}
int
ReadIntFromFile (
char FileName[]
)
{
FILE *fp = NULL;
char szInput [256];
int CountValue = 0;
if (FileExistOrNot (FileName)) {
fp = fopen (FileName, "r");
//
// While the SaveFile exists!!
//
fgets ( szInput, 256, fp );
//
// Convert character to Integer
//
CountValue = atoi (szInput);
//
// Close file
//
fclose (fp);
printf ("Read %s sucessfully!!\n", FileName);
}
return CountValue;
}
void
WriteIntIntoFile (
char FileName[],
int CountValue
)
{
FILE *fp = NULL;
char szInput [256];
//
// Open file
//
fp = fopen (FileName, "w");
//
// Convert Integer to Character
//
itoa (CountValue, szInput, 10);
//
// Write value into file
//
fwrite (szInput, strlen (szInput), 1, fp);
//
// Close file
//
fclose (fp);
}
void
DelaySecond (
int Second
)
{
int Index;
for (Index = 0; Index < Second; Index++) {
printf ("%d...", Index);
fflush (stdout);
delay (1000);
}
printf ("\n");
}
ExecBat.h#ifndef _EXECBAT_H_
#define _EXECBAT_H_
#include
#include
#include
#include
typedef short bool;
#define true ((bool) 1 == 1)
#define false ((bool) 0 == 1)
void
ExecuteBat (
char FileName[]
);
void
DecreaseCountToFile (
char SaveFile[],
int CountValue,
bool DecreaseCount
);
bool
FileExistOrNot (
char FileName[]
);
int
ReadIntFromFile (
char FileName[]
);
void
WriteIntIntoFile (
char FileName[],
int CountValue
);
void
DelaySecond (
int Second
);
#endif //#ifndef _EXECBAT_H_
Makefile# Program, flags, ect.
ASM = wcl
TARGET = ExecBat.exe
OBJ = ExecBat.obj
everything: $(TARGET) .symbolic
all: clean everything
clean: cleanobj cleanexe .symbolic
cleanobj: .symbolic
[tab]@if exist *.obj del *.obj > NUL
[tab]@if exist *.err del *.err > NUL
[tab]@echo All OBJ cleaned
cleanexe: .symbolic
[tab]@if exist $(TARGET) del $(TARGET) >NUL
[tab]@echo All EXE cleaned
$(TARGET): $(OBJ)
[tab]$(ASM) /bcl=dos $< /fe=$@
.c.obj:
[tab]$(ASM) /c $*.c /fo=$*.obj
操作方式:
直接在Dos下執行ExecBat
會產生BatCount.txt,內含預設值10
再執行一次,每一次會減1,並且會執行BatStart.bat
當執行到值為0時,會執行BatEnd.bat
若要一直執行BatStart.bat的話,就把BatCount.txt的值設為負數的整數,就會一直執行BatStart.bat
另外使用者可以在BatStart.bat或EndBat.bat內加自己要執行的指令!