重写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;
}



相关文章
|
11月前
|
存储
图解LeetCode——1233. 删除子文件夹
图解LeetCode——1233. 删除子文件夹
59 0
父组件接收子组件的图片路径写法
父组件接收子组件的图片路径写法
122 0
lodash遍历并继承对象属性,自定义分配
lodash遍历并继承对象属性,自定义分配
99 0
lodash遍历并继承对象属性
lodash遍历并继承对象属性
59 0
lodash反向遍历对象属性
lodash反向遍历对象属性
66 0
lodash反向遍历对象自身的属性
lodash反向遍历对象自身的属性
50 0
lodash移除对象属性路径的属性
lodash移除对象属性路径的属性
98 0
|
PHP 开发者
对象遍历学习路径|学习笔记
快速学习对象遍历学习路径,有时需要把对象里的内容全部遍历出来,这种情况下还需要去控制这种遍历,这时就会用到对象遍历。
48 0
C#编程-18:遍历某一路径下所有文件和文件夹
C#编程-18:遍历某一路径下所有文件和文件夹
445 0
|
API
原来可以这么操作—修改子View绘制顺序
周一的早上,由于项目的开发做的差不多了,正在等待测试结果的我就开始发呆,思考端午节的去处~
280 0
原来可以这么操作—修改子View绘制顺序

热门文章

最新文章