Created
August 4, 2013 05:41
-
-
Save CodeAngry/6149317 to your computer and use it in GitHub Desktop.
Three methods to get the file size of a path in Windows C++.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary>Values that represent the methods available to get the file size.</summary> | |
/// <remarks>Claude "CodeAngry" Adrian, 20 May 2012.</remarks> | |
typedef enum class file_size_methods { | |
create_file = 1, // CreateFile, GetFileSizeEx, CloseHandle (II speed, I reliable) | |
get_attributes = 2, // GetFileAttributesEx (I speed) | |
find_file = 3, // FindFirstFile, FindClose (III speed) | |
} file_size_method; | |
/// <summary>Gets athe file size using the chosen method.</summary> | |
/// <remarks>Claude "CodeAngry" Adrian, 20 May 2012.</remarks> | |
/// <param name="path">Full pathname of the file.</param> | |
/// <param name="method">(optional) The method.</param> | |
/// <returns>The file size.</returns> | |
inline ULONGLONG get_file_size(const TCHAR* path, file_size_method method){ | |
if(!path || !*path){ | |
SetLastError(E_INVALIDARG); | |
return 0; | |
} | |
LARGE_INTEGER file_size = { 0 }; | |
if(method == file_size_methods::create_file){ | |
HANDLE file = CreateFile(path, | |
GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, | |
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); | |
if(file && file != INVALID_HANDLE_VALUE){ | |
if(!GetFileSizeEx(file, &file_size)){ | |
file_size.QuadPart = 0; // clean-up on fail | |
} | |
CloseHandle(file); | |
} | |
}else if(method == file_size_methods::find_file){ | |
WIN32_FIND_DATA find_data; | |
HANDLE find_file = FindFirstFile(path, &find_data); | |
if(find_file && find_file != INVALID_HANDLE_VALUE){ | |
file_size.LowPart = find_data.nFileSizeLow; | |
file_size.HighPart = find_data.nFileSizeHigh; | |
FindClose(find_file); | |
} | |
}else if(method == file_size_methods::get_attributes){ | |
WIN32_FILE_ATTRIBUTE_DATA file_attr_data; | |
if(GetFileAttributesEx(path, GetFileExInfoStandard, &file_attr_data)){ | |
file_size.LowPart = file_attr_data.nFileSizeLow; | |
file_size.HighPart = file_attr_data.nFileSizeHigh; | |
} | |
} | |
return file_size.QuadPart; | |
} | |
/// <summary>Gets athe file size using the chosen method.</summary> | |
/// <remarks>Claude "CodeAngry" Adrian, 20 May 2012.</remarks> | |
/// <param name="path">Full pathname of the file.</param> | |
/// <param name="method">(optional) the method.</param> | |
/// <returns>The filesize.</returns> | |
inline ULONGLONG get_file_size(const std::basic_string<TCHAR>& path, file_size_method method){ | |
if(path.empty() || !path.front()){ | |
SetLastError(E_INVALIDARG); | |
return 0; | |
} | |
return get_file_size(path.c_str(), method); // forward the call to the const TCHAR* | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment