Files
scripts-bash/c-scripts/list_images.c
2026-03-30 15:48:25 -06:00

45 lines
1.1 KiB
C
Executable File

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
int main() {
DIR *dir;
struct dirent *entry;
char filepath[1024];
FILE *fp;
// Open the directory
dir = opendir("/home/arthur/Pictures");
if (dir == NULL) {
perror("opendir");
return 1;
}
// Create a new text file to save the list of images
fp = fopen("image_list.txt", "w");
if (fp == NULL) {
perror("fopen");
return 1;
}
while ((entry = readdir(dir)) != NULL) {
// Check if the entry is a file and ends with ".jpg" or ".jpeg"
if (entry->d_type == DT_REG && strstr(entry->d_name, ".jpg") != NULL) {
printf("Found image: %s\n", entry->d_name);
fprintf(fp, "%s\n", entry->d_name);
}
if (entry->d_type == DT_REG && strstr(entry->d_name, ".png") != NULL) {
printf("Found image: %s\n", entry->d_name);
fprintf(fp, "%s\n", entry->d_name);
}
}
// Close the directory and text file
closedir(dir);
fclose(fp);
return 0;
}