简体   繁体   中英

C++ - Copying a file to all sub directories

I'm trying to create a program for educative purposes which should be able to copy one file (of any extension, the name of the file never changes) inside the same directory of the .exe to all the directories inside the main directory. Thus for example I have:

  • Main_Directory
  • Program.exe
  • file.something
  • Directory1
  • Directory2
  • Directory3

In that situation, the program should copy file.something into directory1, 2 and 3. Through the use of this code I have access to the absolute path of the .exe 's folder:

char basePath[255]="";
_fullpath(basePath,argv[0],sizeof(basePath));

Ideally, to solve the problem, two function could be made:

First function:

Input:
(char[]) The .exe's folder if necessary
Output:
(int) Number of folders inside the .exe's folder.

Second function:

input:
(int) which folder to pick
(int) total number of folders
(char[]) the .exe's folder if necessary
Output:
(char[]) the name of the folder

With those two functions (which should return an array of chars instead of a string as I was forced to use those arrays to make the code work) I'd be able to make a simple loop, changing the destination's folder every time and copying the file to it.

Of course it's just how the problem could be fixed, if a better solution exists, so be it.

One last thing: I'd be really happy if you could add what each block or command does as I'm still learning c++ and I'm currently a beginner.

Assumptions:

  • You write non portable code, making use of non portable function [_fullpath() ] 1
  • You target windows only
  • You work with unicode disabled as you use _fillpath() and not _wfullpath()
  • You do not want to use third party libraries

How to do it with windows ?

You could use FindFirstFile() and FindNextFile() to iterate through the directories. MSDN has a nice example of how to use them.

Unfortunately FindFirstFile() is very basic and limits itself to search entries based on a filename containing wildcards. As you are looking for directories, it's more tricky: you'll have to use FindFirstFileEx() . The parameter called fSearchOp in the function documentation should be FindExSearchLimitToDirectories

The data is returned in WIN32_FIND_DATA structure where you can read the cFileName . By the way, try to avoid at any cost hard coded length like 255 and prefer well known constants instead (here the windows specific MAX_PATH).

Finally, while you are looping, you may perform the copying either using your own C++ standard based copy function, or using the windows specific CopyFile()

Final remarks

This kind of gymnastics is not at all portable. If you'd like to use your code on other platforms than windows, you'd have to use a completely different API, for example dirent.h for unix/linux and co. This is why, for this particular kind of work, a third party library like boost is extremely useful. If you have time, look here for a directory iteration tutorial with boost and compare with the windows based code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM