• How To Loop Through All files and folders in a directory
    7 replies, posted
I can't figure out how to get a list of files and folders in a directory. I would like two arrays/vectors one with file names and one with folders name. I believe I would need the <filesystem> library but I don't see a function that can get files or directories from a base one. EX: [CPP] // Is there anything like this that would work const char *files[] = GetFiles( "C:/Base/dir/" ); const char *folders[] = GetFolders( "C:/Base/dir/" ); [/CPP]
Honestly though generally it's a much easier process to just write a config file that lists the directories to pull from and organise a standardised folder format for your program to pull from. If you absolutely need to pull the list , IE for a mods folder in a game, there's plenty of ways to do it. I'll give you a hint, if you're using c++17 you can find if a specific file is a directory by checking if the [CODE].status().type()[/CODE]of it is equal to [CODE] std::filesystem::file_type::directory[/CODE] If it's not a directory, that probably makes it a file yeah? Now just collect the list of files in a directory and check them, if they are a directory you can shuffle them into a directory vector, you'll probably want to use a vector or other dynamic container because you're not going to be working with a static amount of potential folders if you're collecting them at run-time. If you are working with a static amount of folders you should just hard-code the whole thing and use config files to define each directory, much less waste. You generally will want to standardise the file organisation as much as possible since having to account for stupid amounts of dynamic naming, directories and other misc organisation is a waste of time and can complicate the flow of production further down the line in a project when dev B does shit in a weird fashion and dev C can't understand how to contribute their own resources because of it, etc. Easier to just make one standard way and tell everyone else to get fucked or produce a better standard for you to implement. I don't know your specific application so I can't say whether that advice is valid or not, but just something to think about.
[QUOTE=F.X Clampazzo;52818175]Honestly though generally it's a much easier process to just write a config file that lists the directories to pull from and organise a standardised folder format for your program to pull from. If you absolutely need to pull the list , IE for a mods folder in a game, there's plenty of ways to do it. I'll give you a hint, if you're using c++17 you can find if a specific file is a directory by checking if the [CODE].status().type()[/CODE]of it is equal to [CODE] std::filesystem::file_type::directory[/CODE] If it's not a directory, that probably makes it a file yeah? Now just collect the list of files in a directory and check them, if they are a directory you can shuffle them into a directory vector, you'll probably want to use a vector or other dynamic container because you're not going to be working with a static amount of potential folders if you're collecting them at run-time. If you are working with a static amount of folders you should just hard-code the whole thing and use config files to define each directory, much less waste. You generally will want to standardise the file organisation as much as possible since having to account for stupid amounts of dynamic naming, directories and other misc organisation is a waste of time and can complicate the flow of production further down the line in a project when dev B does shit in a weird fashion and dev C can't understand how to contribute their own resources because of it, etc. Easier to just make one standard way and tell everyone else to get fucked or produce a better standard for you to implement. I don't know your specific application so I can't say whether that advice is valid or not, but just something to think about.[/QUOTE] Well I'm trying to make a program that uses Lua scripting and I'm able to get the Lua working fine, but I need to find all Lua files in an addons folder or something as such. And I can't find anything that gets files or folders.
Well sadly I know almost nothing about LUA so I can't help you. Should probably specify in the OP that you're using LUA.
Is this for Garry's Mod? If so you're in the wrong section and I can move it to the right one.
If this is C++ then go get the Boost::Filesystem libarary from [URL="http://www.boost.org/"]http://www.boost.org/[/URL]
[QUOTE=DarkCarnage;52821394]If this is C++ then go get the Boost::Filesystem libarary from [URL="http://www.boost.org/"]http://www.boost.org/[/URL][/QUOTE] or, if you're able to access the experimental filesystem (MSVC has it as of 2015, Clang+GCC have it as of late too), you can do something like what I do in this code: define a struct/tuple for holding file information you want to retrieve: [url]https://github.com/fuchstraumer/VulpesRender/blob/f77cde39646b1047b4da5b1604859d16b554a797/include/util/Filesystem.hpp#L13[/url] define a method to construct this pack of information: [url]https://github.com/fuchstraumer/VulpesRender/blob/f77cde39646b1047b4da5b1604859d16b554a797/include/util/Filesystem.hpp#L15[/url] iterate through a directory, constructing the file information for each entry in the directory: [url]https://github.com/fuchstraumer/VulpesRender/blob/f77cde39646b1047b4da5b1604859d16b554a797/include/util/Filesystem.hpp#L91[/url] you can also use recursive_directory_iterator, I guess, but then you might want some better way of storing the file info that respects the directory heirarchy and structure. [URL="http://en.cppreference.com/w/cpp/experimental/fs"]check the filesystem docs for more[/URL]. i'll elaborate further if needed, too. I'd say avoid boost if you can use the std filesystem, it's based on the boost one and doesn't require skullfucking yourself with the fuckload of headers boost requires. despite being experimental, I've not had any issues with the filesystem implementation on any OS (I've used it on Windows, Linux, and Mac OSX with MSVC, GCC, and clang just fine as part of VulpesRender) and it makes loads of things just so much easier. [editline]25th October 2017[/editline] enabling the filesystem can be tricky w/ clang though, lmk if you need help with that
Thank you all and this is for a C++ project not gmod
Sorry, you need to Log In to post a reply to this thread.