Here's a way to store some file information
in an array, using some of the code mentioned
in the previous thread.
This will store the filename, filesize, and
creation date and time in a user-defined data
type array.
(Should work with practically any QB version.)
'======================================================
[tt]
' $STATIC
DEFINT A-Z
TYPE FileInfo
Fname AS STRING * 12
Fsize AS LONG
Fdate AS STRING * 8
Ftime AS STRING * 6
END TYPE
REDIM Fi(1 TO 1) AS FileInfo
Cmd$ = ENVIRON$("COMSPEC"
Cmd$ = Cmd$ + " /c dir"
' Set the switches:
' /o:n = sort by file name
Cmd$ = Cmd$ + " /o:n"
' If you use any other switches you
' will probably have to adjust the
' way this program parses the directory
' listing. For instance, you might include
' the /4 switch to show four-digit years
' or /V to show additional information,
' such as disk allocation, modification
' and access dates and file attributes.
' In either case, you would have to
' examine the format of the DIR output
' and adjust the program accordingly.
Cmd$ = Cmd$ + "> mydir.tmp"
SHELL Cmd$
ff = FREEFILE
OPEN "mydir.tmp" FOR BINARY AS #ff
' The following is unfortunate...
' but it's hard to see how many lines
' in a text file without it.
' You could use the program with
' larger directories but you would
' have to open in for input and then
' read each line twice... once to count
' the lines and a second time to read them.
IF LOF(ff) > 32767 THEN
G$ = STRING$(32767, 32)
ELSE
G$ = STRING$(LOF(ff), 32)
END IF
GET #ff, 1, G$
CLOSE #ff
CrLf$ = CHR$(13) + CHR$(10)
Start = 1
' Count the lines in the file...
DO
NextCrlf = INSTR(Start, G$, CrLf$)
IF NextCrlf < 1 THEN EXIT DO
Start = NextCrlf + 2
LineCnt = LineCnt + 1
LOOP
' ...and use the count to
' dimension the array.
REDIM Fi(1 TO LineCnt - 9) AS FileInfo
' Read the lines sequentially...
ff = FREEFILE
OPEN "mydir.tmp" FOR INPUT AS #ff
' ...but pay no attention to the header.
FOR Re = 1 TO 7
LINE INPUT #ff, Header$
NEXT
Fcnt = 1
DO WHILE NOT EOF(ff)
LINE INPUT #ff, L$
IF RIGHT$(L$, 6) = " bytes" THEN
IF INSTR(L$, " file(s)"
> 0 THEN
EXIT DO
END IF
END IF
' Parse the file name
Tmp1$ = LEFT$(L$, 12)
Tmp2$ = RTRIM$(LTRIM$(LEFT$(Tmp1$, 8)))
Tmp3$ = RTRIM$(LTRIM$(RIGHT$(Tmp1$, 3)))
IF Tmp3$ = "" THEN
Fi(Fcnt).Fname = Tmp2$
ELSE
Fi(Fcnt).Fname = Tmp2$ + "." + Tmp3$
END IF
' Parse the file size
Tmp1$ = LTRIM$(MID$(L$, 13, 14))
TmpLoc = INSTR(Tmp1$, ","
IF INSTR(Tmp1$, "<DIR>"
THEN
'Its a directory, mark it
' for later purposes.
Fi(Fcnt).Fsize = -1
ELSE
IF TmpLoc < 1 THEN
Fi(Fcnt).Fsize = VAL(LTRIM$(Tmp1$))
ELSE
Tmp2$ = LEFT$(Tmp1$, TmpLoc - 1)
Tmp3$ = RIGHT$(Tmp1$, LEN(Tmp1$) - TmpLoc)
Fi(Fcnt).Fsize = VAL(LTRIM$(Tmp2$ + Tmp3$))
END IF
END IF
' Get the file date
Fi(Fcnt).Fdate = LTRIM$(MID$(L$, 27, 10))
' Get the file time
Fi(Fcnt).Ftime = LTRIM$(MID$(L$, 37, 7))
Fcnt = Fcnt + 1
LOOP
CLOSE #ff
' Clean up the temporary file
KILL "mydir.tmp"
' Print out your UDT array.
FOR Re = 1 TO UBOUND(Fi)
IF Fi(Re).Fsize > -1 THEN
' It's an ordinary file
PRINT Fi(Re).Fname,
PRINT Fi(Re).Fsize,
PRINT Fi(Re).Fdate,
PRINT Fi(Re).Ftime
ELSE
' It's a directory
PRINT Fi(Re).Fname,
PRINT "<DIRECTORY>",
PRINT Fi(Re).Fdate,
PRINT Fi(Re).Ftime
END IF
NEXT
[/tt]
'======================================================