⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bak.cpp

📁 Boost 库写的备份软件
💻 CPP
字号:
#include <iostream>
#include <fstream>
#include <ctime>
//#include <clocale>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
using namespace std;
void	bakfile(path &pt){
	char	fname[BUFSIZ];
	time_t	now;
	struct tm * tinfo;
	//setlocale(LC_ALL,"chs");
	time(&now);
	tinfo	=	localtime(&now);
	sprintf(fname,"%s.%d%02d%02d",pt.string().c_str(),tinfo->tm_year+1900,tinfo->tm_mon+1,tinfo->tm_mday);
	try{
		copy_file(pt,path(fname));
	}catch(basic_filesystem_error<path> e){
		cerr<<e.what()<<endl;
	}
}
void	copydir(path	&src,path &det,string &data){
	directory_iterator	enditr;
	for(directory_iterator	itr(src);itr!=enditr;++itr){
		if(is_directory(itr->status())){
			if(path(itr->string())	==	det){
				continue;
			}
			path	_tmp(det.string()+"/"+itr->filename()+data);
			try{
				if(exists(_tmp)){
					copydir(path(itr->string()),_tmp,data);
				}
				if(create_directory(_tmp)){
					copydir(path(itr->string()),_tmp,data);
				}
			}catch(basic_filesystem_error<path>	e){
				cerr<<e.what()<<endl;
			}
		}
		else {
			try{
				copy_file(*itr,det.string()+"/"+itr->filename()+data);
			}catch(basic_filesystem_error<path> e){
				cerr<<e.what()<<endl;
			}
		}
	}
}
bool	bakdir(path &pt){
	char	dirname[BUFSIZ]={0};
	char	dir[BUFSIZ]={0};
	time_t	now;
	struct tm * tinfo;
	time(&now);
	tinfo	=	localtime(&now);
	sprintf(dir,".%d%02d%02d",tinfo->tm_year+1900,tinfo->tm_mon+1,tinfo->tm_mday);
	sprintf(dirname,"%s%s",pt.string() .c_str(),dir);
	path	det(dirname);
	try{
		if(exists(det)){
			copydir(pt,det,string(dir));
		}
		if(create_directory(det)){
			copydir(pt,det,string(dir));
		}
		else {
			return	false;
		}
	}catch(basic_filesystem_error<path>	e){
		cerr<<e.what()<<endl;
	}
	return	false;


}
int	main(int	argc,char **	argv){
	while(--argc){
		try{
			path	fe(*(++argv));
			if(!exists( fe )){
				return	0;
			}
			else {
				if(is_directory(fe)){
					bakdir(fe);
				}
				else {
					bakfile(fe);
				}
			}
		}catch(basic_filesystem_error<path> e){
			cerr<<e.what()<<endl;
		}
	}
	return	0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -