Click here to Skip to main content
15,918,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
OriginalGriff 18-Dec-15 6:14am    
What have you tried?
Where are you stuck?
What help do you need?

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): [^]
C#
string searchPat = @"D:/Folder?/Folder*/Folder*";
DirectoryInfo di = new DirectoryInfo(@"D:\);
DirectoryInfo[] directories = di.GetDirectories(searchPattern, SearchOption.AllDirectories);
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 ?
 
Share this answer
 
Comments
ChandraJ 30-Dec-15 0:07am    
@BillWoodruff, Thanks for your solution. Got some idea to implement. There is no problem with hidden files/ folders and permission.
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900