Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,846 questions

51,767 answers

573 users

How to get a list of all the files and sub-directories of specific directory in C

2 Answers

0 votes
#include <stdio.h>
#include <dirent.h>
 
void listFilesDir(const char *path) {
    struct dirent *dn;
    DIR *dir = opendir(path);
 
    if (!dir) 
        return; 
 
    while ((dn = readdir(dir)) != NULL) {
        printf("%s\n", dn->d_name);
    }
 
    closedir(dir);
}
 
int main()
{
    listFilesDir("d://api");
      
    return 0;
}
 
 
 
/*
run:
 
.
..
index.html
test
dev
app
   
*/

 

 



answered Jul 3, 2020 by avibootz
edited Jul 3, 2020 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>

// int system(const char *command)

// return -1 on error, and 0 if ok
  
int main(void) {
    int rv = system("dir d:\\api");
	printf("rv = %d\n", rv);
}
      
  
       
/*
run:
        
 Volume in drive D is New Volume
 Volume Serial Number is 1234-ABCD

 Directory of d:\api

04/17/2020  07:39 PM    <DIR>          .
04/17/2020  07:39 PM    <DIR>          ..
01/13/2020  11:22 AM               911 index.html
04/17/2020  07:39 PM    <DIR>          test
04/17/2020  07:39 PM    <DIR>          dev
04/17/2020  07:39 PM    <DIR>          app
               1 File(s)            911 bytes
               5 Dir(s)  1,387,565,969,408 bytes free
			    
rv = 0
   
*/

 



answered Jul 3, 2020 by avibootz
edited Jul 3, 2020 by avibootz
...