'===========================================================================
' Subject: DIRECTORY TREE MAKER               Date: 02-06-97 (09:22)       
'  Author: Terence Jordan                     Code: QB, QBasic, PDS        
'  Origin: Tjordan@ns15.cca.rockwell.com    Packet: DOS.ABC
'===========================================================================
'Here you go, this program makes a _complete_ tree of a given directory.

'I found this very useful to most of my programs.

'Command line options are simple:
'maketree.exe c:\pathname\ [/i|/o filename.ext|/t filename.txt|/m mail]

'Note: pathname needs driveletter with a colon, and a backslash on the end.
'so these are valid:
'c:\
'c:\windows\
'a:\it\really\is\useful\
'but these aren't:
'c:
'c:\windows
'folder\file\

'Desc. of switches...

'switch           | property
'------------------------------------
'/i               | invisible
'/o filename.ext  | output filename
'/t filename.ext  | tempfile filename
'/m name@whatever | shelled a program called
'                 | "Batchmail" and mailed the
'                 | result to you. You'll need
'                 | to implement this differently
'
'last note:
'This has been tested under win95, win3.1, and the dos versions they
'accompany.

DIM var$(10000)
qt$ = CHR$(34)
outfile$ = "log.txt"
tempfile$ = "dir.txt"
SHELL "Echo off"


curdir$ = COMMAND$
tryagain:
IF LEN(curdir$) < 3 THEN
 LINE INPUT "Enter directory structure: "; curdir$
 GOTO tryagain
END IF
invisible = 0
mailto = 0
switcharoo:
curdir$ = UCASE$(curdir$)
switch$ = LEFT$(curdir$, 3)

IF RIGHT$(curdir$, 1) = " " THEN
 curdir$ = LEFT$(curdir$, (LEN(curdir$) - 1))
 GOTO switcharoo
END IF

IF LEFT$(curdir$, 1) = " " THEN
 curdir$ = RIGHT$(curdir$, (LEN(curdir$) - 1))
 GOTO switcharoo
END IF

IF MID$(switch$, 2) <> ":" THEN
 IF switch$ = "/I " THEN
  invisible = 1
  curdir$ = RIGHT$(curdir$, (LEN(curdir$) - 3))
  GOTO switcharoo
 END IF

 IF switch$ = "/T " THEN
  curdir$ = RIGHT$(curdir$, (LEN(curdir$) - 3))
  FOR xx = 1 TO LEN(curdir$)
   IF MID$(curdir$, xx, 1) = " " THEN
    tempfile$ = LEFT$(curdir$, xx - 1)
    curdir$ = RIGHT$(curdir$, (LEN(curdir$) - xx) + 1)
    xx = LEN(curdir$)
   END IF
  NEXT xx
  GOTO switcharoo
 END IF

 IF switch$ = "/O " THEN
  curdir$ = RIGHT$(curdir$, (LEN(curdir$) - 3))
  FOR xx = 1 TO LEN(curdir$)
   IF MID$(curdir$, xx, 1) = " " THEN
    outfile$ = LEFT$(curdir$, xx - 1)
    curdir$ = RIGHT$(curdir$, (LEN(curdir$) - xx) + 1)
    xx = LEN(curdir$)
   END IF
  NEXT xx
  GOTO switcharoo
 END IF

 IF switch$ = "/M " THEN
  mailto = 1
  curdir$ = RIGHT$(curdir$, (LEN(curdir$) - 3))
  FOR xx = 1 TO LEN(curdir$)
   IF MID$(curdir$, xx, 1) = " " THEN
    mail$ = LEFT$(curdir$, xx - 1)
    curdir$ = RIGHT$(curdir$, (LEN(curdir$) - xx) + 1)
    xx = LEN(curdir$)
   END IF
  NEXT xx
  GOTO switcharoo
 END IF
END IF

IF invisible <> 1 THEN
 PRINT "** MakeTree 4.0! By Terence Jordan"
END IF
orgdir$ = curdir$
IF invisible <> 1 THEN
 PRINT "Creating Tree of: "; curdir$; "...";
END IF
OPEN tempfile$ FOR OUTPUT AS #1
CLOSE #1
OPEN outfile$ FOR OUTPUT AS #1
PRINT #1, curdir$
CLOSE #1

mainichi:
SHELL "dir " + curdir$ + "/o , > " + tempfile$
OPEN outfile$ FOR APPEND AS #2
OPEN tempfile$ FOR INPUT AS #1
 WHILE NOT (EOF(1))
  pr = 0
  LINE INPUT #1, dummy$
  dummy$ = UCASE$(dummy$)
  IF LEFT$(dummy$, 1) = "." THEN
   dummy$ = ""
  END IF

  fd = 0
   IF LEN(dummy$) >= 12 THEN
    IF MID$(dummy$, 9, 4) <> "    " THEN
     IF MID$(dummy$, 9, 1) = " " THEN
      IF LEFT$(dummy$, 1) <> " " THEN
       MID$(dummy$, 9, 1) = "."
      END IF
     END IF
    END IF
   END IF


  IF MID$(dummy$, 16, 5) = "<DIR>" THEN
   dummy$ = LEFT$(dummy$, 12)
   d2$ = ""
   FOR xx = 1 TO LEN(dummy$)
    IF MID$(dummy$, xx, 1) <> CHR$(32) THEN
     d2$ = d2$ + MID$(dummy$, xx, 1)
    END IF
   NEXT xx
   dummy$ = d2$
   PRINT #2, curdir$ + dummy$ + "\"
   IF invisible <> 1 THEN
    PRINT ".";
   END IF
   dummy$ = ""
   count = count + 1
   fd = 1
  END IF
   
  IF MID$(dummy$, 9, 4) = "    " THEN
   IF MID$(dummy$, 9, 1) = " " THEN
    IF LEFT$(dummy$, 1) <> " " THEN
     MID$(dummy$, 9, 1) = "."
    END IF
   END IF
  END IF

  IF fd <> 1 THEN
   IF MID$(dummy$, 9, 1) = "." THEN
    dummy$ = LEFT$(dummy$, 12)
    d2$ = ""
    FOR xx = 1 TO LEN(dummy$)
     IF MID$(dummy$, xx, 1) <> CHR$(32) THEN
      d2$ = d2$ + MID$(dummy$, xx, 1)
     END IF
    NEXT xx
    dummy$ = d2$
    PRINT #2, curdir$ + dummy$
    IF invisible <> 1 THEN
     PRINT ".";
    END IF
    dummy$ = ""
    count = count + 1
    fd = 1
   END IF
  END IF

 WEND
CLOSE #1
CLOSE #2
mainni:
num = num + 1
IF num > count THEN
 GOTO mainsan
END IF
OPEN outfile$ FOR INPUT AS #2
 FOR xx = 1 TO num + 1
  LINE INPUT #2, file$
 NEXT xx
CLOSE #2
IF RIGHT$(file$, 1) = "\" THEN
 curdir$ = file$
 GOTO mainichi
END IF
GOTO mainni


mainsan:
one = 0
dummy$ = ""
IF mailto = 1 THEN
  SHELL "V:\BTCHMAIL\btchmail.exe /R " + mail$ + " /A " + outfile$ + " /S " + CHR$(34) + "MakeTree Result" + CHR$(34)
END IF
   IF invisible <> 1 THEN
    PRINT
    PRINT "Directory tree made." + CHR$(7)
   END IF
