0

I have this code that moves my entire structure of folder to another folder, but I only want to copy this structure to another folder, so my first folder still remains with all files and subdirectories.

I also have to use flags '-a' - to copy hidden files, '-u' - to delete files from source directory (first folder from which I copy), '-o' - existing files in destination to not overwrite and '-dx' - to copy files up to level 'x' nesting, I don't really know how to integrate this.

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <iostream>
#include <dir.h>
#include <string.h>
#include <limits>
using namespace std;

char dest[PATH_MAX];
char old[PATH_MAX];
void recursiveTraverse(char* path);

int main()
{
    strcpy(dest, "E:\\moved");
    chdir("E:\\example");
    recursiveTraverse("E:\\example");
    return 1;
}

void recursiveTraverse(char* path)
{
    char searchPath[PATH_MAX];
    char newPath[PATH_MAX];
    char moved[PATH_MAX];
    int value;
    struct _finddata_t fileinfo;
    strcpy(searchPath, path);
    strcat(searchPath, "\\*.*");
    value = _findfirst(searchPath, &fileinfo);

    while (_findnext(value, &fileinfo) == 0)
    {
        if (strcmp(fileinfo.name, "..") != 0 && strcmp(fileinfo.name, ".") != 0)
        {
            strcpy(moved, dest);
            strcat(moved, "\\");
            strcat(moved, fileinfo.name);
            rename(fileinfo.name, moved);
        }
        if (strcmp(fileinfo.name, "..") && fileinfo.attrib & _A_SUBDIR)
        {
            strcpy(newPath, path);
            strcat(newPath, "\\");
            strcat(newPath, fileinfo.name);
            strcpy(old, dest);
            strcat(dest, "\\");
            strcat(dest, fileinfo.name);
            mkdir(dest);
            recursiveTraverse(newPath);
            strcpy(dest, old);
        }
    }
    _findclose(value);
}
Zeus
  • 3,703
  • 3
  • 7
  • 20
ad3luta
  • 19
  • 4
  • 1
    C is not the same as C++. **See [this](https://en.cppreference.com/w).** Consider using in C++ frameworks like [Qt](http://qt.io/) or [POCO](http://pocoproject.org/). If you use the [GCC](http://gcc.gnu.org/) compiler, invoke it -with all warnings and debug info- as `g++ -Wall -Wextra -g` then use the [GDB](https://www.gnu.org/software/gdb/) debugger to understand the behavior of your program – Basile Starynkevitch Mar 17 '21 at 07:17
  • Study also for inspiration the source code of [ninja](http://ninja-build.org/) or of [GNU make](https://www.gnu.org/software/make/). Both are open source programs relevant to your question. Perhaps you want to use [Glib](https://developer.gnome.org/glib/) whose source code should be inspirational! – Basile Starynkevitch Mar 17 '21 at 07:20
  • For C code, use GCC as `gcc -Wall -Wextra -g` – Basile Starynkevitch Mar 17 '21 at 07:24
  • Read also [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) or some newer C standard. It does not mention "folders". If allowed, consider installing [Debian](http://debian.org/) on your own laptop, since it is made of open source software whose source code you could study and improve – Basile Starynkevitch Mar 17 '21 at 07:25
  • I hope [this](https://stackoverflow.com/questions/15177378/recursive-directory-copying-in-c) will help you. Good Day – Ananth Krish Mar 17 '21 at 07:31
  • If you only need to copy, you can not use `rename` and try to use [`CopyFile`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-copyfile). The flag you want to use later is like the copy parameter in Linux instead of the parameter in Windows. Could you specify what you want to achieve ? – Zeus Mar 18 '21 at 02:11
  • It is a homework and teacher said something about main's arguments and I don't really understand "flags" concept because I never used them in Windows. – ad3luta Mar 18 '21 at 13:48
  • 1
    Try to modify `rename(fileinfo.name, moved);` to `CopyFileA(fileinfo.name, moved,TRUE);`. This will copy the file instead of moving the file. – Zeus Mar 19 '21 at 01:27

0 Answers0