#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    5
    Rep Power
    0

    copy the files in the folder and write a log file


    I am new to C world.

    I need a program which can copy the files in a folder to another folder and then write a log which files were copied successfully.

    Thanks

    Hazrah
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    5
    Rep Power
    0
    This is what I have so far but I still do not know how can I get the list of files from a folder.





    #include <stdio.h>
    #include <io.h>
    #include <strings.h>


    /* How do I get the list of files from a directory. On windows 2000.
    */



    void main(void)
    { // main starts
    //variables
    char sourcedir[100], destdir[100];
    FILE *fp_log, *fp_source, *fp_dest;
    int check;
    char one_byte, destfile[100];


    //get source directory
    printf(“please enter the source directory”);
    scanf(“%s”,&sourcedir);
    printf(“please enter the destination directory”);
    scanf(“%s”,&destdir);
    //open log file
    fp_log=fopen(“logfile.txt”, “w”);
    strcpy(destdir2,destdir);

    check = scan_path(sourcedir);




    int scan_path(char *path)
    {
    char spath[256];
    struct _finddata_t ff;
    long fhandle;

    sprintf(spath,"%s\\*",path);
    fhandle=_findfirst(spath,&ff);
    if(fhandle==-1L)
    {
    printf("Bad path\n");
    return -1;
    }
    else
    {
    /* loop through each etry in the dir */
    while(!_findnext(fhandle,&ff))
    {
    /* if its a sub-dir then recursivly call our self*/
    if(ff.attrib&_A_SUBDIR)
    {
    if(strcmp(ff.name,".."))
    {
    sprintf(spath,"%s\\%s",path,ff.name);
    scan_path(path2);
    }
    }
    else
    {
    /* if its a file then */
    if(ff.attrib^_A_SYSTEM)
    {
    /* in here you will have to check the
    extension */
    printf("Found File %s\n",ff.name);
    //reset the dest dir variable
    strcpy(destdir2,destdir);

    fp_source=fopen(ff.name, "r");
    strcat(destdir2,ff.name);
    fp_dest=fopen(destdir2,"w");
    //what kind of files are these text or binnary??????
    fread(&one_byte, sizeof(char), 1, fp_source);
    while(one_byte !=EOF)
    {
    fwrite(&one_byte, sizeof(char), 1, fp_dest);
    fread(&one_byte, sizeof(char), 1, fp_source);
    }

    }
    }
    }
    }
    return 1;
    }

IMN logo majestic logo threadwatch logo seochat tools logo