使用 windows CopyFile 函数复制所有具有特定名称格式的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3026270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:38:19  来源:igfitidea点击:

Using windows CopyFile function to copy all files with certain name format

c++windowsfile-copying

提问by Ben313

Hello! I am updating some C code that copys files with a certain name. basically, I have a directory with a bunch of files named like so:

你好!我正在更新一些复制具有特定名称的文件的 C 代码。基本上,我有一个包含一堆文件的目录,如下所示:

AAAAA.1.XYZ
AAAAA.2.ZYX
AAAAA.3.YZX
BBBBB.1.XYZ
BBBBB.2.ZYX

AAAAA.1.XYZ
AAAAA.2.ZYX
AAAAA.3.YZX
BBBBB.1.XYZ
BBBBB.2.ZYX

Now, In the old code, they just used a call to ShellExecute and used xcopy.exe. to get all the files starting with AAAAA, they just gave xcopy the name of the file as AAAAA.* and it knew to copy all of the files starting with AAAAA. now, im trying to get it to copy with out having to use the command line, and I am running into trouble. I was hoping CopyFile would be smart enough to handle AAAAA.* as the file to be copied, but it doesnt at all do what xcopy did. So, any Ideas on how to do this without the external call to xcopy.exe?

现在,在旧代码中,他们只是使用了对 ShellExecute 的调用并使用了 xcopy.exe。要获取以 AAAAA 开头的所有文件,他们只是将文件名称指定为 AAAAA.* 并且它知道复制所有以 AAAAA 开头的文件。现在,我试图在不必使用命令行的情况下复制它,但我遇到了麻烦。我希望 CopyFile 足够聪明,可以将 AAAAA.* 作为要复制的文件处理,但它根本没有像 xcopy 那样做。那么,关于如何在没有外部调用 xcopy.exe 的情况下执行此操作的任何想法?

回答by humbagumba

You could also use SHFileOperationor IFileOperation(the latter being only available from Vista upwards but is now the recommended way according to MSDN). SHFileOperation supports wildcards and displays a progress by default, but there's also a flag for silent operation.

您也可以使用SHFileOperationIFileOperation(后者仅适用于 Vista 以上,但现在是根据 MSDN 推荐的方式)。SHFileOperation 支持通配符并默认显示进度,但也有一个用于静默操作的标志。

Check out the following MSDN links for more info:

查看以下 MSDN 链接以获取更多信息:

http://msdn.microsoft.com/en-us/library/bb762164(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/bb762164(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/bb775771(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/bb775771(v=VS.85).aspx

回答by Michael J

There are lots of ways to do it. I'd probably use a loop of FindFirstFile() / FindNextFile().

有很多方法可以做到。我可能会使用 FindFirstFile() / FindNextFile() 循环。

However, is there any reason you can't still use xcopy? You can launch it with CreateProcess(). It isn't pretty, but it works.

但是,有什么理由不能继续使用 xcopy 吗?您可以使用 CreateProcess() 启动它。它不漂亮,但它有效。

回答by Amardeep AC9MF

You would basically have to write code to reproduce the functionality in xcopy. To do so, you must build a list of files by accessing the path and recursing through it. Test each found entry with your pattern and keep only those that match. Then iterate over that list with CopyFile.

您基本上必须编写代码来重现 xcopy 中的功能。为此,您必须通过访问路径并通过它递归来构建文件列表。使用您的模式测试每个找到的条目并仅保留那些匹配的条目。然后使用 CopyFile 迭代该列表。

See the following set of functions that can help you build the file list:

请参阅以下可以帮助您构建文件列表的函数集:

http://en.wikipedia.org/wiki/Dirent.h

http://en.wikipedia.org/wiki/Dirent.h

It might just be easier to keep using xcopy unless you have a specific reason not to.

除非有特殊原因,否则继续使用 xcopy 可能会更容易。