文件複製

#include
#include<iostream.h>
#include<io.h>
#include<string>
#include<cstring>
#include<direct.h>




/*
* 路徑轉換,將單斜槓轉換成雙斜槓
*/
void getDouble(char * str, int len, char * temp) //
{
char * start = NULL;
char * t = NULL;
start = str;
t = temp;
for(int i = 1; i <= len; i++, ++start) //循環len次,來處理'\'
{
if(* start == '\\') //當爲'\'時,在後面再加入一個'\'
{
* t = * start;
++t;
* t = '\\';
++t;
}else{ //不爲'\'時,原樣複製到新空間
* t = * start;
++t;
}
}
* t = '\0';
//cout<<temp<<endl; //輸出路徑
}


/*
* 當有通配符*時,得到父路徑
*/
void getParentPath(char * str, int len, char * temp) //得到父路徑
{
char * start = NULL;
char * end = NULL;
char * t = NULL;
start = str;
end = str + (len - 1); //指向最後一個位置
t = temp;
while( * end != '\\'){  //將end指向'\'
end = end - 1;
}
++end; //將指針放到'\'後面


for(; start != end; start++) //將父路徑寫到temp中
{
* t = * start;
++t;
}
* t = '\0'; //加'\0'結束
//cout<<"Parent Path:"<<temp<<endl;
}
/*
* 得到目的路徑
*/
void getDesPath(char * des, char * desPath) //得到目的路徑
{
strcpy(desPath, (const char *)des);
strcat(desPath, "\\\\");
//cout<<"Des Path:"<<desPath<<endl;
}


void getCommend(char * p, char * src, char * des)
{
strcpy(p, "xcopy ");
strcat(p, (const char *)src);
strcat(p, " ");
strcat(p, (const char *)des);
strcat(p, " /s/e");


//cout<<"命令:"<<p<<endl;

}


void fileCopy(char * src, char * des){
long lf; //定義打開文件的句柄
_finddata_t file; //結構體,存儲文件的信息
char currentPath[100];
char transSrcPath[100];
char transDesPath[100];
char desPath[100];
unsigned char buf[100];



if((lf = _findfirst((const char *)src, &file)) != -1L) //對c盤a文件夾進行復制
{
//cout<<"文件列表:"<<endl;
do  //如果找到下個文件名字成功的話
{
/*
cout<<file.name<<endl;
if(file.attrib == _A_NORMAL)
cout<<"普通文件"<<endl;
else if(file.attrib == _A_RDONLY)
cout<<"只讀文件"<<endl;
else if(file.attrib == _A_HIDDEN)
cout<<"隱藏文件"<<endl;
else if(file.attrib == _A_SYSTEM)
cout<<"系統文件"<<endl;
else if(file.attrib == _A_SUBDIR)
cout<<"子目錄"<<endl;
else cout<<"存檔文件"<<endl;
*/
getDouble(src, strlen((const char *)src), transSrcPath); //將轉換的源路徑存入transPath
getParentPath(transSrcPath, strlen(transSrcPath), currentPath); //得到父路徑 c:\\a\\

getDouble(des, strlen((const char *)des), transDesPath); //將轉換的目的路徑存入transDesPath
getDesPath(transDesPath, desPath); //得到目的路徑 c:\\b\\



if(file.attrib == _A_SUBDIR){ //如果爲子目錄
/*
* 當爲子目錄的時候,利用系統的命令行參數
* 實現子目錄以及子目錄內文件的拷貝
*/


char dirPath[100];
char cmd[100];
getParentPath(src, strlen((const char *)src), dirPath);
strcat(dirPath, file.name); //構建目錄的源路徑 
//cout<<"目錄路徑:"<<dirPath<<endl; // c:\a\bbbabc
getCommend(cmd, dirPath, des);
system((const char *)cmd); //調用系統的命令行參數實現文件夾的拷貝


}else{ //如果不是目錄
/*
* 當文件不是目錄時,利用fstream文件輸入輸出流來對每個文件
* 進行讀/寫操作,從而,達到複製的效果
*/


ifstream fin((const char *)strcat(currentPath, file.name), ios::nocreate|ios::binary); //創建輸入文件流
ofstream fout((const char *)strcat(desPath, file.name), ios::binary); //創建輸入流

if(!fin){
cout<<"源文件路徑沒有找到!"<<endl;
return;
}
if(!fout){
cout<<"目的路徑錯誤!"<<endl;
return;
}
while(!fin.eof()){ //實現文件的複製
fin.read(buf, sizeof(buf));
fout.write(buf, fin.gcount());
}
fin.close(); //關閉流
fout.close();
}

}while(_findnext(lf, &file) == 0);
cout<<"複製已完成!"<<endl;
_findclose(lf);
}else{
cout<<"源文件路徑沒有找到!"<<endl;
}
}


int main()
{
char src[100], des[100]; 
cout<<"請輸入路徑和源文件名稱:"<<endl;
cin>>src; // c:\\a\\*abc.txt
cout<<"請輸入目的路徑:"<<endl;
cin>>des;
fileCopy(src, des); //調用文件拷貝函數


return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章