重写findobj【可以遍历子文件夹】
这一次几乎是完全推翻之前写的代码,重新写了一个函数,完成遍历子文件夹,并挑选出有用文件,复制出去的功能。
我觉得这种代码就是要仔细,不然频繁的字符串操作很容易出错。。。其次是这种功能性很强的代码写起来很有趣,要多积累这些代码程序,以后用起来方便,编程能力也会在不知不觉中提高。
我记得曾经google的老大拉里佩奇说过google员工做事情都很有意思,比如做项目的时候投影仪感觉不太好使了,他们就会把项目放下来去鼓捣投影仪,反而可能发明一种新的投影方法,我觉得这些小程序就和他们的做法不谋而合了。。。
这个程序可以找到同名文件【包括后缀也相同】
另外,这次由于中文乱码的问题,我把注释全部写成了英文。。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<io.h>
#include <windows.h>
char tFPath[60];//target folder
char dFPath[20];//destination folder
char path[60];//now path
char fatherPath[60];//father path
void fileFinder(char * lpPath,char *fileName)
{
strcpy(path,lpPath);
char szFind[MAX_PATH],szFile[MAX_PATH];
WIN32_FIND_DATA FindFileData;
strcpy(szFind,lpPath);
strcat(szFind,"\\*.*");
HANDLE hFind=::FindFirstFile(szFind,&FindFileData);
if(INVALID_HANDLE_VALUE == hFind)
return;
while(TRUE)
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//go deeper folders
if(FindFileData.cFileName[0]!='.')
{
strcpy(szFile,lpPath);
strcat(szFile,"\\");
strcat(szFile,FindFileData.cFileName);
//szFile is the path of now
strcpy(fatherPath,path);
strcpy(path,szFile);
//strcat(path,"\\");
fileFinder(szFile,fileName);
}
}
else
{
//find the specific file
if (strcmp(fileName,FindFileData.cFileName)==0)
{
printf("find this file: %s\n",FindFileData.cFileName);
//copy to destination folder
//make sure szFile has the right path
strcat(path,"\\");
printf("path == %s\n",path);
strcat(path,fileName);
char SysOrder[100]="copy ";
strcat(SysOrder,path);
strcat(SysOrder," ");
strcat(SysOrder,dFPath);
//printf("%s\n\n",SysOrder);
system(SysOrder);
}
}
if(!FindNextFile(hFind,&FindFileData))
{
//return to father folder
strcpy(path,fatherPath);
break;
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
FILE *fp=NULL;
char fileName[30];
char a;
char txtSrc[30];
printf("Please enter the TXT Source file path:\n");
scanf("%s",txtSrc);
fp=fopen(txtSrc,"r");
printf("Please enter the target folder path:\n");
scanf("%s",tFPath);
printf("Please enter the destination folder path:\n");
scanf("%s",dFPath);
int i=0;
do
{
a=fgetc(fp);
if (a!='\n')
fileName[i++]=a;
else
{
fileName[i]='\0';
fileFinder(tFPath,fileName);
i=0;
}
}while(a!=EOF);
fclose(fp);
system("pause");
return 0;
}
最后更新:2017-04-03 05:39:11
上一篇:
Coursera Scala 5-5 List:Reduction of Lists
下一篇:
maven配置详解
相机的照片不小心误删除了怎么办?如何恢复呢?
Ajax请求过程中显示“进度”的简单实现
Database Editor SQLiteExpertSetup.exe
raid功能中spanning和striping模式有什么区别?
NTP组件DDoS攻击漏洞通知
android读取sd卡图片并进行缩放操作
bmon:Linux 下一个强大的网络带宽监视和调试工具
Spring????????????DBCP????????????????????????ms access??????????????????-??????-????????????-?????????
MariaDB完成C轮2700万美元投资 阿里巴巴领投
PostgreSQL压测工具之pgbench-tools使用指南