Announcement

Collapse
No announcement yet.

Size of a Folder

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Size of a Folder

    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.

    ------------------
    -Greg
    -Greg
    [email protected]
    MCP,MCSA,MCSE,MCSD

    #2
    There's PB/DOS source code for that in Randall L. Glass' 4dirsize.zip.
    See http://www.powerbasic.com/files/pub/pbdos/tools/

    ------------------
    Tom Hanlin
    PowerBASIC Staff

    Comment


      #3
      I was hoping for some windows code that would work with a UNC path.

      I thought maybe an API existed for this.

      ------------------
      -Greg
      -Greg
      [email protected]
      MCP,MCSA,MCSE,MCSD

      Comment


        #4
        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

        Comment


          #5
          hi,
          borje hagsten posted some code a while ago, take a look at http://www.powerbasic.com/support/pb...ad.php?t=23275

          -p


          ------------------

          Comment


            #6
            gregery

            try this one:

            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(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
            ------------------
            [email protected]
            [email protected]
            www.dreammodel.dk

            Comment


              #7
              gregery,

              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
              ------------------
              [email protected]
              [email protected]
              www.dreammodel.dk

              Comment


                #8
                Peter,

                I ran your code, and it gpf'd on a unc path and my winnt and winnt\system
                directory.

                ------------------
                -Greg

                [This message has been edited by Gregery D Engle (edited August 28, 2001).]
                -Greg
                [email protected]
                MCP,MCSA,MCSE,MCSD

                Comment


                  #9
                  Gregery

                  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.

                  Regards
                  Peter

                  ------------------
                  [email protected]
                  [email protected]
                  www.dreammodel.dk

                  Comment


                    #10
                    Greg

                    Did you solve the gpf problem?

                    Peter

                    ------------------
                    [email protected]
                    [email protected]
                    www.dreammodel.dk

                    Comment


                      #11
                      After it gpf'd I decided to simply do this:

                      shell "cmd.exe /c dir uncpath /s >c:\tmp.tmp"

                      and then parse tmp.tmp

                      It works perfectly on win2k

                      ------------------
                      -Greg
                      -Greg
                      [email protected]
                      MCP,MCSA,MCSE,MCSD

                      Comment


                        #12
                        been away for a while. guess problem is solved by now, but anyway,
                        a while ago i posted another sample, doing exactly this to: http://www.powerbasic.com/support/pb...ad.php?t=23056


                        ------------------

                        Comment

                        Working...
                        X
                        😀
                        🥰
                        🤢
                        😎
                        😡
                        👍
                        👎