How to find the largest file in a folder in C++
In this tutorial, we will learn how to find the largest file in a folder using C++. The simplest approach for this purpose is to use a header file named as <windows.h>.
<windows.h>: It is a header file defined in C and C++ programming languages. It contains the declaration of all the windows APIs functions and also it provides lots of built-in functions to deal with windows files and folders.
We will also use a pair container to store the destination and size of the largest file.
Check for reference: pair in C++
Approach:
- We will use the searchLargestFile() function to calculate the largest file in the folder.
- We will travel recursively because if there is any other folder inside that folder then we will also travel into that folder.
- We will calculate the size of each and every file by using a while loop and continue until we reached at every file.
- Declare the pair container which will hold two values: first is the destination of file and second is the size of the file and also set the return type of the function to pair<string, __int64>.
- At last, return the pair, extract the data from pair and print the data.
Working of searchLargestFile() function:
- Declare a pair container named largest_file to hold the maximum size of file and file name (Eg.pair<string, int> variable_name).
- Find the first file name in the directory using FindFirstFile function.
- Start traversing each and every file in that directory until we reached the last file.
- Use cFileName attribute for the name of the file and QuadPart for the size of the file.
- At last, return the largest_file variable containing the required result.
Implementation of SearchLargestFile function in C++ programming:
pair<string, __int64> searchLargestFile(string path) { WIN32_FIND_DATA data; string fname = path + "\\*.*"; // for storing file name pair < string, __int64 > largest_file; HANDLE h = FindFirstFile(fname.c_str(), &data);//This function will return the first file of the folder. while (FindNextFile(h, &data)) { if ( (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) { // checking it is folder or file // Now skip those file whose name starts with "." and also check for ".." if ( strcmp(data.cFileName, ".") != 0 && strcmp(data.cFileName, "..") != 0) // check for subdirectory { fname = path + "\\" + data.cFileName; //Declare a temporary result pair<string, __int64> temp_result; // recursion: Check the largest file in that subdirectory temp_result = searchLargestFile(fname); if (temp_result.second > largest_file.second) largest_file = temp_result; } } else { 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; if ((__int64)sz.QuadPart > largest_file.second) { fname = path + "\\" + data.cFileName; //cFileName provides the name of th file which is under processing. largest_file.first = fname; largest_file.second = sz.QuadPart; } } } return largest_file; }
The different functions and attributes used in the above code are:
FindFirstFile: This function finds the first file in the folder.
FindNextFile: It finds the next file in the folder.
cFileName: It just returns the name of the file under processing.
See full code below:
#include<iostream> #include<windows.h> #include<string> using namespace std; pair<string, __int64> searchLargestFile(string path) { WIN32_FIND_DATA data; string fname = path + "\\*.*"; // for storing file path pair < string, __int64 > largest_file; HANDLE h = FindFirstFile(fname.c_str(), &data); //This function will return the first file of the folder. while (FindNextFile(h, &data)) { if ( (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) { // checking it is folder or file // Now skip those file whose name starts with "." and also check for ".." if ( strcmp(data.cFileName, ".") != 0 && strcmp(data.cFileName, "..") != 0) // check for subdirectory { fname = path + "\\" + data.cFileName; //Declare a temporary result pair<string, __int64> temp_result; // recursion: Check the largest file in that subdirectory temp_result = searchLargestFile(fname); if (temp_result.second > largest_file.second) largest_file = temp_result; } } else { 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; if ((__int64)sz.QuadPart > largest_file.second) { fname = path + "\\" + data.cFileName;//cFileName provides the name of th file which is under processing. largest_file.first = fname; largest_file.second = sz.QuadPart; } } } return largest_file; } int main(int argc, char* argv[]) { __int64 size = 0; pair < string, __int64 > result = searchLargestFile("C:\\Users\\91821\\Desktop\\important docs"); // size is in megabytes cout << "File is: " << result.first << " FileSize: " << (float)result.second / (1024 * 1024) << "MB" << endl; // give folder path here return 0; }
Output:
File is: C:\Users\91821\Desktop\important docs\d\syllabus.pdf FileSize: 1.14365MB
I found it a very helpful, very concise explanation. love it.