In my application I could have three paths
- \Shared\1.txt
- \Shared\
- \Shared\*.txt
This file paths will come in variable.
Now How can I check if path has single file or multiple or it has wildcard ? & then move them to another path.
In my application I could have three paths
This file paths will come in variable.
Now How can I check if path has single file or multiple or it has wildcard ? & then move them to another path.
Well, since neither *
nor ?
can be in the path: they are in
Char[] forbidden = Path.GetInvalidPathChars();
so you can just look for them
String path = @"C:\MyData\Shared\*.txt";
...
Boolean isWildCard = path.ContainsAny('?', '*');
As for File/Directory
Boolean isFile;
if (File.Exists(path))
isFile = true; // file already exists
else if (Directory.Exists(path))
isFile = false; // directory already exists
else if (String.Equals(Path.GetExtension(path), ".txt", StringComparison.InvariantCultureIgnoreCase))
isFile = true; // has txt extension, let it be a file
else
isFile = false;
However it seems that you have no need to have any branches (isWildCard, isFile) and just move files:
String path = @"C:\MyData\Shared\*.txt";
...
String sourceDirectory = Path.GetDirectoryName(path);
String destination = @"C:\Destination";
Directory.GetFiles(sourceDirectory, "*.txt")
.ForEach(file => File.Move(file, Path.Combine(destination, Path.GetFileName(file))));