Long Filenames
File and Directory Stuff

DECLARE FUNCTION short.filename$ (longName$)
DECLARE FUNCTION long.filename$ (shortName$)
DECLARE FUNCTION is.file% (filename$)
DECLARE FUNCTION get.file$ (fileSpec$, attr AS INTEGER)
DECLARE SUB load.filelist (path$, spec$)
DECLARE SUB load.filelist2 (path$, filetype.amount%)
DECLARE SUB exit.error (msg$)

'$INCLUDE: 'qb.bi'

'$DYNAMIC
DIM SHARED option.amount AS INTEGER, option.list$(0)
DIM SHARED filetype.max AS INTEGER: filetype.max% = 10
DIM SHARED filetype.ext$(1 TO filetype.max%), option.amount2%(1 TO filetype.max%)

'"Files 0.51" by Plasma and Agamemnus. NOTE: REQUIRES qb.bi!!: "qb.exe /lqb"


' Plasma made the code for the functions "get.file", "long.filename", and
' "short.filename", and I modified the code somewhat and slightly optimized it.
' The other subs/functions are my own creation.
' -Agamemnus

' The following is a list of the SUBs/FUNCTIONs, and how they should be used:
'
' FUNCTION short.filename$ (longName$)
' Converts a long filename or directory to a short filename or directory.
' Requires a directory path for filename conversion.
'
' EXAMPLE:
' PRINT short.filename$("C:\long directory\long name.txt")
'
'
' FUNCTION long.filename$ (shortName$)
' Converts a short filename or directory to a long filename or directory.
' Requires a directory path for filename conversion.
'
' EXAMPLE:
' PRINT long.filename$(short.filename$("C:\long directory\long name.txt"))
'
'
' FUNCTION is.file% (fileName$)
' Checks that the filename$ string only has acceptable alphanumeric characters.
' DOES NOT check that the file exists. Therefore a full directory path
' should NOT be used, just the name of the file itself.
' In windows, unacceptable characters are:
' \ / : * ? " < > |
'
' EXAMPLE:
' PRINT is.file%("Illegal filename? Yes, I think so.txt")
'
'
' FUNCTION get.file$ (fileSpec$, attr AS INTEGER)
' INTERNAL function used to find the next file in the SUBs "load.filelist" and
' "load.filelist2"
'
'
' SUB load.filelist (path$, spec$)
' A sub that REDIMs the "option.list$()" array and then puts all the files
' with extention "spec$" and from directory "path"$ into this array.
' At the same time, this SUB calculates "option.amount%", which is the amount of
' files that fit the criteria and thus the size of "option.list$()".
' NOTE: option.list$() goes from 1 TO option.amount%.
' NOTE2: The long/short format of the directory does not matter. It is
' automatically converted to the short (8 character) format.
'
' EXAMPLE:
' load.filelist "C:\", "*.*"
' FOR i% = 1 TO option.amount%
' PRINT option.list$(i%)
' NEXT i%
'
'
' SUB load.filelist2 (path$, filetype.amount%)
' A sub that REDIMs the "option.list$()" array and then puts all the files
' with any of the extensions from array "filetype.ext$(1 TO filetype.amount%)"
' and from the directory "path$" into this array.
' At the same time, this SUB calculates option.amount, which is the amount of
' files that fit the criteria and thus the size of "option.list$()".
' NOTE: option.list$() goes from 1 TO option.amount%.
' NOTE2: The long/short format of the directory does not matter. It is
' automatically converted to the short (8 character) format.
' NOTE3: The maximum number of extensions is given by filetype.max%.
' (change the value and then redim filetype.ext$() to change the max during
' runtime.)
'
' EXAMPLE:
' filetype.ext$(1) = "*.bat"
' filetype.ext$(2) = "*.exe"
' load.filelist2 "C:\", filetype.amount%
' FOR i% = 1 TO option.amount%: PRINT option.list$(i%): NEXT i%
'
'
' SUB exit.error (msg$)
' A generic error handler where all known errors from the above functions
' are redirected.
'
' EXAMPLE: exit.error ("This program has experienced an upset stomach and will now close.")
'
' NECESSARY code:

' '$INCLUDE: 'qb.bi'
' '$DYNAMIC '(for load.filelist and load.filelist2)
' 'for load.filelist and load.filelist2:
' DIM SHARED option.amount AS INTEGER, option.list$(0)
' 'for load.filelist2:
' DIM SHARED filetype.max AS INTEGER: filetype.max% = 10
' DIM SHARED filetype.ext$(1 TO filetype.max%), option.amount2%(1 TO filetype.max%)

CLS

'Working example:

filetype.ext$(1) = "*.bat"
filetype.ext$(2) = "*.exe"
load.filelist2 "C:\", 2

FOR i% = 1 TO option.amount%
PRINT "short name: "; option.list$(i%)
PRINT "long name: "; long.filename$(option.list$(i%))
PRINT
NEXT i%

REM $STATIC
SUB exit.error (msg$)
PRINT msg$
SYSTEM
END SUB

FUNCTION get.file$ (fileSpec$, attr AS INTEGER) STATIC
DIM dta AS STRING * 44
fileSpecZ$ = fileSpec$ + CHR$(0)
DO
DIM regs AS regTypeX
regs.ax = &H1A00
regs.ds = VARSEG(dta)
regs.dx = VARPTR(dta)
INTERRUPTX &H21, regs, regs
IF fileSpecZ$ <> CHR$(0) THEN
regs.ax = &H4E00
regs.cx = attr
regs.ds = VARSEG(fileSpecZ$)
regs.dx = SADD(fileSpecZ$)
ELSE regs.ax = &H4F00
END IF
INTERRUPTX &H21, regs, regs
IF regs.flags AND 1 THEN get.file$ = "": EXIT FUNCTION
realAttr = ASC(MID$(dta, 22, 1))
IF realAttr AND attr THEN
null = INSTR(31, dta, CHR$(0))
get.file$ = MID$(dta, 31, null - 31)
EXIT FUNCTION
END IF
fileSpecZ$ = CHR$(0)
LOOP
END FUNCTION

FUNCTION is.file% (filename$)
len.filename% = LEN(filename$)
i% = 1
DO
temp% = ASC(MID$(filename$, i%, 1))
IF temp% < 32 THEN EXIT FUNCTION
IF temp% > 126 THEN EXIT FUNCTION
SELECT CASE temp%
CASE 34, 42, 47, 58, 60, 62, 63, 92, 124: EXIT FUNCTION
END SELECT
IF i% = len.filename% THEN EXIT DO
i% = i% + 1
LOOP
is.file% = 1
END FUNCTION

SUB load.filelist (path$, spec$)
path$ = short.filename(path$)
option.amount% = 0
file$ = get.file$(path$ + spec$, &H27)
DO
IF LEN(file$) = 0 THEN EXIT DO
option.amount% = option.amount% + 1
file$ = get.file$("", &H27)
LOOP
IF option.amount% = 0 THEN EXIT SUB
REDIM option.list$(1 TO option.amount%)
file$ = get.file$(path$ + spec$, &H27)
FOR i% = 1 TO option.amount%
option.list$(i%) = path$ + file$
file$ = get.file$("", &H27)
NEXT i%
END SUB

SUB load.filelist2 (path$, filetype.amount%)
path$ = short.filename$(path$)
option.amount% = 0
j% = 1
file$ = get.file$(path$ + filetype.ext$(1), &H27)
DO
IF LEN(file$) = 0 THEN
option.amount2%(j%) = option.amount%
j% = j% + 1
IF j% > filetype.amount% THEN EXIT DO
file$ = get.file$(path$ + filetype.ext$(j%), &H27)
END IF
option.amount% = option.amount% + 1
file$ = get.file$("", &H27)
LOOP

IF option.amount% = 0 THEN EXIT SUB

REDIM option.list$(1 TO option.amount%)
j% = 1
file$ = get.file$(path$ + filetype.ext$(1), &H27)
FOR i% = 1 TO option.amount%
IF i% > option.amount2%(j%) THEN
j% = j% + 1
file$ = get.file$(path$ + filetype.ext$(j%), &H27)
END IF
option.list$(i%) = path$ + file$
file$ = get.file$("", &H27)
NEXT i%
END SUB

FUNCTION long.filename$ (shortName$)
shortNameZ$ = shortName$ + CHR$(0)
longNameZ$ = SPACE$(261)
DIM regs AS regTypeX
regs.ax = &H7160
regs.cx = &H2
regs.ds = VARSEG(shortNameZ$)
regs.si = SADD(shortNameZ$)
regs.es = VARSEG(longNameZ$)
regs.di = SADD(longNameZ$)
INTERRUPTX &H21, regs, regs
IF regs.ax = &H2 THEN exit.error "Invalid component in directory path or drive letter."
IF regs.ax = &H3 THEN exit.error "Malformed path or invalid drive letter."
stringEnd = INSTR(longNameZ$, CHR$(0)) - 1
IF stringEnd >= 1 THEN longName$ = LEFT$(longNameZ$, stringEnd)
long.filename$ = longName$
END FUNCTION

FUNCTION short.filename$ (longName$)
longNameZ$ = longName$ + CHR$(0)
shortNameZ$ = SPACE$(128)
DIM regs AS regTypeX
regs.ax = &H7160
regs.cx = &H1
regs.ds = VARSEG(longNameZ$)
regs.si = SADD(longNameZ$)
regs.es = VARSEG(shortNameZ$)
regs.di = SADD(shortNameZ$)
INTERRUPTX &H21, regs, regs
IF regs.ax = &H2 THEN exit.error "Invalid component in directory path or drive letter."
IF regs.ax = &H3 THEN exit.error "Malformed path or invalid drive letter."
stringEnd = INSTR(shortNameZ$, CHR$(0)) - 1
IF stringEnd < 1 THEN shortName$ = "" ELSE shortName$ = LEFT$(shortNameZ$, stringEnd)
short.filename$ = shortName$
END FUNCTION


LongFilenames - page last edited 2004-01-17 18:09:29 by 172.171.163.45 (home) (edit)
Blast WIKI - by RoboticBoy - edited and tweaked for our evil purposes by Hexadecimal Disaster