Post by blametroi on Jul 20, 2019 9:51:46 GMT -5
A lot of crud ends up in your %PATH% variable on Windows; installers and uninstallers are sloppy. I created validatepath.bas to try out a few things in LB and am sharing in case anyone wants more examples of API calls.
From Kernel32 this calls GetEnvironmentVariableA to read %PATH% into a string buffer, and then on each directory GetFileAttributesA is called to see if the directory exists. Along the way WORD$ is used to extract the individual directory names and FILES is used to see if the directory contains any files.
' validatepath.bas - utility to verify directories in your path
'
' troy brumley, july 2019.
'
' apis used from kernel32:
'
' GetEnvironmentVariableA to retrieve full path string
' if output buffer is too small nothing useful is returned
' if an invalid variable name is passed, result is 0
'
' GetFileAttributesA to retrieve basic attributes for an entry in the path
' result is a dword bitmask of the file or directory attributes,
' if the operation fails for any reason, -1 is returned and it is
' assumed the directory does not exist
print "validatepath -- validate that directories listed in your path"
print "exist and are not empty."
pathName$ = "PATH"
pathValue$ = space$(32766)+chr$(0)
n = 32767
maxdirs = 500
dim pathDirs$(maxdirs)
dim emptyDirs$(maxdirs)
dim missingDirs$(maxdirs)
open "kernel32.dll" for dll as #kernel32
calldll #kernel32, "GetEnvironmentVariableA", _
pathName$ as ptr, _
pathValue$ as ptr, _
n as long, _
result as long
close #kernel32
if result > n then
print "error -- path too long"
goto [endit]
end if
if result = 0 then
print "error -- path variable not found"
goto [endit]
end if
'
' trim zero byte crap
'
pathValue$ = left$(pathValue$, instr(pathValue$, chr$(0)) - 1)
' separate path into distinct directories, on
' windows directory names in the path are delimited
' by a semicolon
i = 0
do
i = i + 1
d$ = word$(pathValue$, i, ";")
pathDirs$(i) = d$
loop while d$ <> "" and i <= maxdirs
nDirs = i - 1
j = 0
k = 0
for i = 1 to nDirs
if exists(pathDirs$(i)) then
dim echeck$(10,10)
files pathDirs$(i), echeck$()
if val(echeck$(0,0)) = 0 then
' directory looks empty
k = k + 1
emptyDirs$(k) = pathDirs$(i)
end if
else
' directory is missing
j = j + 1
missingDirs$(j) = pathDirs$(i)
end if
next
print "total path directories: ", nDirs
print "missing path directories: ", j
print "empty path directories: ", k
if j <> 0 then
print "path contains missing directories!!!"
for i = 1 to j
print missingDirs$(i)
next i
end if
if k <> 0 then
print "path contains empty directories!!!"
for i = 1 to k
print emptyDirs$(i)
next i
end if
'
' and all done
'
[endit]
print "end of validatepath"
end
'
' check for file/directory existence
' this check is simplistic, any error is assumed to be a not found, which
' should be fine on a typical desktop
'
function exists(fn$)
open "kernel32" for dll as #kernel32
calldll #kernel32, "GetFileAttributesA", fn$ as ptr, result as long
close #kernel32
' if result = -1 then print ">>>debug<<<"; fn$; ", ", result
exists = (result <> -1)
end function