Announcement

Collapse
No announcement yet.

FindFirstFile/FindNextFile

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

  • FindFirstFile/FindNextFile

    I'm getting sporatic results here, basically I have a folder that has nothing but subfolders in it.

    Each of those subfolders has ONLY files in it.

    So easy enough, read the folder to get the folder names, read the folder contents, add up the size of it, and move on.

    This is so I can organize the folders by size in preparation for burning to DVD, I need to get 4.7 gigs of folders moved into separate folders.

    Basically I have a few folders that have say, 350 mb of data in them but it's showing 167 or so (half?)


    Code:
    '#Register None
    #Option Version5
    #Compile Exe
    #Include "WIN32API.INC"
    Declare Function GetSizeofFolderinGB(ByVal InputDir As String) As Dword
    
    '============================================<WINMAIN>==================================================================
    Function WinMain (ByVal hInstance     As Long, _
                      ByVal hPrevInstance As Long, _
                      ByVal lpCmdLine     As Asciiz Ptr, _
                      ByVal iCmdShow      As Long) As Long
    
    
    Local f             As Asciiz * %MAX_PATH
    Local FindData      As WIN32_FIND_DATA
    Local hDir          As Long
    
    Local DiskNo        As Long 'Which DVD folder are we on.
    Local sTmp          As String
    Local FolderSize    As Dword
    Local TotalinMB     As Dword
    Local RootDir       As String
    Local SubDir        As String
    Local WorkingDir    As String
    Local ErrType       As Long
    Local lResult       As Long
    
    RootDir = "G:\UserData\"
    
    'Read in all folders
    FindData.dwFileAttributes = %FILE_ATTRIBUTE_DIRECTORY
    f = RootDir & "\*.*" 'Read all files, filter later
    hDir = FindFirstFile(f, FindData)
    If hDir = %INVALID_HANDLE_VALUE Then
        StdOut "Unale to read directory!"
        Exit Function
    End If
    
    Do
        WorkingDir = RootDir & FindData.cFileName
        FolderSize = GetSizeofFolderinGB(ByVal WorkingDir)
        TotalinMB = TotalinMB + FolderSize
    '    stdout WorkingDir & ": " & Format$(FolderSize)
    Loop While FindNextFile(hDir, FindData)
    'stdout "Total: " & Format$(TotalinMB)
    FindClose hDir
    End Function
    '---------------------------------------------------------------------------------
    Function GetSizeofFolderinGB(ByVal InputDir As String) As Dword
    Local hDir        As Long
    Local FolderSize  As Dword
    Local f           As Asciiz * %MAX_PATH
    Local FindData    As WIN32_FIND_DATA
    
    
    ChDir InputDir
    FindData.dwFileAttributes = %FILE_ATTRIBUTE_DIRECTORY
    f = "*.*" 'Read all files, filter later
    hDir = FindFirstFile(f, FindData)
    If hDir = %INVALID_HANDLE_VALUE Then
        Function = -1
        Exit Function
    Else
        FolderSize = (FindData.nFileSizeHigh * (%MAXDWORD+1)) + FindData.nFileSizeLow
    End If
    
    Do
        'FolderSize = FolderSize + ((FindData.nFileSizeHigh * &H100000000) + FindData.nFileSizeLow)
        'FolderSize = FolderSize + (FindData.nFileSizeHigh + FindData.nFileSizeLow)
        FolderSize = FolderSize + (FindData.nFileSizeHigh * (%MAXDWORD+1)) + FindData.nFileSizeLow
    Loop While FindNextFile(hDir, FindData)
    FindClose hDir
    Function = (FolderSize/1074)
    End Function
    '---------------------------------------------------------------------------------
    '---------------------------------------------------------------------------------
    '---------------------------------------------------------------------------------
    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

  • #2
    >Local FolderSize As Dword

    Big enough to hold files potentially > 4 GB? (Answer: no).

    Also
    Function name is FolderSizeinGB, when I think you meant FolderSizeInMB, except dividing by 1074 rather than 1024 is kind of interesting.

    First thing to try is making FolderSize a QUAD. You may be hitting an overflow error, which is not trappable.

    You might also want to assign the expression on "%MAXDWORD+1" to a QUAD variable and use that in your calculation.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      OK THat worked, I coulda swore I tried the QUAD before.

      Note: My conversion tool shows 1 Gb as 1073.7mb....right click properties on a folder and it now gives me exactly what I have for a calculation:

      Code:
      '#Register None
      #Option Version5
      #Compile Exe
      #Include "WIN32API.INC"
      Declare Function GetSizeofFolderinMB(ByVal InputDir As String) As Dword
      
      '============================================<WINMAIN>==================================================================
      Function WinMain (ByVal hInstance     As Long, _
                        ByVal hPrevInstance As Long, _
                        ByVal lpCmdLine     As Asciiz Ptr, _
                        ByVal iCmdShow      As Long) As Long
      
      
      Local f             As Asciiz * %MAX_PATH
      Local FindData      As WIN32_FIND_DATA
      Local hDir          As Long
      
      Local DiskNo        As Long 'Which DVD folder are we on.
      Local sTmp          As String
      Local FolderSize    As Quad
      Local TotalinMB     As Dword
      Local RootDir       As String
      Local SubDir        As String
      Local WorkingDir    As String
      Local ErrType       As Long
      Local lResult       As Long
      
      RootDir = "G:\UserData\"
      
      'Read in all folders
      FindData.dwFileAttributes = %FILE_ATTRIBUTE_DIRECTORY
      f = RootDir & "\*.*" 'Read all files, filter later
      hDir = FindFirstFile(f, FindData)
      If hDir = %INVALID_HANDLE_VALUE Then
          StdOut "Unale to read directory!"
          Exit Function
      End If
      
      Do
          WorkingDir = RootDir & FindData.cFileName
          FolderSize = GetSizeofFolderinMB(ByVal WorkingDir)
          TotalinMB = TotalinMB + FolderSize
          StdOut WorkingDir & ": " & Format$(FolderSize)
      Loop While FindNextFile(hDir, FindData)
      StdOut "Total: " & Format$(TotalinMB)
      FindClose hDir
      End Function
      '---------------------------------------------------------------------------------
      Function GetSizeofFolderinMB(ByVal InputDir As String) As Dword
      Local hDir        As Long
      Local FolderSize  As Quad
      Local f           As Asciiz * %MAX_PATH
      Local FindData    As WIN32_FIND_DATA
      
      Local Quadrafrier As Quad
      
      Quadrafrier = %MAXDWORD+1
      
      ChDir InputDir
      FindData.dwFileAttributes = %FILE_ATTRIBUTE_DIRECTORY
      f = "*.*" 'Read all files, filter later
      hDir = FindFirstFile(f, FindData)
      If hDir = %INVALID_HANDLE_VALUE Then
          Function = -1
          Exit Function
      Else
          FolderSize = (FindData.nFileSizeHigh * (Quadrafrier)) + FindData.nFileSizeLow
      End If
      
      Do
          FolderSize = FolderSize + (FindData.nFileSizeHigh * (Quadrafrier)) + FindData.nFileSizeLow
      Loop While FindNextFile(hDir, FindData)
      FindClose hDir
      Function = (FolderSize/1074)
      End Function
      '---------------------------------------------------------------------------------
      '---------------------------------------------------------------------------------
      '---------------------------------------------------------------------------------
      Last edited by Scott Turchin; 16 Jun 2008, 05:27 PM.
      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


      • #4
        You could have found that using
        Code:
        #DEBUG OVERFLOW ON
        ... oops, My Bad! That is a 'new feature suggestion' not a new feature. (Yet?)
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          If you have PBWIN8+ or PBCC4+ there is a MAK(QUAD... function...built into the compiler.
          Adam Drake
          PowerBASIC

          Comment

          Working...
          X