#getting file size (windows fileapi.h)

25 messages · Page 1 of 1 (latest)

fickle kayak
#

https://hastebin.skyra.pw/pihomozawa.cc

I want to write code that will traverse the entire filesystem and generate a file with lines like C:\full\file\path fileSize

In line 59 I tried using both FileSize and getFileSize function and noticed that the FileSize one (which calls GetFileAttributesEx) is much faster, but what worries me is a warning given by my IDE on line 9

I don't understand it, am I doing this the right way?

valid grailBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

fickle kayak
#

here's the warning:

blazing sigil
#

Why not just use std::filesystem::recursive_directory_iterator + std::filesystem::file_size?

#

To answer your question though: You've got the wrong data type. You're trying to convert from an unsigned type to a signed type. You can do an explicit cast or fix the underlying type.

fickle kayak
#

omg these functions are cool, I didn't know they existed, tyvm for telling me about it, I'll look into it and use them

regarding data types, I can see that LARGE_INTEGER.HighPart is a LONG while LowPart is DWORD, but fad.nFileSizeHigh and low are both DWORDS, unsigned integers
In this case, would static_cast<LONG>(fad.nFileSizeHigh) work?

blazing sigil
#

yes

#

And std::filesystem is pretty good for general purpose code. It's been around since C++17. All modern compilers have support for it

fickle kayak
#

thank you very much!

#

!solved

valid grailBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

fickle kayak
# blazing sigil Why not just use `std::filesystem::recursive_directory_iterator` + `std::filesys...

unfortunately, using std::filesystem::recursive_directory_iterator results in some weird behavior when called on the root dir (C:\\):

terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
  what():  filesystem error: cannot increment recursive directory iterator: Invalid argument```with just this:```cpp
for (const auto& entry : std::filesystem::recursive_directory_iterator(root_path)) {
}```
#

and it doesn't look like it provides any easy control over that

blazing sigil
#

root path should be std::filesystem::current_path().root_path() and that'll get the path of the root

fickle kayak
#

if you mean like this...it's the same thing

#

I believe what's making errors is that some directories are not accessible, e.g. no permissions

naive silo
fickle kayak
#

winapi calls, specifically the code i sent on hastebin, works without problems

naive silo
#

then debugging may be a good choice

fickle kayak
clever jungle
echo tendon
#

those should help if you want to mess with permissions through those functions. or if you just want it to continue past what it can't enter due to permissions you can probably try/catch around that stuff. i'm pretty sure most of those functions will throw when it comes across a situation like that.

fickle kayak