You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
Anybody have some quick and dirty code to determine the size of
a given folder? I need to setup some monitoring of User's Home
Drives for audting purposes.
Short of doing a DIR on the entire file, I would take this function and run with it, but I do not know if there is a piece of the TYPE you can use to get that.
You may have to do a DIR (I have the code for it).
Code:
Function FolderExists(ByVal FolderSpec As String) Export As Long
Local hDir As Long
Local FindData As WIN32_FIND_DATA
FindData.dwFileAttributes = %FILE_ATTRIBUTE_DIRECTORY
hDir = FindFirstFile(ByVal StrPtr(FolderSpec), FindData)
If Bit(FindData.dwFileAttributes, 4) = 1 Then Function = %TRUE
If hDir = %INVALID_HANDLE_VALUE Then
Exit Function 'file not found
End If
FindClose hDir
End Function
'
'
'
'If that fails, go with this....I would test it, I can't remember if it's 100% functional but I think it is....
You'll need to do subfolders as well..
Function GetDirList(CurrentDir As String,wFileSpec As String,LfnOrSfn As Long) Export As String
Local x As Long
Local wCount As Long
Local hDir As Long
Local f As String
Local sTmp As String
Local St As String
Local ShortName As Asciiz * 64
Local LongName As Asciiz * 64
Local l_FileSpec As String
Local FindData As WIN32_FIND_DATA
If Len(wFileSpec) = 0 Then
If Right$(CurrentDir,1) = "\" Then CurrentDir = Left$(CurrentDir,Len(CurrentDir) - 1) 'Remove rigt "\"
l_FileSpec = CurrentDir + "\*.*"
Else
l_FileSpec = CurrentDir + "\" + wFileSpec
End If
FindData.dwFileAttributes = %FILE_ATTRIBUTE_DIRECTORY
hDir = FindFirstFile(ByVal StrPtr(CurrentDir), FindData)
If hDir = %INVALID_HANDLE_VALUE Then 'Directory did not exist
Function = "File or directory does not exist"
Exit Function
Else
sTmp = "Directory of " + CurrentDir + "\" + $CRLF
FindData.dwFileAttributes = %FILE_ATTRIBUTE_NORMAL Or %FILE_ATTRIBUTE_ARCHIVE Or %FILE_ATTRIBUTE_READONLY
hDir = FindFirstFile(ByVal StrPtr(l_FileSpec), FindData)
If IsFalse LfnOrSfn Then 'Use Long File Name otherwise use short name
LongName = FindData.cFileName
GetShortPathName LongName,ShortName, 63
FindData.cFileName = ShortName
End If
If hDir = %INVALID_HANDLE_VALUE Then
Function = "File not found"
Exit Function
End If
Do
If FindData.cFileName = "." Or FindData.cFileName = ".." Then Iterate
Incr wCount
If FindData.dwFileAttributes = %FILE_ATTRIBUTE_DIRECTORY Then
If IsFalse LfnOrSfn Then 'Use Long File Name
LongName = FindData.cFileName
If IsTrue GetShortPathName(LongName,ShortName, 63) Then FindData.cFileName = ShortName
End If
FindData.cFileName = "<" + FindData.cFileName + ">"
Else
sTmp = sTmp + PadString(Left$(FindData.cFilename,25),25,32) + " " + _
PadString(Format$(FindData.nFileSizeLow,"###,###,###"),15,32) + " " + _
padstring(FileDateTime(FindData),15,32) + $CRLF
End If
Loop While FindNextFile(hDir, FindData)
End If
FindClose hDir
Function = sTmp
End Function
'-----------------------------------------------------------------------------------
------------------
Scott Turchin
MCSE, MCP+I
Computer Creations Software
http://www.tngbbs.com/ccs
Scott Turchin
MCSE, MCP+I http://www.tngbbs.com
---------------------- True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi
#compile exe
#include "win32api.inc"
#include "scandir.inc" ' download: [url="http://www.powerbasic.com/support/pbforums/showthread.php?t=23120"]http://www.powerbasic.com/support/pbforums/showthread.php?t=23120[/url]
$targetfolder = "c:\winnt"
global foldersize as ext
' user-defined callback function defining the criteria of the scan
function scanfunc(byval file as string, byval path as string) as long
local hfile as long, fd as win32_find_data
hfile = findfirstfile(path + "\" + file, fd )
if hfile = %invalid_handle_value then exit function
findclose hfile
foldersize = foldersize + fd.nfilesizelow
end function
function pbmain
scandir $targetfolder, codeptr(scanfunc), %true
msgbox "size of folder '" + $targetfolder + "' is " + format$(foldersize) + " bytes"
end function
your question made me realize that a more general formulation
of scandir.inc could speed up the scan. here is the code using
version 3 of scandir.inc
regards
peter
Code:
#compile exe
#include "win32api.inc"
#include "scandir.inc" ' download: [url="http://www.powerbasic.com/support/pbforums/showthread.php?t=23120"]http://www.powerbasic.com/support/pbforums/showthread.php?t=23120[/url]
$targetfolder = "c:\winnt"
global foldersize as ext
' user-defined callback function defining the criteria of the scan
function scanfunc(wfd as win32_find_data) as long
foldersize = foldersize + wfd.nfilesizelow
end function
function pbmain
scandir $targetfolder, codeptr(scanfunc), %true
msgbox "size of folder '" + $targetfolder + "' is " + format$(foldersize) + " bytes"
end function
Are you sure you are using version 3 of ScanDir.inc and my last
example? I have no problems. I (and others) have used the original
code 100's of times without gpf's.
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment