简体   繁体   English

C ++-将文件复制到所有子目录

[英]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. 我正在尝试创建一个出于教育目的的程序,该程序应该能够将.exe相同目录内的一个文件(任何扩展名,文件名永不更改)复制到主目录内的所有目录中。 Thus for example I have: 因此,例如,我有:

  • Main_Directory 主目录
  • Program.exe 程式
  • file.something file.something
  • Directory1 目录1
  • Directory2 目录2
  • Directory3 目录3

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: 在这种情况下,程序应将file.something复制到directory1、2和3。通过使用此代码,我可以访问.exe文件夹的绝对路径:

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. 使用这两个函数(应该返回一个char数组而不是一个字符串,因为我被迫使用这些数组使代码正常工作),我将能够进行一个简单的循环,每次更改目标文件夹并复制归档到它。

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. 最后一件事:如果您可以添加每个块或命令的功能,因为我还在学习c ++,而我目前是一个初学者,我将非常高兴。

Assumptions: 假设:

  • You write non portable code, making use of non portable function [_fullpath() ] 1 您使用非可移植函数[_fullpath() ] 1编写非可移植代码
  • You target windows only 您仅定位Windows
  • You work with unicode disabled as you use _fillpath() and not _wfullpath() 您使用_fillpath()而不是_wfullpath()禁用了Unicode
  • You do not want to use third party libraries 您不想使用第三方库

How to do it with windows ? 用Windows怎么做?

You could use FindFirstFile() and FindNextFile() to iterate through the directories. 您可以使用FindFirstFile()和FindNextFile()遍历目录。 MSDN has a nice example of how to use them. MSDN有一个很好的示例 ,说明了如何使用它们。

Unfortunately FindFirstFile() is very basic and limits itself to search entries based on a filename containing wildcards. 不幸的是, FindFirstFile()是非常基础的,它自身只能基于包含通配符的文件名来搜索条目。 As you are looking for directories, it's more tricky: you'll have to use FindFirstFileEx() . 在查找目录时,这会更加棘手:您必须使用FindFirstFileEx() The parameter called fSearchOp in the function documentation should be FindExSearchLimitToDirectories 函数文档中称为fSearchOp的参数应为FindExSearchLimitToDirectories

The data is returned in WIN32_FIND_DATA structure where you can read the cFileName . 数据以WIN32_FIND_DATA结构返回,您可以在其中读取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). 顺便说一句,尝试不惜一切代价避免像255这样的硬编码长度,而改用众所周知的常量(此处是Windows特定的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() 最后,在循环时,您可以使用自己的基于C ++标准的复制功能或Windows特定的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. 如果要在Windows以外的其他平台上使用代码,则必须使用完全不同的API,例如,针对unix / linux和co的dirent.h This is why, for this particular kind of work, a third party library like boost is extremely useful. 这就是为什么对于这种特殊的工作,像boost这样的第三方库非常有用。 If you have time, look here for a directory iteration tutorial with boost and compare with the windows based code. 如果有时间,请在此处查找具有boost的目录迭代教程,并与基于Windows的代码进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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