使用 Qt 复制目录
我想将目录从一个驱动器复制到另一个驱动器。我选择的目录包含许多子目录和文件。
我如何使用 Qt 实现相同的功能?
I want to copy a directory from one drive to another drive. My selected directory contains many sub directories and files.
How can I implement the same using Qt?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以手动执行以下操作:
1)。使用下面的 func 您可以生成文件夹/文件列表(递归地) - 目标文件。
2)。然后您可以开始将文件从目标列表复制到新的源目录,如下所示:
仅此而已。祝你好运!
Manually, you can do the next things:
1). with func below you generate folders/files list (recursively) - the destination files.
2). then you may to start copying files from destination list to the new source directory like that:
And that's all. Good luck!
我想要类似的东西,并且在谷歌上搜索(徒劳),所以这就是我必须做的:
它使用看起来非常相似的
rmDir
函数:这不处理链接和特殊文件,顺便提一句。
I wanted something similar, and was googling (in vain), so this is where I've got to:
It uses an
rmDir
function that looks pretty similar:This doesn't handle links and special files, btw.
艰难的方式。单独复制每个文件。
QDir::entryList()
迭代目录的内容QDir::cd()
和QDir::cdUp()
进出目录The hard way. Copy every file individually.
QDir::entryList()
to iterate over the content of a directoryQDir::cd()
andQDir::cdUp()
to go in and out of directoriesQDir::mkdir()
andQDir::mkpath()
to create the new folders treeQFile::copy()
to copy the actual files这基本上是 petch 的答案,由于它在 Qt 5.6 中对我造成了破坏,因此略有变化(这是最热门的问题) ,因此所有功劳都归于 petch。
功能
用途:
This is basically petch's answer with a slight change due to it breaking for me in Qt 5.6 (this is the top question hit), so all credit goes to petch.
function
Use:
试试这个:
Try this:
我制作了一个库来通过 shell 命令风格的 API 来操作文件。它支持文件的递归复制并处理更多条件。
https://github.com/benlau/qtshell#cp
示例
I have made a library to manipulate files by a shell command style API. It supports a recursively copy of files and handled several more conditions.
https://github.com/benlau/qtshell#cp
Example
由于我在 macOS 上使用 App-Bundles 时遇到了一些问题,这里有一个使用 QDirIterator 的解决方案
}
Since I had some trouble with App-Bundles on macOS, here's a solution with QDirIterator
}
如果您使用的是基于 Linux 的系统,并且
cp
命令存在并且可以运行,那么您可以使用QProcess
启动 bash:cp 详细信息:
- r
表示递归-v
表示打印成功复制的文件注意:如果复制操作很长,那么您需要管理 UI 冻结,如前所述此处
If you are on a linux based system and the
cp
command exists and can be run, then you can use aQProcess
to launch a bash:cp details:
-r
means recursively-v
means it prints the successfully copied fileNote: if the copy operation is long, then you need to managed the UI freezing, as noted here
具有相同的名称”并且使用递归没有问题
功能!! 那么代码将是这样的,
递归地复制目录,那么如果您曾经处理过树数据结构并尝试创建一个递归函数来执行“深度优先搜索”算法, 您将得到一个< strong>85%相似的算法,实际上我就是从中得到这个想法的。
目录中的fileInfoList,以及相应的状态来显示if
您是否已经使用过此文件信息。你收集所有
首先是有关源位置的子目录和文件的信息。
这就是大多数操作系统和其他文件管理器复制数据的方式,通过显示要复制的文件的大小、要复制的文件和文件夹的数量,以及最后是否存在文件或文件夹冲突甚至在开始复制之前就使用相同的名称“如果您将对目标执行相同的算法,以便可以匹配文件名”。
祝你好运!。
with the same names" and you have no problem to use a Recursive
function!! to copy a directory recursively then the code will be something like this
if you ever dealt with tree data structures and tried to create a Recursive function to do a "depth-first search" Alogorithm you will get a 85% similar algorithm, which Actually I got this idea from.
fileInfoList in a directory, and the corresponding state to show if
you have used this fileInfo or not yet. and you gather all
information firstly about sub directories and files from the source location.
And this is how most OS and other file managers do to copy data, by showing you the size of files to be copied, how many files and folders are going to be copied, and finally, if there is any conflict of files or folders with the same name before you even initiate copying "if you will do the same algorithm with the destination so that you can match filenames".
Good luck!.