List files in a directory (recursively too!)

Поделиться
HTML-код
  • Опубликовано: 3 янв 2021
  • Source code can be found here:
    code-vault.net/lesson/18ec194...
    ===== Support us through our store =====
    code-vault.net/shop
    ===== Check out our website =====
    code-vault.net
    ===== Check out our Discord server =====
    discord.code-vault.net

Комментарии • 77

  • @robertwitt1276
    @robertwitt1276 3 года назад +24

    You are an awesome human being and have helped me out tremendously in my classes! keep it up man you are helping a lot of engineers out in this world

  • @Squish888
    @Squish888 2 года назад +9

    This guy is carrying me through my OS course.

  • @bozdogan_
    @bozdogan_ 3 года назад +6

    Thanks a lot you are really good at teaching. Loved how you explain things step by step and still keeping good pace.

  • @amyrs1213
    @amyrs1213 3 года назад +6

    Hey man, just discovered your channel! Thank you so much for your tutorials!

  • @Benjaskz
    @Benjaskz 2 года назад +2

    This really helped me a lot! you explain everything so well, thank you!

  • @thetablue8507
    @thetablue8507 3 года назад

    Your videos have been helpful as I am learning system calls in Linux. Thanks again

  • @michagolemo3765
    @michagolemo3765 3 года назад +1

    Great video! Thank you so much for your tutorials.

  • @dhruv_195
    @dhruv_195 2 года назад

    thank u so much for explaining everything so clearly, the video was really helpful

  • @nope7505
    @nope7505 Год назад

    thank you so much for your videos sir, you have helped so many people

  • @TheScoobysteve
    @TheScoobysteve 2 года назад

    Freggin awesome, man. Thanks for that!

  • @markthompson6552
    @markthompson6552 4 месяца назад

    You know a video is special when you have to log into like it.

  • @izotovadaria7151
    @izotovadaria7151 2 года назад

    The best tutorial for c, honestly

  • @adarshramlaul3299
    @adarshramlaul3299 Год назад

    You're the best. Thank you.

  • @newseven385
    @newseven385 2 года назад

    thank you so much. really helpful

  • @odiljonabduvaitov
    @odiljonabduvaitov 2 года назад +1

    Thank you so much!

  • @weihyac
    @weihyac 2 года назад +1

    Thanks so much

  • @newcomer3419
    @newcomer3419 Год назад

    Much thanks you dude from school 21!

  • @novigradmerchant2400
    @novigradmerchant2400 3 года назад +6

    First thanks for all your great tutorials. Secondly do you have any plan to make a video series about socket programming. I don't know maybe a simple chat app that communicate with 2 different people from 2 different locations or IPC through sockets etc... That would be great. Again thanks for everything ❤️

  • @Rust185
    @Rust185 2 года назад

    Great! Thanks a lot

  • @robmayol
    @robmayol Год назад

    Thank you for your wonderful videos! I found this one looking for a tutorial on using nftw which is a bit more complicated but there doesn't seem to be anything on RUclips about it. How did you get your video to show up when searching for "nftw c"??? Is there hope that you might make one? :)
    All the best,
    R

  • @dominikheinz2297
    @dominikheinz2297 3 года назад +2

    Cool video. Can you make one about unit testing too? :)

    • @CodeVault
      @CodeVault  3 года назад

      Haven't done unit testing on C but I'll see what I can find. Thanks for the suggestion!

  • @saraaljbn859
    @saraaljbn859 3 года назад

    YOU ARA AWESOME MAAAAAAAAAAAAAAAAAAAAN

  • @Anim233
    @Anim233 3 года назад +1

    nice video,bro! very informative. but can you explain me why there are "int argc,char* argv[]"in the arguments of main? i'm just a newbie into this, so i don't know much

    • @CodeVault
      @CodeVault  3 года назад

      Those aren't really needed but I always have them to make people ask about them haha.
      Here's a video explaining what's up with those: code-vault.net/lesson/dbijqbwu2a:1603733526118

  • @vibranteye4369
    @vibranteye4369 2 года назад

    Hello sir, this may be an odd question but shouldn't you allocate space for "entity"? EX: struct dirent *entity = malloc(sizeof(struct dirent) * 100);

    • @CodeVault
      @CodeVault  2 года назад

      No. Because the readdir function already allocates the memory for me and returns a pointer to that. Notice I use:
      entity = readdir(...);
      And not:
      *entity = readdir(...); // This would require allocation

  • @jeremiahlee9703
    @jeremiahlee9703 Год назад

    So is readdir similar to readline? As in does it read the next file/dir in the current dir?

  • @hamzahalamdeen2057
    @hamzahalamdeen2057 Год назад

    what extension are you using to auto prompt the type like ->d_name ???

    • @CodeVault
      @CodeVault  Год назад

      I'm using the C/C++ extension from Microsoft

  • @TheAbsoluteSir
    @TheAbsoluteSir 2 года назад

    Could you explain what your if statement means if it is != 0? I feel as if it's a bit unclear. Thanks!

    • @CodeVault
      @CodeVault  2 года назад

      If you look at the definition of strcmp: www.cplusplus.com/reference/cstring/strcmp/
      It returns 0 if the strings compared are the same. So the if condition is checking if the folder is NOT "." and NOT ".." (since we don't want to iterate through those)

  • @kamleshkailasjoshi2620
    @kamleshkailasjoshi2620 Год назад

    Is it possible to get the locations of all folders from top-level folder

    • @CodeVault
      @CodeVault  Год назад

      Yeah, you can simply print only when a folder is found and make sure to start from the "/" folder. You might need to launch the program with sudo

  • @anshugangwar7344
    @anshugangwar7344 2 года назад

    I want to pars folder at one level, don't want to go in all subdirectory recursively. Because required file present one directory below.
    Eg: There are multiple folder/Sub directory present inside A and B both We don't want to traverse inside those .
    folder structure is just for reference (but It's on linux)
    path\A
    ef.txt
    path\B
    ef.txt
    I want to get name of A and B directory only , and search file inside these not in sub directory.
    DIR * dir = opendir("path");
    if(NULL == dir)
    return;
    struct dirent * entity = readdir(dir);
    can you please suggest how to call readdir only one time and get list of all directory at one level .

    • @CodeVault
      @CodeVault  2 года назад

      I guess just remove the recursive call from inside the listFiles function.

  • @iulianab
    @iulianab 3 года назад

    Hi! I am trying to do the same thing but for new files added in the directory. I really don't know how. Can you suggest me something? Thank you!

    • @CodeVault
      @CodeVault  3 года назад +1

      How do you define new files? Files that were created not too long ago? Empty files?

  • @alphaedits1808
    @alphaedits1808 Год назад

    Sir what autocomplete extension do you use for c

    • @CodeVault
      @CodeVault  Год назад

      It's the C/C++ extension from Microsoft

  • @Carlos_hum
    @Carlos_hum 2 года назад

    Hi, I've watched your video and it was great, but when i tried to use your code for my needs I ran into problem that buffer is too small. Do you have any idea what can i do to repair that?

    • @CodeVault
      @CodeVault  2 года назад

      Where do you get the error? Maybe you need to have to make the char array larger

    • @Carlos_hum
      @Carlos_hum 2 года назад

      ​@@CodeVault I get it in strcat(path, entity->d_name); also i changed char path[100] = { 0 }; to char path[260] = { 0 }; . Also in error window is something like that
      File:
      minkernel\crts\ucrt\inc\corecrt_internal_string_templates.h
      Line: 50
      and I use listFiles("C:\\"); instead of listFiles(".");

  • @theawesome-est34
    @theawesome-est34 Год назад

    is there any way that i can scan for only .wav files
    i want to scan a folder and print all the .wav files present in it
    can anyone help?

    • @CodeVault
      @CodeVault  Год назад

      You can simply check if entity->d_name ends with .wav
      There are many ways of doing it, here's one of them:
      size_t len = strlen(entity->d_name);
      if (len >= 4 && strcmp(entity->d_name + len - 4, ".wav") == 0) {
      // this is a wav file
      }

  • @erik.rodriguez
    @erik.rodriguez 3 года назад

    Hello i wanted to know how can I pass different directories instead of “.”

    • @CodeVault
      @CodeVault  3 года назад

      Just pass an absolute or relative path instead of "."

  • @sakkamouid3343
    @sakkamouid3343 Год назад

    In line 15 , you just said if we printed a name of directory we should read the next one. Why would you even make a while loop? Because in this program it's about a one directory which is the current directory and that doesn't need a while loop. What am I not understanding right here?

    • @CodeVault
      @CodeVault  Год назад +1

      The code should work even if you have multiple directories inside the directory we are searching. That's why it's a while loop. If you only need the first one then you don't need the loop at all

  • @ishangupta8731
    @ishangupta8731 2 года назад

    If I try to run the program for large directory eg home
    it causes "stack smashing". How to avoid that.

    • @CodeVault
      @CodeVault  2 года назад

      Hmm... odd. Maybe try to change the program to iterative instead of recursive

    • @ishangupta8731
      @ishangupta8731 2 года назад

      @@CodeVault Sure I will try. Is there a way to make the program tail recursive to optimise recursion.

    • @alexlazea9978
      @alexlazea9978 2 года назад

      That is probably because it is recursive and if u have a very large number of files the recursive stack is huge, from what i read on stackoverflow this is a pretty bad way to do it.

    • @vibranteye4369
      @vibranteye4369 2 года назад

      @@alexlazea9978 Is that why? huh.

  • @yoan0608
    @yoan0608 3 года назад +2

    Cool, but can you make it in windows too. Thanks!

    • @CodeVault
      @CodeVault  3 года назад +1

      That's using the Win32 API. I'll look into it

  • @sanmeetwakchaure8530
    @sanmeetwakchaure8530 3 года назад

    Sir, d_type is not working in codeblocks. It shows error like "struct dirent has no member named d_type". What to do?

    • @CodeVault
      @CodeVault  3 года назад +1

      This is only for Linux. You gotta use the Win32 API on Windows

    • @sanmeetwakchaure8530
      @sanmeetwakchaure8530 3 года назад

      @@CodeVault will it work on virtual box ??

    • @CodeVault
      @CodeVault  3 года назад +1

      Yea, should be

    • @hershey3
      @hershey3 3 года назад

      @@CodeVault actually their is a dirent.h port for windows that even comes pre package with most compilers expected vs. d_type isnt supported. you need to use the stat() function.

  • @ibrarahmad8488
    @ibrarahmad8488 Год назад

    can't open sourse file dirent.h solution please

    • @CodeVault
      @CodeVault  Год назад

      dirent is only on Linux. You have to use the Win32 API if you're on Windows

  • @fakihe8147
    @fakihe8147 2 года назад

    It would be great if you could add Turkish subtitles to these quality videos.Because
    finding resources is a bit of a problem.

    • @CodeVault
      @CodeVault  2 года назад +1

      Hmm. An idea I have for the future is to hire someone to take care of subtitles for the videos or other content. Thanks for telling me, this is the first someone mentioned this issue

    • @fakihe8147
      @fakihe8147 2 года назад

      @@CodeVault Thank you for your consideration. I wish you a good day :)

  • @felixmaster7606
    @felixmaster7606 Год назад

    it cant find d_type in my code and I have the same code like you.... what now?

    • @CodeVault
      @CodeVault  Год назад

      From the documentation it should be there: www.man7.org/linux/man-pages/man3/readdir.3.html
      Maybe an import of dirent.h is missing?

    • @felixmaster7606
      @felixmaster7606 Год назад

      @@CodeVault no, my code is on windows

    • @CodeVault
      @CodeVault  Год назад

      What I'm showing in the video is for Linux only. You'll need to use the Win32 API for these on Windows

  • @Bastian-dm2pc
    @Bastian-dm2pc 7 месяцев назад

    not a good debug ure printf, a printf("here"); will be a better debug 🤣

    • @CodeVault
      @CodeVault  6 месяцев назад

      Maybe with a DEBUG preprocessor flag check, but usually you don't want to have output to stdout just for debugging. Just use gdb instead

  • @lkilgoretrout
    @lkilgoretrout 2 года назад

    Spectacular video ! I'm struggling in an assignment to do just this and this video was extremely helpful ! P.S. How did you get to load in VSCode ? GDB is a nightmare...

    • @CodeVault
      @CodeVault  2 года назад

      dirent is a Linux specific library, it won't work on Windows