Windows下C++文件管理器

实现功能:

1、实现文件夹创建、进入。
2、实现当前文件夹下的内容罗列。
3、实现文件拷贝和文件夹拷贝(文件夹拷贝指深度拷贝,包括所有子目录和文件)。
4、实现文件删除和文件夹删除(文件夹删除指深度删除,包括所有子目录和文件)。
四个文件:MainFileManager .cpp、FileManager.h、FileManager.cpp、dirent.h(从Git-Hub上下载

MainFileManager .cpp

#include "FileManager.h"
int main() {
	FileManager fileManager;
	fileManager.start();
	system("pause");
	return 0;
}

FileManager.h

#include "dirent.h"

class FileManager
{
private:
	void welcome();
	void getHelp();
	char* getPath();
	bool changeDir(char*);
	bool createDir(char*);
	DIR* openDir(char*);
	char* addFileNames(char*, char*);
	void input();
	void outPutDirent();
	void removeIt(char*);
	void closeDir(DIR*);
	void copyIt(char*, char*);
	bool copyDir(char*, char*);
	bool ifDirExist(char*);
	bool ifFileExist(char*);
	void enter(char*);
	bool copyFile(char*, char*);
	bool removeDir(char*);

public:
	FileManager();
	~FileManager();
	void start();
};

FileManager.cpp

#include "FileManager.h"
#include <iostream>
#include <string>
#include <direct.h>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

FileManager::FileManager() {
}

FileManager::~FileManager() {
}

void FileManager::start() {
	welcome();
	getHelp();
	input();
}

bool FileManager::createDir(char* fileName) {
	if (fileName != NULL && !_mkdir(fileName))//&&短路
		return true;
	return false;
}

bool FileManager::changeDir(char* newFilePath) {
	if ((newFilePath != NULL) && (_chdir(newFilePath) == 0))
		return true;
	return false;
}

void FileManager::enter(char* fileName) {
	if (ifDirExist(fileName) == false) {
		cout << fileName << "文件夹不存在" << endl;
		return;
	}
	char* filePath_new = NULL;
	if ((filePath_new = addFileNames(getPath(), fileName)) == NULL) {
		cout << "获取目标文件夹路径失败" << endl;
		return;
	}
	else if (changeDir(filePath_new)) {
		cout << "进入文件夹成功" << endl;
		delete[] filePath_new;
		return;
	}
	else
		cout << "进入文件夹失败" << endl;
	delete[] filePath_new;
}

DIR* FileManager::openDir(char* fileName) {
	DIR* dirPtr = NULL;
	if (fileName != NULL)
		dirPtr = opendir(fileName);
	return dirPtr;
}

void FileManager::closeDir(DIR * dirPtr) {
	if (dirPtr != NULL)
		closedir(dirPtr);
}

char* FileManager::addFileNames(char* filePath, char* fileName) {
	if (filePath == NULL || fileName == NULL)
		return NULL;
	char* absoluteNames_new = new char[strlen(filePath) + strlen(fileName) + 2];
	strcpy_s(absoluteNames_new, strlen(filePath) + 1, filePath);
	strcat_s(absoluteNames_new, strlen(filePath) + 2, "\\");
	strcat_s(absoluteNames_new, strlen(filePath) + strlen(fileName) + 2, fileName);
	return absoluteNames_new;
}

void FileManager::removeIt(char* fileName) {
	if (fileName == NULL)
		return;
	if (remove(fileName) == 0) {
		cout << "删除" << fileName << "成功" << endl;
		return;
	}
	if (ifDirExist(fileName) == false)
		cout << "文件名不存在,检查输入" << endl;
	else if (removeDir(fileName))
		cout << "删除" << fileName << "成功" << endl;
	else
		cout << "删除" << fileName << "失败" << endl;
}

bool FileManager::ifFileExist(char* fileName) {
	fstream _file;
	_file.open(fileName, ios::in);
	if (!_file)
		return false;
	return true;
}
bool FileManager::ifDirExist(char* fileName) {
	char* filePath = getPath();
	DIR* dirPtr = openDir(filePath);
	dirent* direntPtr = NULL;
	readdir(dirPtr);
	readdir(dirPtr);
	while (direntPtr = readdir(dirPtr))
		if (!strcmp(direntPtr->d_name, fileName)) {
			closeDir(dirPtr);
			return true;
		}
	closeDir(dirPtr);
	return false;
}

bool FileManager::removeDir(char* fileName) {
	if (_rmdir(fileName) == 0) {
		return true;
	}
	DIR* dirPtr;
	char* filePath;
	char* filePath_new;
	if ((dirPtr = openDir(fileName)) == NULL)
		return false;
	if ((filePath = getPath()) == NULL)
		return false;
	if ((filePath_new = addFileNames(getPath(), fileName)) == NULL)
		return false;
	else if (changeDir(filePath_new) == false) {
		delete[] filePath_new;
		return false;
	}
	dirent* direntPtr = NULL;
	readdir(dirPtr);
	readdir(dirPtr);
	while (direntPtr = readdir(dirPtr)) {
		if (remove(direntPtr->d_name) == 0) {
			continue;
		}
		if (removeDir(direntPtr->d_name) == false) {
			delete[] filePath_new;
			return false;
		}
		if (changeDir(filePath_new) == false) {
			delete[] filePath_new;
			return false;
		}
	}
	delete[] filePath_new;
	if (changeDir(filePath) == false)
		return false;
	if (_rmdir(fileName) != 0)
		return false;
	return true;
}

void FileManager::outPutDirent() {
	char* filePath = NULL;
	DIR* dirPtr = NULL;
	dirent* direntPtr = NULL;
	if ((filePath = getPath()) == NULL) {
		cout << "获取路径失败,打印失败" << endl;
		return;
	}
	if ((dirPtr = openDir(filePath)) == NULL) {
		cout << "打开当前文件夹失败,打印失败" << endl;
		return;
	}
	cout << "+" << setfill('-') << setw(9) << "+"
		<< setw(9) << "+"
		<< setw(9) << "+" << endl;
	while (direntPtr = readdir(dirPtr)) {
		cout << "|" << setfill(' ') << setw(8) << direntPtr->d_name
			<< "|" << setw(8) << direntPtr->d_ino
			<< "|" << setw(8) << direntPtr->d_reclen << "|" << endl;
	}
	cout << "+" << setfill('-') << setw(9) << "+"
		<< setw(9) << "+"
		<< setw(9) << "+" << endl;
	closeDir(dirPtr);
}

void FileManager::copyIt(char* filePath_target, char* fileName) {
	if (!ifDirExist(fileName) && !ifFileExist(fileName)) {
		cout << "源文件不存在" << endl;
		return;
	}
	if (openDir(filePath_target) == NULL) {
		cout << "目标路径不存在" << endl;
		return;
	}
	char* filePath_source = NULL;
	if ((filePath_source = getPath()) == NULL) {
		cout << "获取路径失败,拷贝失败" << endl;
		return;
	}
	int strlen_filePath_source = (int)strlen(filePath_source);
	int strlen_filePath_target = (int)strlen(filePath_target);
	int shorter = (strlen_filePath_source < strlen_filePath_target) ? strlen_filePath_source : strlen_filePath_target;
	if (strncmp(filePath_target, filePath_source, shorter) == 0) {
		if (strlen_filePath_source < strlen_filePath_target) {
			cout << "目标文件夹是源文件夹的子文件夹,不能拷贝" << endl;
			return;
		}
		else if (strlen_filePath_source = strlen_filePath_target) {
			cout << "目标文件路径与源文件路径相同,不能拷贝" << endl;
			return;
		}
	}
	if (copyFile(filePath_target, fileName))
		cout << "拷贝" << fileName << "成功" << endl;
	else if (copyDir(filePath_target, fileName)) {
		changeDir(filePath_source);
		cout << "拷贝" << fileName << "成功" << endl;
	}
	else
		cout << "拷贝" << fileName << "失败" << endl;
}

bool FileManager::copyFile(char* filePath_target, char* fileName) {
	char* filePath_target_new = NULL;
	if (((filePath_target_new = addFileNames(filePath_target, fileName))) == NULL)
		return false;
	ifstream file_source;
	ofstream file_target;
	file_source.open(fileName, ios::in);
	if (!file_source) {
		delete[] filePath_target_new;
		return false;
	}
	file_target.open(filePath_target_new, ios::binary);
	if (!file_target) {
		delete[] filePath_target_new;
		return false;
	}
	file_target << file_source.rdbuf();
	delete[] filePath_target_new;
	file_source.close();
	file_target.close();
	return true;
}

bool FileManager::copyDir(char* filePath_target, char* fileName) {
	if (filePath_target == NULL)
		return false;
	if (fileName == NULL)
		return false;
	char* filePath_target_new = NULL;
	if ((filePath_target_new = addFileNames(filePath_target, fileName)) == NULL)
		return false;
	if (_mkdir(filePath_target_new) != 0) {
		delete[] filePath_target_new;
		return false;
	}
	DIR* dirPtr = NULL;
	if ((dirPtr = openDir(fileName)) == NULL) {
		delete[] filePath_target_new;
		return false;
	}
	char* filePath_source = NULL;
	if ((filePath_source = getPath()) == NULL) {
		delete[] filePath_target_new;
		return false;
	}
	char* filePath_source_new = NULL;
	if ((filePath_source_new = addFileNames(filePath_source, fileName)) == NULL)
		return false;
	if (changeDir(filePath_source_new) == false) {
		delete[] filePath_source_new;
		return false;
	}
	dirent* direntPtr = NULL;
	readdir(dirPtr);
	readdir(dirPtr);
	while (direntPtr = readdir(dirPtr)) {
		if (direntPtr->d_type != DT_DIR) {
			if (copyFile(filePath_target_new, direntPtr->d_name) == false)
				return false;
			continue;
		}
		if (copyDir(filePath_target_new, direntPtr->d_name) == false) {
			delete[] filePath_source_new;
			return false;
		}
		if (changeDir(filePath_source_new) == false) {
			delete[] filePath_source_new;
			return false;
		}
	}
	delete[] filePath_source_new;
	delete[] filePath_target_new;
	return true;
}

void FileManager::input() {
	string order;
	cout << "请输入命令:" << endl;
	while (getline(cin, order)) {
		if (_stricmp(order.c_str(), "createDir") ==0 ) {
			string fileName;
			cout << "请输入需要创建的文件名:" << endl;
			getline(cin , fileName);
			if (createDir((char*)fileName.c_str()))
				cout << "创建文件夹" << fileName << "成功" << endl;
			else
				cout << "创建文件夹" << fileName << "失败" << endl;
		}
		else if (_stricmp(order.c_str(), "removeIt") == 0) {
			string fileName;
			cout << "请输入需要删除的文件名:" << endl;
			getline(cin, fileName);
			removeIt((char*)fileName.c_str());
		}
		else if (_stricmp(order.c_str(), "copyIt") == 0) {
			string fileName;
			string filePath_target;
			cout << "请输入需要拷贝的文件名:" << endl;
			getline(cin, fileName);
			cout << "请输入目标路径:" << endl;
			getline(cin,filePath_target);
			copyIt((char*)filePath_target.c_str(), (char*)fileName.c_str());
		}
		else if (_stricmp(order.c_str(), "changeDir") == 0) {
			string filePath_target;
			cout << "请输入需要改变的路径名:" << endl;
			getline(cin , filePath_target);
			if (changeDir((char*)filePath_target.c_str()))
				cout << "改变路径成功!" << endl;
			else
				cout << "改变路径失败!" << endl;
			cout << "当前工作路径为:  " << getPath() << endl;
		}
		else if (_stricmp(order.c_str(), "showDirent") == 0)
			outPutDirent();
		else if (_stricmp(order.c_str(), "enterDir") == 0) {
			string fileName;
			cout << "请输入需要进入的文件名:" << endl;
			getline(cin, fileName);
			enter((char*)fileName.c_str());
		}
		else if (_stricmp(order.c_str(), "exit") == 0) {
			cout << "欢迎下次使用!" << endl;
			break;
		}
		else
			cout << "检查命令输入!" << endl;
		cout << endl << "请输入命令:" << endl;
	}
}

void FileManager::getHelp() {
	cout << "使用说明:" << endl;
	cout << "+" << setfill('-') << setw(12) << "+" << setw(41) << "+" << endl;
	cout << "|" << setfill(' ') << "命令" << setw(8)
		<< "|" << "含义" << setw(37) << "|" << endl;
	cout << "+" << setfill('-') << setw(12) << "+" << setw(41) << "+" << endl;
	cout << "|" << setfill(' ') << "showDirent" << setw(2)
		<< "|" << "当前工作目录下文件内容罗列" << setw(15) << "|" << endl;
	cout << "|" << "enterDir" << setw(4)
		<< "|" << "进入当前工作目录下的一个文件夹" << setw(11) << "|" << endl;
	cout << "|" << "changeDir" << setw(3)
		<< "|" << "改变工作目录" << setw(29) << "|" << endl;
	cout << "|" << "createDir" << setw(3)
		<< "|" << "在当前工作目录下创建文件夹" << setw(15) << "|" << endl;
	cout << "|" << "removeIt" << setw(4)
		<< "|" << "删除当前工作目录下的一个文件" << setw(13) << "|" << endl;
	cout << "|" << "copyIt" << setw(6)
		<< "|" << "复制当前工作目录下的一个文件到指定目录" << setw(3) << "|" << endl;
	cout << "|" << "exit" << setw(8)
		<< "|" << "退出文件管理器" << setw(27) << "|" << endl;
	cout << "+" << setfill('-') << setw(12) << "+" << setw(41) << "+" << endl;
	cout << "输入路径时请以单斜杠分隔,如:	D:\\z\\a" << endl << endl;
}

void FileManager::welcome() {
	for (int i = 0; i < 56; i++)
		cout << "-";
	cout << "欢迎使用";
	for (int i = 0; i < 56; i++)
		cout << "-";
	cout << endl;
}

char* FileManager::getPath() {
	char* filePath = NULL;
	filePath = _getcwd(NULL, 0);
	return filePath;
}

猜你喜欢

转载自blog.csdn.net/qq_41017648/article/details/89787164