How to sort all the file names by their size in C++
In this tutorial, we will learn to sort all the file names by their size in C++. We will print the name and size of the files according to the required manner.
For this, we need <windows.h>, which is a header file in C++ which contains the declaration and all the windows API functions and provides lots of functions and attributes to deal with the file system.
We will use a vector of pairs to store the name and size of the file.
We will also use a sort() function to sort the files in the vector.
We will also use a comparator function to sort the file.
The sort function in C++ takes a third argument which just tells, how to sort the elements or on which criteria to sort the elements and that third argument is called a comparator function.
Formally, A comparator function is a function that takes two arguments x and y and returns the value indicating the relative order in which x and y should be sorted.
See: pairs in C++
Also See: vectors in C++
Comparator Function Code:
// Comparator function is made to sort the // in increasing order bool comparator(pair<string, int> file1, pair<string, int> file2) { return file1.second < file2.second; }
Working of sortFiles() function:
- We will use a sortFiles() function which will take path of the folder as an argument.
- Declare a vector of pair which will hold the file name and its size.
- Iterate through each and every file in the folder using a while loop until we reached the last file.
- While iterating the folder, push back the file name and file size into the vector.
- At last, return the vector of pair.
vector<pair<string, int>> sortFiles(string path) { // make the vector of pair to hold the name and size of the file vector<pair<string, int>> file_data; string fname = path + "\\*.*"; WIN32_FIND_DATA data; HANDLE h = FindFirstFile(fname.c_str(), &data); bool present = FindNextFile(h, &data); // check if any file present in the folder or not // This while loop will only check for all the files in the folder while (FindNextFile(h, &data)) { __int64 total_size; // store the size of the file string file_name = ""; // store the name of the file LARGE_INTEGER sz; // what we get here is the file size. // file sizes can be larger than 2 gigs therefore the size is written as two objects of DWORD type. // they combine to make 64 bit integer values. sz.LowPart = data.nFileSizeLow; sz.HighPart = data.nFileSizeHigh; total_size = sz.QuadPart; file_name = data.cFileName; file_data.push_back({file_name, total_size}); // push the file data into the vector } return file_data; }
Working of main() function:
- We will use the sort() method to sort the vector according to the size of the file.
- We have to make a comparator function that will sort the vector by only considering the size of the files.
- At last, we have to print the files with their names and sizes in ascending order.
Full code is given below:
#include<bits/stdc++.h> #include<windows.h> // #define int long long using namespace std; // Comparator function is made to sort the // in incresing order bool comparator(pair<string, int> file1, pair<string, int> file2) { return file1.second < file2.second; } vector<pair<string, int>> sortFiles(string path) { // make the vector of pair to hold the name and size of the file vector<pair<string, int>> file_data; string fname = path + "\\*.*"; WIN32_FIND_DATA data; HANDLE h = FindFirstFile(fname.c_str(), &data); bool present = FindNextFile(h, &data); // check if the any file present in the folder or not // This while loop will check for all the files in the folder while (FindNextFile(h, &data)) { __int64 total_size; // store the size of the file string file_name = ""; // store the name of the file LARGE_INTEGER sz; // what we get here is the file size. // file sizes can be larger than 2 gigs therefore the size is written as two objects of DWORD type. // they combine to make 64 bit integer values. sz.LowPart = data.nFileSizeLow; sz.HighPart = data.nFileSizeHigh; total_size = sz.QuadPart; file_name = data.cFileName; file_data.push_back({file_name, total_size}); } return file_data; } int main() { vector<pair<string, int>> files; string path = "C:\\Users\\91821\\Desktop\\important docs"; // enter the path here files = sortFiles(path); // Now sort files using comparator function // comparator function will sort the values according to size of the file. sort(files.begin(), files.end(), comparator); // print the files name with their sizes in ascending order. for (int i = 0; i < files.size(); i++) { cout << files[i].first << " " << (float)files[i].second/1024 <<"KB" << endl; } return 0; }
OUTPUT:
Payment Successful.pdf 68.9092KB HimanshuResume.pdf 90.4268KB syllabus.pdf 1171.1KB
Also Read: Find the largest file in a folder in C++
Leave a Reply