C语言文件提取神器for Mac

来源:互联网 发布:心动网络官网電話 编辑:程序博客网 时间:2024/05/08 18:12
  • 程序功能
    • 将一个文件内所有子文件夹中的指定类型文件提取并复制到同目录下targetFolder文件夹下。
  • 操作方法
    • 用X-Code编译完成将可执行文件拖到桌面上双击打开后根据提示操作。
  • 代码如下
#include<stdio.h>#include<stdlib.h>#include<string.h>//提取文件全路径下最后的文件名字符串void fun(char a[]){    int i,j=0;    i=strlen(a);    while(a[i]!='/')    {        i--;    }    for(i++;a[i];i++,j++)    {        a[j]=a[i];    }    a[j]=0;}int main(){    FILE *fp;    char ch,singleFile[5000],suffix[20],command[5000],mkdir[5000];    char folderPath[5000],tempFile[5000],targetFolder[5000];    int fileNum,i;    while(1)    {        printf("请拖入文件夹路径:");        scanf("%s",folderPath);        if(strcmp(folderPath,"exit")==0) exit(0);        printf("请输入提取文件后缀(如.c,.txt):");        scanf("%s",suffix);        //作用Unix下mkdir命令创建同目录下targetFolder文件夹        strcpy(targetFolder,folderPath);        strcat(targetFolder,"/targetFolder/");        strcpy(mkdir,"mkdir ");        strcat(mkdir,targetFolder);        system(mkdir);        strcpy(command,"find ");        strcat(command,folderPath);        strcat(command," -name *");        strcat(command,suffix);        strcat(command,">>");        strcpy(tempFile,folderPath);        strcat(tempFile,"/temp.txt");//temp.txt路径        strcat(command,tempFile);        system(command);//执行find命令        fp=fopen(tempFile,"r");        fileNum=0;        while(1)        {            i=-1;            singleFile[++i]='\"';            while((singleFile[++i]=fgetc(fp))!=10&&singleFile[i]!=EOF);            if(singleFile[i]==EOF)            {                break;            }            singleFile[i++]='\"';            singleFile[i]=0;            //执行复制命令将单个文件复制到目标文件夹            strcpy(command,"cp ");            strcat(command,singleFile);            strcat(command," \"");            strcat(command,targetFolder);            fun(singleFile);//提取文件全路径下最后的文件名字符串            strcat(command,singleFile);            system(command);            printf("%d:%s\n",++fileNum,command);        }        fclose(fp);        printf("\n共%d个%s文件已拷贝到同目录targetFolder文件夹中!\n\n",fileNum,suffix);        //删除临时文件        strcpy(singleFile,"rm ");        strcat(singleFile,tempFile);        system(singleFile);    }    return 0;}
  • 结果如下

这里写图片描述

这里写图片描述

0 0
原创粉丝点击