How can I find the size of all files located inside a folder?

后端 未结 12 2407
萌比男神i
萌比男神i 2020-12-06 05:49

Is there any API in c++ for getting the size of a specified folder?

If not, how can I get the total size of a folder including all subfolders and files?

12条回答
  •  青春惊慌失措
    2020-12-06 05:57

    //use FAT32   
    #undef UNICODE        // to flag window deactive unicode
    #include  //to use windows api
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    #pragma pack(1)  //tell compiler do'nt do prag
    struct BPB
    {
    BYTE            JMP[3];
    BYTE            OEM[8];
    WORD            NumberOfBytesPerSector;
    BYTE            NumberOfSectorsPerCluster;
    WORD            NumberOfReservedSectors;
    BYTE            NumberOfFATs;
    WORD            NumberOfRootEntries16;
    WORD            LowNumbferOfSectors;
    BYTE            MediaDescriptor;
    WORD            NumberOfSectorsPerFAT16;
    WORD            NumberOfSectorsPerTrack;
    WORD            NumberOfHeads;
    DWORD           NumberOfHiddenSectors;
    DWORD           HighNumberOfSectors;
    
    DWORD           NumberOfSectorsPerFAT32;
    WORD            Flags;
    WORD            FATVersionNumber;
    DWORD           RootDirectoryClusterNumber;
    WORD            FSInfoSector;
    WORD            BackupSector;
    BYTE            Reserver[12];
    BYTE            BiosDrive;
    BYTE            WindowsNTFlag;
    BYTE            Signature;
    DWORD           VolumeSerial;
    BYTE            VolumeLabel[11];
    BYTE            SystemID[8];
    BYTE            CODE[420];
    WORD            BPBSignature;
    };
    //-----------------------------------------------------------
    struct DirectoryEntry
    {
    BYTE Name[11];
    BYTE Attributes;
    BYTE Reserved;
    BYTE CreationTimeTenth;
    WORD CreationTime;
    WORD CreationDate;
    WORD LastAccessTime;
    WORD HiClusterNumber;
    WORD WriteTime;
    WORD WriteDate;
    WORD LowClusterNumber;
    DWORD FileSize;   //acual size of file
    };
    //---------------------------------------------------
    void dirFunction(string s){
    string path = "\\\\.\\" + s + ":";
    HANDLE hFile = CreateFile(path.c_str(), GENERIC_READ|GENERIC_WRITE,
        FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);//open partition 
    BPB bootSector;//var from bootSector structure
    DWORD readBytes = 0;
    if (hFile == INVALID_HANDLE_VALUE)
    {
        cout << "Error " << GetLastError()<" ;
        }else{
    
            cout<<"\t" ;
        }
        clusterNumber = root[i].HiClusterNumber << 16;
        clusterNumber |= root[i].LowClusterNumber;
        cout <<"\t"< example E or G\n\n";
    cin>>directory;
    string path = "\\\\.\\" + directory + ":";
    cout<<"current directory is "<>swich_value;
    
    if(swich_value.at(1)!='i')
        cin>>file_name;
    string answer;
    switch(swich_value.at(1)){  
    case 'i':
        dirFunction(directory);
        cout<<"\nare  you want to do another process: y or n?";
        cin>>answer;
        if (answer.at(0)=='y')
            goto again;
        break;
    case 'y':
        typeFunction(removeExtention(file_name), directory);
        cout<<"\nare  you want to do another process: y or n?";
        cin>>answer;
        if (answer.at(0)=='y')
            goto again;
        break;
    case 'e':
        delFunction(removeExtention(file_name), directory);
        cout<<"\nare  you want to do another process: y or n?";
        cin>>answer;
        if (answer.at(0)=='y')
            goto again;
        break;
    
    
    }
    }
    

提交回复
热议问题