#include <io.h> //for _finddata_t,_findfirst,_findnext,_findclose
#include <direct.h> //for mkdir
#include <iostream>
#include <string>
using namespace std;
// 查询C:/windows文件夹下.exe文件的个数
void traverse(string fileName = "C:/windows/*.exe", int exeNum = 0)
{
_finddata_t fileInfo;
long handle = _findfirst(fileName.c_str(), &fileInfo);
if (handle == -1L)
{
cerr << "failed to transfer files" << endl;
}
do
{
exeNum ++;
cout << fileInfo.name <<endl;
} while (_findnext(handle, &fileInfo) == 0);
cout << " .exe files' number: " << exeNum << endl;
_findclose(handle);
}
// 将字符串s中的所有的'/'替换成'\'
string replaceChar(string &s){
// 这里值得一提的是字符串里面的'\'得写成"\\"
while(s.find("/") != string::npos){
s.insert(s.find("/"),1,'/');
s.replace(s.find("/"),2,"\\");
}
return s;
}
// 遍历一个路径下所有的文件(包括子文件夹里面的文件)
int fileCnt=0;
void dfsFolder(string srcPath /*= "C:/a"*/, string dstPath /*= "d:/root"*/)
{
_finddata_t FileInfo;
string strfind = srcPath + "/*";
long Handle = _findfirst(strfind.c_str(), &FileInfo);
if (Handle == -1L)
{
cerr << "can not match the folder path" << endl;
return ;
}
do{
//判断是否有子目录
if (FileInfo.attrib == _A_SUBDIR)
{
// 由于系统在进入一个子目录时,匹配到的头两个文件(夹)
// 是"."(当前目录),".."(上一层目录)。需要忽略掉这两种情况
if( (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0))
{
string newPath = srcPath + "/" + FileInfo.name;
dfsFolder(newPath,dstPath);
}
}
else
{
// 文件名字(包括后缀名)
string nameExt(FileInfo.name);
// 文件格式
string ext = nameExt.substr(nameExt.rfind('.'));
// 如果不是影像或者高程数据,则不予处理
if (ext != ".tif" && ext != ".jpg" && ext != ".png" && ext != ".gif")
continue;
// 行
string rowStr = nameExt.substr(0,nameExt.length()-ext.length());
// 列
string columStr = srcPath.substr(srcPath.rfind('/')+1);
// 中间变量,为了求“层数”
string tmp = srcPath;
tmp.erase(srcPath.rfind('/'));
// 层
string layerStr = tmp.substr(tmp.rfind('/')+1);
// 将层、行、列转换成数值
int layer = atoi(layerStr.c_str());
int colum = atoi(columStr.c_str());
int row = atoi(rowStr.c_str());
float degreePerGrid = 180.0f/pow(2.0,layer);
float longitude = colum * degreePerGrid-180;
float latitude = row * degreePerGrid - 90;
//string tileRootPath = tmp.erase(tmp.rfind('/'));
// 创建存储影像和高程数据的文件夹
if(-1 == _access((dstPath+"/image").c_str(),0))
system(("mkdir "+replaceChar(dstPath+"/image")).c_str());
if(-1 == _access((dstPath+"/elevation").c_str(),0))
system(("mkdir "+replaceChar(dstPath+"/elevation")).c_str());
// 行列不足4位(world wind瓦片文件的命名规则),左边用'0'补齐
while(rowStr.size()<4)
rowStr.insert(0,1,'0');
while(columStr.size()<4)
columStr.insert(0,1,'0');
// 处理影像图片
if( ".jpg" == ext )
{
// 创建(层-行)文件夹,World Wind瓦片按照行优先存储
if(-1 == _access((dstPath+"/image/"+layerStr+"/"+rowStr).c_str(),0))
system(("mkdir "+replaceChar(dstPath+"/image/"+layerStr+"/"+rowStr)).c_str());
string newName = rowStr+"_"+columStr;
string source = srcPath+"/"+nameExt;
string destination = dstPath+"/image/"+layerStr+"/"+rowStr+"/"+newName+ext;
// 下面将source和destination里面的'/'替换成'\'
// 因为要用到system调用,而system调用中的'/'的含义是选项设置,⊙﹏⊙b汗
replaceChar(source);
replaceChar(destination);
// 一时找不到好的复制文件的函数,只知道dos命令行的system调用
system(("copy "+source+" "+destination).c_str());
// rename函数可以用来移动文件,但是目标路径必须存在,否则此函数不起作用
//rename(source.c_str(),destination.c_str());
}
else if(".tif" == ext)
{
if(-1 == _access((dstPath+"/elevation/"+layerStr+"/"+rowStr).c_str(),0))
system(("mkdir "+replaceChar(dstPath+"/elevation/"+layerStr+"/"+rowStr)).c_str());
fileCnt++;
string newName = rowStr+"_"+columStr;
string source = srcPath+"/"+nameExt;
string destination = dstPath+"/elevation/"+layerStr+"/"+rowStr+"/"+newName+ext;
replaceChar(source);
replaceChar(destination);
//cout<<source<<endl<<destination<<endl;
system(("copy "+source+" "+destination).c_str());
// rename函数可以用来移动文件,但是目标路径必须存在,否则此函数不起作用
//rename((fullPath+ext).c_str(),"d:/a/a.jpeg"/*newName.c_str()*/);
}
}
}while (_findnext(Handle, &FileInfo) == 0);
_findclose(Handle);
}
void main(int argc, char **argv)
{
//traverse();
// 新建一个文件夹
//system("mkdir c:\\a\\b");
dfsFolder("d:/china","d:/root1");
// 删除一个目录以及里面的子文件夹及其文件
//system("rd /s /q d:\\a");
system("pause");
}