REM File: Bytefree v1.4a for Dndbbs v5.0a Release r2.0

REM Gets free disk drive space on default dndbbs directory and
REM  prints the value to output file.

REM Uses a shell to path in Cmd/Comspec.

REM Compile: BC Bytefree/ah/s/e/g3/o;
REM Link: LINK Bytefree/e;

REM Version v1.1a - add comspec.
REM Version v1.2a - add /d switch.
REM Version v1.3a - add DND.Path$
REM Version v1.4a - edit command line

CONST Version$="v1.4a"

ON ERROR GOTO Error.Routine1
100
V$ = COMMAND$
V$ = LTRIM$(RTRIM$(V$))
IF LEN(V$) THEN
   SELECT CASE V$
   CASE "/D"
      Debug = -1
   CASE "/?", "-?"
      COLOR 15, 0
      PRINT "Bytefree "+Version$
      COLOR 14, 0
      PRINT "Usage:"
      COLOR 15, 0
      PRINT "  Bytefree [/D]"
      COLOR 14, 0
      PRINT "Where:"
      COLOR 15, 0
      PRINT "  /D  -  debug mode."
      COLOR 7, 0
      END
   CASE ELSE
      COLOR 15, 0
      PRINT "Command line error."
      COLOR 14, 0
      PRINT "  Type bytefree /? for usage."
      COLOR 7, 0
      END
   END SELECT
END IF
200
IF Debug THEN
   COLOR 15, 0
   PRINT "Bytefree "+Version$
END IF
300
d$ = "CMD.EXE"
GOSUB GetFreeSpace
IF x# = 0# THEN
   d$ = ENVIRON$("COMSPEC")
   IF d$ = "" THEN
      IF Debug THEN
         COLOR 15, 0
         PRINT "Comspec not found."
         COLOR 7, 0
      END IF
      END
   END IF
   GOSUB GetFreeSpace
END IF
400
IF x# THEN
   CLOSE
   DND.Path$ = ENVIRON$("DNDBBS")
   IF DND.Path$ <> "" THEN
      IF RIGHT$(DND.Path$, 1) <> "\" THEN
         DND.Path$ = DND.Path$ + "\"
      END IF
   END IF
   f$ = DND.Path$ + "FREEDISK.OUT"
   OPEN f$ FOR OUTPUT AS #1
   PRINT #1, x#
   CLOSE
   IF Debug THEN
      COLOR 14, 0
      PRINT " Used: "; d$
      PRINT " File: "; f$
      PRINT " Disk:"; x#
   END IF
   COLOR 7, 0
   END 0
END IF
COLOR 7, 0
END 1
500
GetFreeSpace:
x# = 0#
CLOSE
DND.Path$ = ENVIRON$("DNDBBS")
IF DND.Path$ <> "" THEN
   IF RIGHT$(DND.Path$, 1) <> "\" THEN
      DND.Path$ = DND.Path$ + "\"
   END IF
END IF
OutputFile$=DND.Path$+"FILE.OUT"
IF DIR$(OutputFile$) <> "" THEN
   KILL OutputFile$
END IF
InputFile$=DND.Path$+"DNDBBS.EXE"
SHELL d$ + " /C dir "+InputFile$+" > "+OutputFile$
IF DIR$(OutputFile$) = "" THEN
   RETURN
END IF
600
OPEN OutputFile$ FOR INPUT AS #1
x = 0
DO
   IF EOF(1) THEN
      EXIT DO
   END IF
   LINE INPUT #1, a$
   a$ = RTRIM$(a$)
   IF a$ <> "" THEN
      x$ = LCASE$(a$)
      x = INSTR(x$, "bytes free")
      IF x THEN
         EXIT DO
      END IF
   END IF
LOOP
700
IF x THEN
   V = INSTR(x$, "dir(s)")
   IF V THEN
      x$ = MID$(x$, V + 6)
      x = INSTR(x$, "bytes free")
   END IF
   x$ = LEFT$(x$, x - 1)
   x$ = RTRIM$(x$)

   ' remove commas
   DO
      l = INSTR(x$, ",")
      IF l THEN
         x$ = LEFT$(x$, l - 1) + MID$(x$, l + 1)
      ELSE
         EXIT DO
      END IF
   LOOP

   ' store value
   x# = INT(VAL(x$))
END IF
800
' delete temp file
CLOSE
IF DIR$(OutputFile$) <> "" THEN
   KILL OutputFile$
END IF
RETURN

' simple error routine
Error.Routine1:
IF Debug THEN
   PRINT "Critical error:"; ERR; " Line:"; ERL
END IF
END 1
