[C] 纯文本查看 复制代码
#include <stdio.h>
#include <windows.h>
//创建文件夹所需头文件
#include <io.h>
#include <direct.h>
#define YEAR 2
//存放时间,用作文件夹名
TCHAR foldername[YEAR*12][200] = {0};
int t;
void FileTimeCompare(SYSTEMTIME FileTime){
char buf[200] = {0};
int i, p;
memset(buf, 0, 200);
//printf("t=%d\n", t);
wsprintf(buf, "%d-%d", FileTime.wYear, FileTime.wMonth);
if(t==0 && strcmp(foldername[t], buf) != 0){
strcpy(foldername[t], buf);
t++;
}
//信息是否重复的开关,如果是则为1
p=0;
for(i=0; i<t; i++ ){
if(strcmp(foldername[i], buf) == 0){
p=1;
}
}
if(p==0){
strcpy(foldername[t], buf);
t++;
}
}
void Renfile(char *filename, SYSTEMTIME FileTime){
char buf[MAX_PATH];
char newpath[MAX_PATH];
int i;
memset(buf, 0, MAX_PATH);
memset(newpath, 0, MAX_PATH);
wsprintf(buf, "%d-%d", FileTime.wYear, FileTime.wMonth);
for(i=0; i<t; i++){
if(strcmp(foldername[i], buf) == 0){
wsprintf(newpath, ".\\%s\\%s", buf, filename);
if(rename(filename, newpath) == 0)
printf("%s\t\t已移动!\n", filename);
}
}
}
void FFind(char *path, char *myname);
void TFind(char *path, char *myname);
void main(int argc, char *argv[]){
char path[MAX_PATH] = {0};
//创建文件名缓冲区
char filename[MAX_PATH] = {0};
char tmp;
int i;
//获取当前路径
getcwd(path, MAX_PATH);
printf("path=%s\n", path);
strcat(path, "\\*.*");
t=0;
//第一遍,遍历所有文件,将其时间进行收集对比
FFind(path, argv[0]);
printf("发现%d个时间段的文件,请问是否创建相应文件夹并移入?(Y/anyKey):",t);
scanf("%c", &tmp);
fflush(stdin);
if(tmp != 'y' && tmp != 'Y'){
printf("你输入的字符为:%c,已取消操作\n", tmp);
system("pause");
exit(0);
}
//创建文件夹
for(i=0; i<t; i++){
if(_access(foldername[i], 0) == -1){
_mkdir(foldername[i]);
printf("%s\t\t文件夹已创建\n", foldername[i]);
}
}
//第二遍,遍历并移动所有文件
TFind(path, argv[0]);
printf("\n\n已移动所有文件!\n");
system("pause");
//printf("当前路径为:%s\n", GetPath());
//printf("发现创建时间为%s的文件,创建文件夹。\n",GetCreatTime());
}
void FFind(char *path, char* myname){
//创建查找结构体
WIN32_FIND_DATA FindFile;
//创建查找文件句柄
HANDLE hFind;
char filename[MAX_PATH] = {0};
SYSTEMTIME spec_time;
hFind = FindFirstFile(path, &FindFile);
if(INVALID_HANDLE_VALUE == hFind){
printf("创建文件查找结构体出错!\n");
exit(0);
}
do{
if(FindFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
if(0 != strcmp(FindFile.cFileName, ".") && 0 != strcmp(FindFile.cFileName, "..")){
//发现目录,暂不处理
}
}else{
//找到文件,查看其最后一次修改日期
//排除自身
getcwd(filename, MAX_PATH);
strcat(filename, "\\");
strcat(filename, FindFile.cFileName);
if(strcmp(filename, myname) != 0){
FileTimeToSystemTime(&FindFile.ftLastWriteTime, &spec_time);
FileTimeCompare(spec_time);
}
}
}while(FindNextFile(hFind, &FindFile));
}
void TFind(char *path, char *myname){
//创建查找结构体
WIN32_FIND_DATA RenFile;
//创建查找文件句柄
HANDLE tFind;
char filename[MAX_PATH] = {0};
SYSTEMTIME spec_time;
tFind = FindFirstFile(path, &RenFile);
if(INVALID_HANDLE_VALUE == tFind){
printf("创建tFind文件查找结构体出错!\n");
exit(0);
}
//第二遍遍历文件,移动文件
do{
if(RenFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
if(0 != strcmp(RenFile.cFileName, ".") && 0 != strcmp(RenFile.cFileName, "..")){
//发现目录,暂不处理
}
}else{
//找到文件并移动至已创建目录
//排除自身
//strcpy(filename, getcwd(path, MAX_PATH));
getcwd(filename, MAX_PATH);
strcat(filename, "\\");
strcat(filename, RenFile.cFileName);
if(strcmp(filename, myname) != 0){
FileTimeToSystemTime(&RenFile.ftLastWriteTime, &spec_time);
Renfile(RenFile.cFileName, spec_time);
}
}
}while(FindNextFile(tFind, &RenFile));
}