C Program Introduction In this assignment, we will develop a tool to organize a
ID: 3817287 • Letter: C
Question
C Program
Introduction In this assignment, we will develop a tool to organize a collection of files by creating directories, renaming some files, and moving some files into those directories. The tool is called organizer. Given a directory filled with various kinds of files, the organizer tool will create a subdirectory for each type of file, and move each file into the appropriate subdirectory. It will then organize each subdirectory based on the file type and the naming scheme. File Names and Formats The organizer program expects the files to adhere to special naming conventions. Recall that the extension of a filename imposes no format on the file itself. The purpose of an extension is to inform users and programs what the expected format is for that file. However, if a file has a particular extension (e.g. docx, there is no garauntee that the file is actually that type of file (i.e a docx file might not actually be a Microsoft Word file). A program that opens that file (e.g. Microsoft Word) is making an assumption, and trusting whoever created the file, that the extension accu- rately reflects the underlying format. Our organizer program will also make assumptions based on the given file extension. Therefore, you, as the programmer, can assume the following. 1. Each file will have an extension. In other words, each filename will contain at least one period character The substring starting with the last period character and ending with the last character in the filename is the extension. For example: In the filename groceries.txt, the substring '.txt' is the extension. In the filename birthday.party the substring jpg' is the extension 2. For the extensions mp3, .mkv, .docx, .txt, .jpg, and .png, we can safely assume that the fileExplanation / Answer
#include <dirent.h>
#include <stdio.h>
#include <string.h>
/*
void organize_movie(const char*, const char*, const char*);
void organize_mp3(const char*, const char*, const char*);
*char concat(int, ...)
*char get_filename_ext(const char*);
*/
void organize_movie(const char *filename, const char *organizer, const char *directorypath ) {
int i;
int counter = 0;
for (i=0; s[i]; s[i]=='.' ? i++ : *s++);
p = strtok (filename,"-");
char *org_file_name;
org_file_name = filename;
if(i > 1){
//for movies
directorypath = concat(3, directorypath, "/", "movies");
mkdir(directorypath);
while (p!= NULL)
{
if(counter == 1){
//last step
rename(concat(3, organizer, "/", org_file_name), concat(3, directorypath, "/", p));
}else{
directorypath = concat(3, directorypath, "/", p);
mkdir(directorypath);
p = strtok (NULL, ",:");
}
counter++;
}
}
else{
//for shows
directorypath = concat(3, directorypath, "/", "shows");
mkdir(directorypath);
while (p!= NULL)
{
if(counter == 3){
//last step
rename(concat(3, organizer, "/", org_file_name), concat(3, directorypath, "/", p));
}else{
directorypath = concat(3, directorypath, "/", p);
mkdir(directorypath);
p = strtok (NULL, ",:");
}
counter++;
}
}
}
void organize_mp3(const char *filename, const char *organizer, const char *directorypath ) {
int counter = 0;
p = strtok (filename,"-");
char *org_file_name;
org_file_name = filename;
directorypath = concat(3, directorypath, "/", "music");
mkdir(directorypath);
while (p!= NULL)
{
if(counter == 2){
//last step
rename(concat(3, organizer, "/", org_file_name), concat(3, directorypath, "/", p));
}else{
directorypath = concat(3, directorypath, "/", p);
mkdir(directorypath);
p = strtok (NULL, ",:");
}
counter++;
}
}
const char *get_filename_ext(const char *filename) {
//get the last dot
const char *dot = strrchr(filename, '.');
if(!dot || dot == filename) return "";
return dot + 1;
}
char* concat(int count, ...)
{
va_list ap;
int i;
// Find required length to store merged string
int len = 1; // room for NULL
va_start(ap, count);
for(i=0 ; i<count ; i++)
len += strlen(va_arg(ap, char*));
va_end(ap);
// Allocate memory to concat strings
char *merged = calloc(sizeof(char),len);
int null_pos = 0;
// Actually concatenate strings
va_start(ap, count);
for(i=0 ; i<count ; i++)
{
char *s = va_arg(ap, char*);
strcpy(merged+null_pos, s);
null_pos += strlen(s);
}
va_end(ap);
return merged;
}
int main(void)
{
DIR *d;
struct dirent *dir;
char *file_name;
char *extension;
char *tmp_dir;
char *organizer, *directorypath;
d = opendir(organizer);
if (d)
{
//read all files in a folder
while ((dir = readdir(d)) != NULL)
{
file_name = dir->d_name;
extension = get_filename_ext(file_name);
//if file is mp3
if(strcmp(extension, "mp3") == 0){
organize_mp3(file_name, organizer, directorypath);
}
//if file is movie
else if(strcmp(extension, "mkv") == 0){
organize_movie(file_name, organizer, directorypath);
}
//if file is image
else if(strcmp(extension, "jpg") == 0 || strcmp(extension, "png") == 0){
tmp_dir = concat(3, directorypath, "/", "pictures");
mkdir(tmp_dir);
rename(concat(3, organizer, "/", file_name), concat(3, tmp_dir, "/", file_name));
}
//if file is doc/text
else if(strcmp(extension, "docx") == 0 || strcmp(extension, "txt") == 0){
tmp_dir = concat(3, directorypath, "/", "documents");
mkdir(tmp_dir);
rename(concat(3, organizer, "/", file_name), concat(3, tmp_dir, "/", file_name));
}
else{
tmp_dir = concat(3, directorypath, "/", "others");
mkdir(tmp_dir);
rename(concat(3, organizer, "/", file_name), concat(3, tmp_dir, "/", file_name));
}
}
closedir(d);
}
return(0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.