CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    Getting Free Disk Space of certain directory

    Code:
    _int64 free_space_64bit;
    PULARGE_INTEGER lpFreeBytesAvailable, lpTotalNumberOfBytes,lpTotalNumberOfFreeBytes;
    //char currentPath[MAX_PATH];
    //GetCurrentDirectoryA(MAX_PATH, currentPath);
    GetDiskFreeSpaceExA("H:\\C", lpFreeBytesAvailable, lpTotalNumberOfBytes,lpTotalNumberOfFreeBytes);
    free_space_64bit = lpFreeBytesAvailable->HighPart << 32 | lpFreeBytesAvailable->LowPart;
    This directory "H:\\C" does exist, if I comment out the GetDiskFreeSpaceExA line, the program doesn't crash, but it leads to some peculiar results (some uninitialized and random value, but at least it doesn't crash)
    Any ideas?
    Thanks
    Jack
    Last edited by lucky6969b; November 23rd, 2014 at 07:28 AM.

  2. #2
    Join Date
    Dec 2010
    Posts
    907

    Re: Getting Free Disk Space of certain directory

    Still not working
    Code:
    LPDWORD lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters,  lpTotalNumberOfClusters;
    //char currentPath[MAX_PATH];
    //GetCurrentDirectoryA(MAX_PATH, currentPath);
    GetDiskFreeSpaceA("H:\\C",lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters);
    free_space_64bit = *lpBytesPerSector * *lpSectorsPerCluster * *lpNumberOfFreeClusters;

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Getting Free Disk Space of certain directory

    The pointers you're passing as arguments aren't initialized.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Getting Free Disk Space of certain directory

    Code:
    DWORD SectorsPerCluster, BytesPerSector, NumberOfFreeClusters,  TotalNumberOfClusters;
    GetDiskFreeSpaceA("H:\\C",&SectorsPerCluster, &BytesPerSector, &NumberOfFreeClusters, &TotalNumberOfClusters);
    free_space_64bit = BytesPerSector * SectorsPerCluster * NumberOfFreeClusters;
    You need to pass the address of the variables used. GetDiskFreeSpaceEx() is used similarly.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Dec 2010
    Posts
    907

    Re: Getting Free Disk Space of certain directory

    Thanks

  6. #6
    Join Date
    Dec 2010
    Posts
    907

    Re: Getting Free Disk Space of certain directory

    But one thing, is that how come this api would tell that a "directory" has 3Gigs but the total disk space for the drive is 20Gigs up
    What is the true meaning of the free disk space of a "directory"?
    Thanks
    Jack

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Getting Free Disk Space of certain directory

    There's such a thing called quota. Physical disk free space is a physical limit, but user's quota is a virtual one.
    Last edited by Igor Vartanov; November 24th, 2014 at 02:47 PM.
    Best regards,
    Igor

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Getting Free Disk Space of certain directory

    Besides quota there might be another sort of limit, not that obvious one. In case the directory is a symlink pointing to a directory hosted on other physical drive, the real directory limit is the corresponding drive free space.
    Best regards,
    Igor

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Getting Free Disk Space of certain directory

    hence... if you want the free space on the volume, you should query the free space for the root path.
    Anything else might be more/less than that depending on what the path really is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured