我如何...用通配符复制文件夹中的文件(文件夹路径有通配符); C#; 。净; Windows应用程序 [英] How do I...Copy the files from folders recursively with wildcard characters (folder path has wildcard characters); C#; .Net; Windows App

查看:190
本文介绍了我如何...用通配符复制文件夹中的文件(文件夹路径有通配符); C#; 。净; Windows应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



美好的一天。



我必须复制文件夹中的所有文件(包括子文件夹)到其他共享驱动器位置以备份数据。我面临的挑战是带有通配符的文件夹路径。



例如,



文件夹结构如下



D:/ Folder1 / Folder11 / Folder111



D:/ Folder2 / Folder22 / Folder222



D:/ Folder3 / Folder33 / Folder333



我正在寻找输入格式应该是D:/ Folder?/ Folder * / Folder *。因此它必须根据通配符字符模式循环。



能帮助我吗。



问候,



Chandra

Hi All,

Good day.

I have to copy all the files in folders (including sub folders) to other shared drive location for backup the data. The challenge, which I am facing is folder path with wildcard characters.

For example,

The folder structure is like below

D:/Folder1/Folder11/Folder111

D:/Folder2/Folder22/Folder222

D:/Folder3/Folder33/Folder333

I am looking for the input format should be "D:/Folder?/Folder*/Folder*". So that it has to loop according to the wildcard character patterns.

Can you please help me.

Regards,

Chandra

推荐答案

fyi:Directory.GetDirectories支持使用Type的模式参数进行搜索允许您使用某些通配符字符的字符串...星号和问号...(但不接受RegEx表达式):[ ^ ]
fyi: Directory.GetDirectories supports searching with a pattern argument of Type string that allows you to use certain wild-card characters ... asterisk and question-mark ... (but does not accept a RegEx expresion): [^]
string searchPat = @"D:/Folder?/Folder*/Folder*";
DirectoryInfo di = new DirectoryInfo(@"D:\);
DirectoryInfo[] directories = di.GetDirectories(searchPattern, SearchOption.AllDirectories);

获得所有匹配目录的列表后,即可复制目录:



1.遍历目录中的DirectoryInfo数组,进行外卡搜索生成



2.获取DirectoryInfo的'FullName属性(完整文件路径):在目标驱动器上创建匹配的目录



然后你可以递归复制每个目录的内容到目标驱动器上具有相同名称的新创建的目标目录。



网上有很多示例,包含递归文件夹复制的代码;这里展示和讨论了几个:[ ^ ]。这个MDSN页面上有一个完整的例子:[ ^ ](第二个代码示例)。



您可能会想到的问题:



1.隐藏文件或文件夹?



2.复制时的访问/角色/权限问题?

Once you have a list of all matching Directories, you can then copy the directories:

1. iterate over the array of DirectoryInfo in 'directories the wild-card search produced

2. get the 'FullName Property (the full file-path) of the DirectoryInfo: create a matching Directory on the target drive

Then you can recursively copy the contents of each Directory to the newly created target directory with the same name on the target drive.

There are lots of examples on the net with code for recursive folder copying; several are shown and discussed here: [^]. There's a full example on this MDSN page: [^] (the second code example).

Issues you might think about:

1. hidden files or folders ?

2. issues of access/role/permission when copying ?


使用 Directory.GetFiles 方法

[ ^ ]列出所有文件和子目录。

在递归循环中,您可以访问树底部的所有文件。但是,我建议您使用专有备份程序进行调查,因为以这种方式进行复制并不能保证安全备份。
Use the Directory.GetFiles Method
[^] to list all files and subdirectories.
In a recursive loop you can then access all files down to the bottom of the tree. However, I would suggest you investigate using a proprietary backup program as copying in this way does not guarantee secure backups.


这篇关于我如何...用通配符复制文件夹中的文件(文件夹路径有通配符); C#; 。净; Windows应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆