304
技術社區[雲棲]
重寫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使用指南