 Hey, everybody, it's Brian and in this video, we're going to talk about directories, specifically Q Durr. Funny name, Q Durr, kind of sounds like you're making fun of somebody. But remember, in Qt, everything starts with the letter Q and then after that is a self descriptive name of what's going on. You notice how I'm covering up the Q with the cursor. So it says Durr. We're talking about directories. We're also going to touch on file information and file information lists, which you guessed it just contains a lot of information. If we highlight Q Durr and hit F1 brings up the internal help system, which says the Q Durr class provides access to directory structures and their contents. And this is a pretty intuitive little class. There's not a whole lot of mysticism to this. It's just very straightforward. There are a couple little gotchas though. Now, speaking of directory trees, I'm assuming if you're watching this video, you have a vague concept of what a file system is and what a directory structure is and things like that. So this is my file system on a Linux box. And you can see it has folders and files in each folder can have subfolders and other folders and files and so on and so on. That creates a directory structure or a directory tree. Quick segue. Speaking of trees, this is an actual headline for my local newspaper here. Only in Michigan snow today, flirt with the 80 degrees tomorrow and frost again later in the week. What? Yes, this is Michigan. This is where I live. I don't know why I live here, but this is where I live. Anyways, because the temperatures all over the chart trees do this thing called a pollen attack. And I have to apologize. I'm very sensitive to pollen and I have asthma. So when the trees do these pollen attacks, yes, when I when the trees attack, I'm just miserable and I have a hard time breathing. So I may have to stop the video. I may have to edit it heavily. You may actually hear me cough. And I'm coming off of COVID-19 vaccine modern era shot number two. And I'm going to just be brutally honest with you. That shot beat me down. I was dead sick for days and I'm still not feeling 100%. I'm actually I can feel I'm losing my voice. But regardless, the show must go on, onward to creating tutorials in the magical land of kittens and unicorns. All right, we got to start somewhere. So let's find the current folder. I've got this little function here called test current. And this is going to be a little bit confusing, but I'm going to try to explain it in a very down to earth language. All right, so we're gonna say Q info. And we're gonna go Q dirt. Current path. Now when you see current path, a lot of people will tell you, Oh, this is where your applications installed. No, incorrect. This is your application's current directory. And yes, usually, current directory is the directory in which your application was started from, but not necessarily. This is the directory that, well, basically, your application says, this is where I'm going to start from, it could be your current directory where the application is, it could be something the user supplied, it could be something the operating system supplied. All right, so you may ask, how do I find where my application is, see this Q core application application dirt path returns the directory that contains the application executable. So those are the subtle differences. Don't get hung up and think current path is the current path of your application. Instead, you'd want to do Q core application application to her path. Like I said, super confusing, but I think we explained it pretty well. Now if I save and run, you'll see this out on the screen here. Tada, the absolute path absolute path means it's going to start at the very root of this and build the whole path out no hidden file names or anything like that. And this is where we're running from right here. This build QT six E man, I maybe I should change my naming convention, but that's a very long folder. If we wanted to, we can actually go out to the file system here and see, there it is, there's our executable on windows, you're going to see a dot exe. But there it is. Pretty interesting. Now, there is a different way of doing this. And I just want to for completeness include it here. So I'm going to say Q Dur, Dur, and I don't often say Dur, but sometimes I think I sound like I say Dur quite a bit. I actually hate the sound of my own voice. And I don't know why, but maybe it's just a me thing. We're going to say dur dot absolute path. Now you notice how there's an absolute file path, and an absolute path. What are the difference between these two here? So absolute file path, notice how it's got this, give me a file name. So it's going to give you the absolute path of something inside of the directory, where absolute path will give you the absolute path of the directory itself. That's another subtle gotcha. Just wanted you to be aware of that. Let's go ahead and save run and see what this looks like. And they should be identical because we're basically calling the same thing two different ways. It's super easy to work with once you wrap your head around it. Remember, when you get stuck with this class, because you often will just highlight cue dur on the screen and hit F1 and you have the entire help file right at your fingertips here. Okay, anybody who's watched my previous videos know I like to take the training wheels off as fast as possible and just throw people right in the deep end. And that's exactly what we're going to do. All right, what we're going to do is we're going to list things recursively. If you don't know what recursive means, let's just say we have a folder, we're going to start here. And we're going to list all these things. But if it's a folder, we're going to jump into it and list those things and if we find more folders, we're going to jump in there and jump in there and just keep going and going and going until we run out of folders. That's what recursive means. All right, this is not for the faint of heart, but cute makes it ridiculously simple. I'm going to say cue dur, root. And one of the constructors for a cue dur is we can give it a path. So I'm going to just take the path from our parameters list and notice recursive is automatically false. We're going to switch that later. But we're going to give it a path. And now we need to, you guessed it, make sure that exists. So I'm going to say, if not root, I could spell root. I did tell you I was having trouble breathing exists. Then we want to kindly hit the eject button. But before we do want to tell the user, Hey, something bad happened. We say Q warning path not found. And then let's just super convenient. Give him the path. Now, why do I do this? Because we're passing a string and we don't know if this thing actually exists. What if the user just hands us gibberish, or what if somebody copies and paste this function to their program, and they have an invalid path and they think the problems with the function and the reality is the path just simply doesn't exist. So it's always good to do a little bit of defensive programming here, just to make sure we've got some good data. Now, what we're passing to this is actually our current path. Now, remember, this is the application's current directory that current directory may or may not exist. That's why we got to be a little careful. Now, this is where things are going to get really murky, really, really fast. And that's why I wanted to cover this section. If you go to the help file for Q Durr, you say, I just want to print the contents of this folder. I want to know what folders and files are in here. And you just go, okay, scroll down. Well, let's start at the top. I don't see it. I don't see like a children or a files or a directories or anything like that. It just doesn't seem to be in there. Okay. Everything else is pretty self-explanatory, like make Durr, make path, refresh, remove Durr. But there's no, like, give me a list of files. But then you stumble across this right here, entry info list and entry list. This is actually what we're going to be working with here. So entry list will return a Q string list or a list of strings. Entry info list will return a list of Q file infos or more appropriately, a Q file info list. And this is why people get just horribly confused when they start working with this. We're going to work with entry info list. And it says returns a list of Q file infos. Ah, enter in this stuff. Q file info. Q file info is a class that provides system independent file information and it has pretty much anything you'd ever want to know about a file. Is it a file? Is it a folder? Is a symbolic link? How big is it? When was it created? So on and so on. I mean, I love this class. It's amazing. Now think of Q file info list as a Q list of you file infos just with a snazzy name. Ah, boy, that is confusing. All right, so let's go Q file info list. Let's just simply call it list because I like simple. I'm going to say root dot. And remember, we've got the entry list, which is a Q string list. Strings are useless to us. We want an entry info list. Aha. Oh, no. complexity. It wants filters. Well, I don't want a filter. I just want to list everything. But there's a problem with this. If we go into it and we say qder filter, and we just look at the filters, you'll see, ah, all there's all entries. That's exactly what I want right here. But let's just scroll down just out of curiosity. You see this. No dot. No dot and dot dot. No dot dot. What is this? It sounds like a foreign language. This is almost comical. This was so confusing to me when I first started Q. So if you don't know what these are, it's actually, it's a part of your file system. Let me explain. I'm going to open up a terminal here and I'm just going to say print working directory. I realize some of you watching this may not be on Linux, but basically this is the directory we're in home root shell. Okay. So if I say change directory and I go to dot, what happens? I'm in the same directory. So dot is simply your current directory. If I say CD dot dot, what happens? Notice how the folder name changed. If I say print working directory, I'm out actually up one. I'm in the home folder instead of home root shell. So dot dot is the parent folder. So this is what I mean when no dot. We don't want the current folder and we want no dot dot, no parent folder. Now this is why it's confusing. Why don't you want these? Think about it. If you're doing it recursively, the first thing you're going to come across is dot yourself. So you're going to go back to yourself and come into dot and come back to dot and come back and you see how you're now making an infinite loop. And if you somehow got past that, you now have the parent folder. So now you're going to scan yourself again and then come to the dot again and it just gets horrible. So we need to do the no dot and dot dot. And if you're still confused, just trust me on this, just do no dot and no dot dot. Sounds like something an Ewok from Star Wars would say. But anyways, now we want to go filter and you don't have to do filter, but I'm just putting it in there so people know where it is. And we're going to say all entries. Yes, you could say only directories or only files, but I want all of the entries. I want to know everything. Now that we have our list, we can simply work with it here. So I'm going to say for each. And of course, there's different ways you could do this. View file info. Let's call this f I in our list. And this is where the beauty of copy and paste comes in here. I'm going to do a little smidge of copy and paste just to save a moment of typing here. So what we're doing is we're just printing out a bunch of these little dashes. We're going to say the name and then the file info file name, file path, absolute file path, the birth time, which is actually when it's created. You notice we're doing a two string because that's going to be a Q date time. The last modified and the size. And then we're going to say a type and initially unknown. And then we're going to say if it is a dirt is a directory, do dirt, otherwise it's a file. And there's other things like you could say, is it a symbolic link? Is it a file and so on. When in doubt, check the help file. And then last but not least, I'm saying if recursive our little bull right here, then go ahead and recall this function using the file info absolute file path, meaning it's going to say if it is recursive, go ahead and rescan it. And if you wanted to, you could take this a step further and say if recursive and is a directory that we're not trying to recursively scan a file. All right, let's go ahead and let's test this. Now the first run, we're going to have it recursive false because false is the default here. Right here, maybe my mouse would cooperate. Save and run. Let's see what this looks like here. And you see, we're getting a lot of information here. Now, let's see what that looks like if we do recursive, save, run. Let's see what this looks like. If your guess we're going to get a lot more information, you are absolutely correct. Now this is one of the caveats with recursiveness, you have to be a little bit careful because you're going to go through everything. So if you did this, say at the root of your file system, it's going to go through every single folder on your system. So you gotta be a little bit careful here. Let's just jump right into the volcano here and see how crazy this can get. So I've got this function called test modify, which we are going to handle a qdir called root, meaning we're going to just say qdir current notice current path is a q string where current is a qdir already created and ready for us. So let's just dive in here. And I'm gonna say if root dot exists. And notice there's two different ways of doing it. So of course, the roots going to exist because it's current directory, but I want to see if a sub folder exists. And I want to see if that folder is named test. If that exists, well, I'm going to just hit the eject button because someone else is doing things on my computer, and I don't want to screw them up. Test folder already exists. Let's save and run and see what happens here. So no errors. So pretty safe to say that folder doesn't exist. Let's go out there and check it out. So I'm going to go to code and then right in here and to not know test folder, I'm going to just minimize that for now, we're going to reference back to that folder throughout this little section here. Now we're going to make a directory. Let's say if root dot mkdir. Notice how there's two different flavors mkdir and mk path these do two totally different things. mkdir will make one and only one directory in the current folder. mk path will make an entire directory structure. And what I mean by that creates a directory path. The function creates all parent directories. So if you say something like test slash a slash B slash C, so on and so forth, it will create all of that for you, but it does it very unapologetically meaning if it screws up, it's just going to try to keep going. You got to be a little careful using make path, but I don't want to deter you from it. We're going to use mkdir. Now what does mk stand for? It stands for make. If you're used to the command line, especially the Linux command line, that's exactly what it does. It's just going to make a directory. All right. Now that we're in here, we're going to make a qdir and let's call this dur and test. Wait, did you just see what happened here? I didn't have to give it the absolute path. I don't even know what folder I'm in at this point. I'm just going to say, Hey, take our current directory and make a sub folder, or I should say make a qdir pointing to the sub folder test that we just made. Whoa, that gets kind of confusing for newbies. But basically when you don't see a path, just think cute is going to do this in the current directory where we currently are. All right. Now that we've clarified that, let's go ahead and just do a little copy and paste. Tada. Double checker folder. There is no test folder in there. Let's go ahead and save run, see what happens. We created now suddenly it knows the absolute path. That's one thing that you're going to actually come to enjoy about q is you don't have to know the absolute path, but you can still get it very, very easily. All right. Now that that's in there, if we rerun this because we did this check right here, it will not progress. See, test folder already exists. Go ahead and wipe that out for now. Now inside here, what we're going to do is we're going to rename that folder. So I'm going to say if root dot rename, not remove rename. There we go. Test. We're going to rename that to S2. Now notice that's going to return a bull. And sometimes that can get a little tricky figuring out why that either doesn't work or seems to work, but doesn't actually work. Usually it's some weird thing with the OS, but 99% of the time it will just work. Now I'm going to copy and paste a little bit of gibberish in here. We're going to say we renamed it Durr absolute path, but we're also going to check to see if it exists. See something interesting happens here. We have Durr up here that's pointing to test. We're going to go to our route and rename test to test to. So drumroll, do you think Durr is going to exist or not exist? And let's double check. There's no test folder in there. Let's see this thing work. It's magic. False. It does not exist. Oh, oh, that is brutal. So you notice how it says renamed and it's now pointing to a folder that doesn't exist. This is what I mean by you got to be a little bit careful when you start making changes, because if you're still using this guy, but you just renamed it, you're now pointing to something that doesn't exist on the file system. And that gets a little bit scary. So when in doubt, if you're going to do some sort of operation like that, you should always check for existence. So say if root dot exists, that's two. Then go ahead and do something. Now, another little caveat here is every operating system is a little different. Some are case sensitive like Linux. So there's a difference between upper case test to and lower case test to you can have both of those folders. And on Windows, I believe it's not case sensitive. So there could be one and only one. So root dot exists, is this test to then we're going to go ahead and say if root dot exists, or I'm sorry, RM, there, which is shorthand for remove directory, you'll notice there is a remove and remove recursively. Those are two different things. This actually works with files. We want the RM or removed or and remove path. Now remember path is not a single. And you gotta be a little careful if you were to do a RM path. So this function will remove all parent directories in the dirt path provided they are empty, as opposed to make path. Notice that they have to be empty. Oh, that just will drive you absolutely crazy. There are ways of doing it. But it's really bad practice to just wipe something completely out. We're going to say. RM, there. And we're going to wipe out test two. Now looking at RM, there removes a directory directory must be empty for RM there to succeed this consider this a safety net. This is a safety net because if you try to remove a directory that's got files in it, it's going to say you may still need these files and it will return false from here. You guessed it. We're just going to say if we fail to remove this, then let's go ahead and queue info some stuff out, just letting us know, hey, we were unsuccessful in removing that. So let's go ahead and get rid of test two. So now we're going to load this up. We're going to create tests, rename it to test two and then delete test two. That says false on the screen. But if we go out here, sure enough, it's gone. Reason why it says false is because our queue dir, this little guy is still pointing to the directory that does not exist. Be a little bit careful when you're working with queue dir because as I pointed out several times and I know I sound repetitious, you can absolutely point to directories that are complete gibberish and don't exist and will never exist. So you have to be a little bit careful and make sure to check for existence before you try to do something with it. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers and we talk about everything technology related, not just the technology that you just watched. And if you want official training, I do develop courses out on Udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there. Drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those and as always, help me help you smash that like and subscribe button. The more popular these videos become, the more I'll create and publish out on YouTube. Thank you for watching.