在DOS命令行下,我们经常会使用一些DOS命令实现对文件或文件夹的管理:
COPY、DEL、DIR、MD、RD、RENAME、TYPE等命令,虽然我们可以用system("command");简单实现,但要学好一门语言,我个人认为必须通过自己的编程来验证自己的想法,并通过学习笔记的形式记录下来,才能更快进步。
1.COPY命令实现://Ccopy.cpp
#include 《fstream》
#include 《 iostream》
using namespace std;
int main(int argc,char *argv[])
{
if (3!=argc)
{
cout<<"Please Input:[Ccopy inFiler outFiler]"<<endl;
return 0;
}
fstream inFiler(argv[1],ios::in|ios::binary);
if (!inFiler)//如果没有找到文件则输出提示
{
cout<<"Error:No Find you Input inFiler Name! "<<argv[1]<<endl;
inFiler.close();
return 0;
}
fstream outFiler(argv[2],ios::out|ios::binary);
if(!outFiler)
{
cout<<"Error:Not Open You Input outFiler Name! "<<argv[2]<<endl;
outFiler.close();
return 0;
}
char temp;
while(inFiler.get(temp))
{
outFiler<<temp;
}
cout<<"a Filer Copy"<<endl;
inFiler.close();
outFiler.close();
return 0;
}
2.DEL命令实现://Cdel.cpp
#include 《iostream》
#include 《windows.h》
#include 《io.h》
using namespace std;
int main(int argc,char *argv[])
{
if (2!=argc)
{
cout<<"Please Input Del Filer Path and Filer Name:[CDel FilePathAndName]"<<endl;
return 0;
}
char FilePathAndName[200];
strcpy(FilePathAndName,argv[1]);
if (!_access(FilePathAndName,0))//如果文件存在:文件为只读无法删除
{
//去除文件只读属性
SetFileAttributes(FilePathAndName,0);
if(DeleteFile(FilePathAndName))//如果删除成功
{
cout<<"Filer Delete!"<<endl;
}
else//无法删除,文件只读或无权限
{
cout<<"Filer No Delete!"<<endl;
}
}
else//文件不存在
{
cout<<"Error:No Find Filer!"<<endl;
}
return 0;
}
3.DIR命令实现://Cdir.cpp
#include 《iostream》
//#include 《windows.h》也可以包含wnddows.h而不用string.h
//#include 《string.h》
using namespace std;//
int main(int argc,char *argv[])
{
if (argc==2)
{
char CommandName[100];
char cDir[100]="dir ";
strcpy(CommandName,argv[1]);
system(strcat(cDir,CommandName));
//strcpy(c,b).字符中B复制到C
//strcat(c,b).把B连接到C
}
else
{
system("dir");
}
return 0;
}
4.MD命令实现://Cmd.cpp
#include 《direct.h》
#include 《iostream》
using namespace std;
int main(int argc,char *argv[])
{
if (2!=argc)
{
cout<<"Please Input :[Cmd PathName]"<<endl;
return 0;
}
char PathName[100];
strcpy(PathName,argv[1]);
mkdir(PathName);
return 0;
}
5.RD命令实现://Crd.cpp
#include《 iostream》
//#include 《windows.h》可以包含wnddows.h而不用string.h
//#include 《string.h》
using namespace std;//
int main(int argc,char *argv[])
{
if (argc==2)
{
char PathName[100];
char cRd[100]="rd ";
strcpy(PathName,argv[1]);
system(strcat(cRd,PathName));
//strcpy(c,b).字符中B复制到C
//strcat(c,b).把B连接到C
}
else
{
cout<<"Error:No Input Path Name!"<<endl;
}
return 0;
}
6.RENAME命令实现://Crename.cpp
#include 《iostream》
#include 《windows.h》
#include 《io.h》
using namespace std;
int main(int argc,char *argv[])
{
if (3!=argc)
{
cout<<"Please Input :[Credname OldName NewName]"<<endl;
return 0;
}
char OldName[100];
char NewName[100];
strcpy(OldName,argv[1]);
strcpy(NewName,argv[2]);
if (!_access(OldName,0))//如果文件存在
{
if(!rename(OldName,NewName))//重命名成功
{
cout<<"Credname OK!"<<endl;
}
else
{
cout<<"Crendame NO!"<<endl;//不能重命名原因:1已有此文件。2正在使用没有关闭。3你没有权限。
}
}
else//文件不存在
{
cout<<"No Find Filer!"<<endl;
}
return 0;
}
7.TYPE命令实现://Ctype.cpp
#include 《fstream》
#include 《iostream》
using namespace std;
int main(int argc,char *argv[])
{
if (2!=argc)
{
cout<<"Please Input:[CType FileName]"<<endl;
return 0;
}
fstream FileName(argv[1],ios::in|ios::binary);
if (!FileName)//如果没有找到文件则输出提示
{
cout<<"Error:No Find you Input File Name! "<<argv[1]<<endl;
FileName.close();
return 0;
}
char temp;
while(FileName.get(temp))
{
cout<<temp;
}
cout<<endl;
FileName.close();
return 0;
}