简体   繁体   English

如何判断文件夹是否是递归文件夹副本的子文件夹?

[英]How to tell if folder is a subfolder for recursive folder copy?

I'm trying to implement a folder copy method that calls FindFirstFile and FindNextFile in a loop, that may call itself recursively on any subfolders. 我正在尝试实现一个在循环中调用FindFirstFileFindNextFile的文件夹复制方法,它可以在任何子文件夹上递归调用自身。

To prevent an obvious infinite loop I need to make sure that the destination folder is not a subfolder of a source folder. 为了防止明显的无限循环,我需要确保目标文件夹不是源文件夹的子文件夹。 The question is how to do that? 问题是如何做到这一点? My thinking was to translate a DOS path into a device specific path (need to find out how) but there seems to be more to it. 我的想法是将DOS路径转换为特定于设备的路径(需要找出方法),但似乎还有更多。

So I'm testing it for this situation: 所以我正在测试它的情况:

I set up My Documents folder to be redirected to a network share to \\\\Server\\Home\\UserA\\Documents , plus that folder is also mapped to the drive R: on the client machine. 我将My Documents文件夹设置为重定向到网络共享到\\\\Server\\Home\\UserA\\Documents ,此外该文件夹也映射到客户端计算机上的驱动器R: \\\\Server\\Home\\UserA\\Documents So that means that all of the following folders: 这意味着所有以下文件夹:

"R:\Documents\Subfolder1"
"\\Server\Home\UserA\Documents\Subfolder1"
"C:\Users\UserA\Documents\Subfolder1"

point technically to the same physical location, that is a subfolder of My Documents . 从技术上讲,指向同一物理位置,即“ My Documents的子文件夹。

The question is how to know this reliably? 问题是如何可靠地了解这一点?

Use GetFileInformationByHandle to retrieve the volume serial number and file index for the destination directory and for each possible match. 使用GetFileInformationByHandle检索目标目录和每个可能匹配的卷序列号和文件索引。 If both the serial number and the file index are the same, they are the same directory. 如果序列号和文件索引都相同,则它们是同一目录。

Note that you will need to use the FILE_FLAG_BACKUP_SEMANTICS flag in CreateFile in order to open a handle to a directory. 请注意,您需要在CreateFile中使用FILE_FLAG_BACKUP_SEMANTICS标志才能打开目录句柄。 (You do not need backup privilege to do so.) (您不需要备份权限。)

It may be possible for cloned volumes to have the same serial number (I'm not sure offhand whether Windows forces a serial number change or not) so it might be wise to provide an option to the user that disables this check. 克隆卷可能具有相同的序列号(我不确定Windows是否强制更改序列号),因此向用户提供禁用此检查的选项可能是明智之举。

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

相关问题 如何判断一个文件夹是否是特殊Windows文件夹的子文件夹? - How to tell if a folder is a subfolder of a special Windows folder? 将文件夹和子文件夹中的所有 files.doc 或 .docx 复制到另一个文件夹中 - Copy all files .doc or .docx in folder and subfolder into another folder 如何将文件从一个文件夹复制到另一个文件夹 - How to copy a file from a folder to another folder 具有自动递归文件夹/目录创建的C / C ++复制文件 - C/C++ Copy file with automatic recursive folder/directory creation 计算文件夹中的文件(包括子文件夹) - Count File in a folder (include subfolder) 如何复制“程序文件”文件夹中的文件或文件夹 - How do I copy a file Or folder in the Program Files folder 如何使用 Premake5 复制包含资源的文件夹 - How to copy a folder with resources using Premake5 如何判断路径是否来自Active Directory重定向文件夹? - How to tell if a path is from an Active Directory redirected folder? 在 listWidget 中显示文件夹和子文件夹中的所有文件 - display all Files in Folder and Subfolder in listWidget 如何告诉 QPluginLoader 检查包含文件夹而不是 exe 文件夹中的 dll 依赖项 - How to tell QPluginLoader to check for dll dependencies in containing folder instead of exe folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM