-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem-15
More file actions
60 lines (51 loc) · 1.74 KB
/
problem-15
File metadata and controls
60 lines (51 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
void createDirectory(const char *path) {
if (mkdir(path, 0755) == -1) {
perror("mkdir");
} else {
printf("Directory created: %s\n", path);
}
}
void organizeFiles(const char *sourceDir, const char *userDir) {
struct dirent *entry;
DIR *dp = opendir(sourceDir);
if (dp == NULL) {
perror("opendir");
return;
}
while ((entry = readdir(dp))) {
if (entry->d_type == DT_REG) { // Regular file
char newPath[1024], oldPath[1024];
snprintf(oldPath, sizeof(oldPath), "%s/%s", sourceDir, entry->d_name);
snprintf(newPath, sizeof(newPath), "%s/%s", userDir, entry->d_name);
if (rename(oldPath, newPath) == -1) {
perror("rename");
} else {
printf("Moved file %s to %s\n", oldPath, newPath);
}
}
}
closedir(dp);
}
int main() {
const char *masterDir = "master_directory";
const char *users[] = {"user1", "user2", "user3"};
const int numUsers = sizeof(users) / sizeof(users[0]);
// Create master directory
createDirectory(masterDir);
// Create user directories and organize files
for (int i = 0; i < numUsers; ++i) {
char userDir[1024];
snprintf(userDir, sizeof(userDir), "%s/%s", masterDir, users[i]);
createDirectory(userDir);
// Here we organize files for each user. For simplicity, we use the same source directory.
// In a real scenario, you would have separate source directories for each user.
const char *sourceDir = "."; // Current directory
organizeFiles(sourceDir, userDir);
}
return 0;
}