#include<iostream>
#include<conio.h>
#include<fstream>
#include<stdio.h>
#include<string>
#include<stdlib.h>
#include<vector>;
#include <direct.h>
#include <cstring>
using namespace std;
struct file {
string name;
char *data;
long size;
};
struct folder {
string name;
vector<file>files;
int count;
folder *folders[1000];
};
string userfile, s;
char *memoryblock;
ifstream infile;
long long fsize = 0;
ofstream outfile;
folder *myfolders;
folder *head;
folder *prv;
void print(string path)//to print all paths in memory
{
cout << "this files :" << myfolders->files.size() << "\n";
for (int i = 0; i < myfolders->files.size(); i++)
{
string nn = "";
nn = myfolders->files[i].name;
string s = path + "\\" + nn;
cout << s << endl;//print files
}
cout<< "___________________________________________________\n";
for (int i = 0; i < myfolders->count; i++)
{
string s = path + "\\" + myfolders->folders[i]->name;
cout << "folder " << s << ":" <<endl;//print folders
prv = myfolders;
myfolders = myfolders->folders[i];
print(s);
myfolders = prv;
}
}
void print_all_path()
{
cout << "folder mydisk:\n";
print("my disk");
}
void delete_file()
{
while (1)
{
cout << "if you want delete file from here Enter Yes\n";
cout << "else you want put it in folder Enter folder name\n";
getline(cin, s);
if (s == "Yes")
{
int xx = -1;
cout << "Enter file name from memory \n";
getline(cin, s);
for (int i = 0; i < myfolders->files.size(); i++)
{
if (s == myfolders->files[i].name)
xx = i;
}
if (xx != -1)
{
myfolders->files.erase(myfolders->files.begin() + xx);
cout << "Done\n___________________________________________________\n";
}
else {
cout << "Not found \n___________________________________________\n";
}
myfolders = head;
break;
}
else
{
for (int i = 0; i < myfolders->count; i++)
if (s == myfolders->folders[i]->name)
myfolders = myfolders->folders[i];
}
}
}
void delete_folder()
{
while (1)
{
cout << "if you want delete folder from here Enter Yes\n";
cout << "else you want put it in folder Enter folder name\n";
getline(cin, s);
if (s == "Yes")
{
int xx = -1;
cout << "Enter folder name from memory \n";
getline(cin, s);
for (int i = 0; i < myfolders->count; i++)
{
if (s == myfolders->folders[i]->name)
{
for (i; i < myfolders->count - 1; i++)
{
myfolders->folders[i] = myfolders->folders[i + 1];
}
cout << "DONE\n___________________________________________________\n";
myfolders->count--;
break;
}
}
if (xx == -1)
{
cout << "not found \n___________________________________________________\n";
}
myfolders = head;
break;
}
else
{
for (int i = 0; i < myfolders->count; i++)
if (s == myfolders->folders[i]->name)
myfolders = myfolders->folders[i];
}
}
}
void solve(string path)// To access all files and folders
{
for (int i = 0; i < myfolders->files.size(); i++)
{
string nn = myfolders->files[i].name;
string s = path + "\\" + nn;
outfile.open(s, ios::binary | ios::out | ios::app);
outfile.write(myfolders->files[i].data, myfolders->files[i].size);
outfile.close();
cout << "Done\n___________________________________________________\n";
}
for (int i = 0; i < myfolders->count; i++)
{
string s = path + "\\" + myfolders->folders[i]->name;
char *c = const_cast<char*>(s.c_str());
_mkdir(c);
prv = myfolders;
myfolders = myfolders->folders[i];
solve(s);
myfolders = prv;
}
}
void read_file_in_folder()
{
while (1)
{
cout << "if you want put file here Enter Yes\n";
cout << "else you want put it in folder Enter folder name\n";
getline(cin, s);
if (s == "Yes")
{
streampos begin, end;
cout << "Enter path of file\n";
getline(cin, userfile);
cout << "The file name in your memory and type of it like * jpg ,m4a ,txt .....\n";
getline(cin, s);
infile.open(userfile, ios::binary);
if (infile.is_open())
{
begin = infile.tellg();
infile.seekg(0, ios::end);
end = infile.tellg();
fsize = end - begin;
infile.seekg(0, ios::beg);
memoryblock = new char[fsize];
infile.read(memoryblock, fsize);
infile.close();
file f;
f.name = s;
f.size = fsize;
f.data = new char[fsize];
f.data = memoryblock;
int t = 0;
for (int i = 0; i < myfolders->files.size(); i++)
{
if (s == myfolders->files[i].name)
t = 1;
}
if (t == 0) {
myfolders->files.push_back(f);
cout << "Done\n___________________________________________________\n";
myfolders = head;
}
else
{
cout << "This file exists Please change the name\n___________________________________________________\n";
myfolders = head;
}
}
else
{
cout << "Error opening file\n___________________________________________________\n";
}
myfolders = head;
break;
}
else
{
for (int i = 0; i < myfolders->count; i++)//To access all the intended folder
if (s == myfolders->folders[i]->name)
myfolders = myfolders->folders[i];
}
}
}
void write_all_data()
{
cout << "Path for write like D:\\ , E:\\ .... \n";
getline(cin, s);
s = s + "Disk";
char *c = const_cast<char*>(s.c_str());
_mkdir(c);
solve(c);
}
void create_folder() {
string s;
while (1)
{
cout << "if you want put folder here Enter Yes\n";
cout << "else you want put it in folder Enter folder name\n";
getline(cin, s);
if (s == "Yes")
{
int t = 0;
cout << "Enter folder name \n";
getline(cin, s);
for (int i = 0; i < myfolders->count; i++)
{
if (s == myfolders->folders[i]->name)
t = 1;
}
if (t == 0) {
myfolders->folders[myfolders->count] = new folder();
myfolders->folders[myfolders->count]->name = s;
myfolders->folders[myfolders->count]->count = 0;
myfolders->count++;
myfolders = head;
cout << "Done\n___________________________________________________\n";
break;
}
else
{
myfolders = head;
cout << "This folder exists Please change the name\n_____________________________________________\n";
break;
}
}
else
{
for (int i = 0; i < myfolders->count; i++)// To access all the intended folder
{
if (s == myfolders->folders[i]->name)
myfolders = myfolders->folders[i];
}
}
}
}
void write_one_file()
{
string s;
while (1)
{
cout << "if you want write file from here Enter Yes\n";
cout << "else you want write file from folder Enter folder name\n";
getline(cin, s);
if (s == "Yes")
{
cout << "Enter name of file \n";
getline(cin, userfile);
cout << "path of write file like D:\\asd.txt ... \n";
getline(cin, s);
int indx = -1;
for (int i = 0; i < myfolders->files.size(); i++)
{
if (userfile == myfolders->files[i].name)
indx = i;
}
if (indx != -1) {
outfile.open(s, ios::binary | ios::out | ios::app);
outfile.write(myfolders->files[indx].data, myfolders->files[indx].size);
outfile.close();
cout << "Done\n____________________________________________________\n";
}
myfolders = head;
break;
}
else
{
for (int i = 0; i < myfolders->count; i++)//To access all the intended folder
{
if (s == myfolders->folders[i]->name)
myfolders = myfolders->folders[i];
}
}
}
}
void write_one_folder()
{
string path = "";
while (1) {
cout << "if you want write folder from here Enter Yes\n";
cout << "else you want write file from folder Enter folder name\n";
getline(cin, s);
if (s == "Yes")
{
cout << "Enter the path to write it like D:\\ , E:\\ \n";
getline(cin, path);
path += "\\" + myfolders->name;
char *c = const_cast<char*>(path.c_str());
_mkdir(c);
solve(c);
/*for (int i = 0; i < myfolders->files.size(); i++)
{
string nn = myfolders->files[i].name;
path+="\\"+nn;
outfile.open(path, ios::binary | ios::out | ios::app);
outfile.write(myfolders->files[i].data, myfolders->files[i].size);
outfile.close();
cout << "Done\n";
}*/
myfolders = head;
break;
}
else
{
for (int i = 0; i < myfolders->count; i++)//To access all the intended folder
{
if (s == myfolders->folders[i]->name)
myfolders = myfolders->folders[i];
}
}
}
}
int main()
{
cout << " **welcome** \n";
myfolders = new folder();
myfolders->count = 0;
myfolders->name = "disk";
head = myfolders;
int x = 9;
while (1) {
string path, G;
cout << "Enter 1 :to create folder\n";
cout << "Enter 2 :to read \n";
cout << "Enter 3 :to write one file \n";
cout << "Enter 4 :to write one folder\n";
cout << "Enter 5 :to write all data\n";
cout << "Enter 6 :to delete file\n";
cout << "Enter 7 :to delete folder\n";
cout << "Enter 8 :to print all paths\n";
cout << "Enter 9 :to clean console\n";
cout << "Enter 10:to Exit\n";
cout << "___________________________________________________\n";
getline(cin, s);
if (s == "1")
{
create_folder();
}
if (s == "2")
{
read_file_in_folder();
}
if (s == "3")
{
write_one_file();
}
if (s == "4")
{
write_one_folder();
}
if (s == "5")
{
write_all_data();
}
if (s == "6")
{
delete_file();
}
if (s == "7")
{
delete_folder();
}
if (s == "8")
{
print_all_path();
}
if (s == "9")
{
system("CLS");
}
if (s == "10")
{
return 0;
}
}
}