C# Recursive find file using Pattern - c#

I have a Visual Studio solution with the following structure:
- root
- src
- project01
- project02
- config
- application.config
- database.dacpac
Now, assuming I am executing a code from the path:
C:\DEV\root\project01\bin\release\project01.exe
How can I find the first occurrence of the file "application.config" for example?
I know that I can start with this:
public string GetExecutingDirectory(){
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
And use it in the following way:
var executingDirectory = GetExecutingDirectory();
But now, how can I traverse the Root Tree back and forward until I find the file I am looking for?
I tried this code but it doesn't find anything:
var path = Directory
.GetFiles(executingDirectory,
"**/application.config",
System.IO.SearchOption.AllDirectories);
I need it because I am running Integration Tests from different paths and they need these files which can be in a different location of the solution tree depending on the DEV machine configuration so I need to use the "pattern" because the structure can be different from PC to PC.

You can do like this:
static string FindConfig()
{
string appFolder = Path.GetDirectoryName(#"C:\DEV\root\project01\bin\release\project01.exe");
string configPath = null;
while (true)
{
Console.WriteLine(appFolder);
string[] files = Directory.GetFiles(appFolder, "*application.config", SearchOption.AllDirectories);
foreach (var file in files)
{
if (file.ToLower().EndsWith(#"\application.config"))
{
return file;
}
}
appFolder = Path.GetDirectoryName(appFolder);
if (appFolder.Length < 4) // "C:\" don't search root drive
{
break;
}
}
return configPath;
}

Related

How do I move all subfolders in one directory to another folder in c#?

I have a directory with folders and files, and I want to move all the folders into another folder that's inside the directory.
I already can move all the files in the directory(Not including the files in the subfolders), but I am also trying to figure out how to move the subfolders.
string selectedDrive = comboBox1.SelectedItem.ToString();
string folderName = selectedDrive + #"\\Encrypted"; // Creates a directory under the selected drive.
System.IO.Directory.CreateDirectory(folderName);
string moveTo = (selectedDrive + #"\Encrypted");
String [] allFolders = Directory.GetDirectories(selectedDrive); //Should return folder
foreach (String Folder in allFolders)
{
if (!Directory.Exists(moveTo + Folder))
{
FileSystem.CopyDirectory(Folder, moveTo);
}
}
Instead, I have an error at this line FileSystem.CopyDirectory(Folder, moveTo);
saying the following: System.IO.IOException: 'Could not complete operation since source directory and target directory are the same.'
I have reworked your code to make it work in this way:
// source of copies, but notice the endslash, it's required
// if you use the string replace method below
string selectedDrive = #"E:\temp\";
string destFolderName = Path.Combine(selectedDrive, "Encrypted");
System.IO.Directory.CreateDirectory(destFolderName);
// All folders except the one just created
var allFolders = Directory.EnumerateDirectories(selectedDrive)
.Except(new[] { destFolderName });
// Loop over the sources
foreach (string source in allFolders)
{
// this line in framework 4.8,
string relative = source.Replace(selectedDrive, "");
// or this line if using NET Core 6
// string relative = Path.GetRelativePath(selectedDrive, source));
// Create the full destination name
string dest = Path.Combine(destFolderName, relative);
if (!Directory.Exists(dest))
{
FileSystem.CopyDirectory(source, dest);
}
}
Also noted now that you are using a root drive (F:) as source for the directories to copy. In this case you should consider that you have other folders not accessible or not copy-able. (For example the Recycle.bin folder). However you can easily add other exclusions to the array passed to the IEnumerable extension Except
....
.Except(new[] { destFolderName, "Recycle.bin" });

Directory.EnumerateDirectories : Only get back Directores with a special file in it [duplicate]

I wish to get list of all the folders/directories that has a particular file in it. How do I do this using C# code.
Eg: Consider I have 20 folders of which 7 of them have a file named "abc.txt". I wish to know all folders that has the file "abc.txt".
I know that we can do this by looking thru all the folders in the path and for each check if the File.Exists(filename); But I wish to know if there is any other way of doing the same rather than looping through all the folder (which may me little time consuming in the case when there are many folders).
Thanks
-Nayan
I would use the method EnumerateFiles of the Directory class with a search pattern and the SearchOption to include AllDirectories. This will return all files (full filename including directory) that match the pattern.
Using the Path class you get the directory of the file.
string rootDirectory = //your root directory;
var foundFiles = Directory.EnumerateFiles(rootDirectory , "abc.txt", SearchOption.AllDirectories);
foreach (var file in foundFiles){
Console.WriteLine(System.IO.Path.GetDirectoryName(file));
}
EnumerateFiles is only available since .NET Framework 4. If you are working with an older version of the .NET Framework then you could use GetFiles of the Directory class.
Update (see comment from PLB):
The code above will fail if the access to a directory in denied. In this case you will need to search each directory one after one to handle exceptions.
public static void SearchFilesRecursivAndPrintOut(string root, string pattern)
{
//Console.WriteLine(root);
try
{
var childDireactory = Directory.EnumerateDirectories(root);
var files = Directory.EnumerateFiles(root, pattern);
foreach (var file in files)
{
Console.WriteLine(System.IO.Path.GetDirectoryName(file));
}
foreach (var dir in childDireactory)
{
SearchRecursiv(dir, pattern);
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
The following shows how to narrow down your search by specific criteria (i.e. include only DLLs that contain "Microsoft", "IBM" or "nHibernate" in its name).
var filez = Directory.EnumerateFiles(#"c:\MLBWRT", "*.dll", SearchOption.AllDirectories)
.Where(
s => s.ToLower().Contains("microsoft")
&& s.ToLower().Contains("ibm")
&& s.ToLower().Contains("nhibernate"));
string[] allFiles = filez.ToArray<string>();
for (int i = 0; i < allFiles.Length; i++) {
FileInfo fInfo = new FileInfo(allFiles[i]);
Console.WriteLine(fInfo.Name);
}

How to build a list of directories from args that may contain drive letters and/or wildcards

I want to pass a list of directories to my C# console application as arguments which may be relative paths based on the current working directory, or contain drive letters and/or wildcards. E.g.:
myApp.exe someDir another\dir dirs\with\wild*cards* ..\..\a\relative\dir C:\an\absolute\dir\with\wild*cards*
So far I've come up with this:
static void Main(string[] args)
{
List<string> directories = new List<string>();
foreach (string arg in args)
{
if (Directory.Exists(arg))
{
// 'arg' by itself is a valid directory, and must not contain any wildcards
// just add it to 'directories'
directories.Add(arg);
}
else
{
// 'arg' must either be a non-existant directory or invalid directory name,
// or else it contains wildcard(s). Find all matching directory names, starting
// at the current directory (assuming 'arg' might be a relative path), and add
// all matching directory names to 'directories'.
string[] dirs = Directory.GetDirectories(Directory.GetCurrentDirectory(), arg, SearchOption.TopDirectoryOnly);
directories.AddRange(dirs);
}
}
Console.WriteLine("Full list of directories specified by the command line args:");
foreach (string dir in directories)
{
Console.WriteLine(" " + dir);
}
// Now go do what I want to do for each of these directories...
}
This works great for someDir, another\dir, and dirs\with\wild*cards*, but won't work for ..\..\a\relative\dir or C:\an\abolute\dir\with\wild*cards*. For the relative dir, Directory.GetDirectories() throws a System.ArgumentException saying "Search pattern cannot contain '..' to move up directories and can be contained only internally in file/directory names, as in 'a..b'." For the drive-letter-based directory, it throws a System.ArgumentException saying "Second path fragment must not be a drive or UNC name."
How can I handle the ".." and drive letters? This has to be a solved problem, but I can't find examples of such for C#.
"Bonus question": My above code also doesn't handle a\path\with\wild*cards*\in\anything\other\than\the\top\directory. Any easy way to handle that too?
Couple of observations:
1) To check if path absolute or relative - you can use Path.IsRooted()
2) To resolve path with ".." to absolute path (be it relative or absolute) you can use:
path = new Uri(Path.Combine(Directory.GetCurrentDirectory(), path)).AbsolutePath;
Routing it though Uri expands those dots. Path.GetFullPath will fail in case of wildcards, but Uri will not.
3) To check if path contains wildcards you can just do path.Contains("*") || path.Contains("?") since both of those characters are not valid path chars, so cannot be present in a context other than being wildcards.
4) To resolve wildcard in absolute path (and match your "bonus" requirement) you need to find out first directory which does not contain wildcard. So you basically need to split path into to parts - before first wildcard and after first wildcard. For example:
C:\an\abolute\dir\with\wild*cards*
Path before wildcard is C:\an\abolute\dir\with, after (and including) wildcard: wild*cards*.
C:\an\abolu*e\dir\with\wild*cards*
Path before first wildcard: C:\an, after: abolu*e\dir\with\wild*cards*. There are different ways to do that of course, easiest I think is regex:
#"[\\/](?=[^\\/]*[\*?])"
It basically matches directory separator, but only if it is followed by 0 or more charactres which are NOT directory separators, then followed by wildcard symbol.
Combining this all together we have:
static IEnumerable<string> ResolveDirectories(string path) {
if (path.Contains("*") || path.Contains("?")) {
// we have a wildcard,
// resolve it to absolute path if necessary
if (!Path.IsPathRooted(path))
path = Path.Combine(Directory.GetCurrentDirectory(), path);
// resolve .. stuff if any
path = new Uri(Path.Combine(Directory.GetCurrentDirectory(), path)).AbsolutePath;
// split with regex above, only on first match (2 parts)
var parts = new Regex(#"[\\/](?=[^\\/]*[\*?])").Split(path, 2);
var searchRoot = parts[0];
var searchPatterns = parts[1].Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
foreach (var dir in ResolveWildcards(searchRoot, searchPatterns))
yield return dir;
}
else {
// this should work with "../.." type of paths too, will resolve relative to current directory
// so no processing is necessary
if (Directory.Exists(path)) {
yield return path;
}
else {
// invalid directory?
}
}
}
static IEnumerable<string> ResolveWildcards(string searchRoot, string [] searchPatterns) {
// if we have path C:\an\abolu*e\dir\with\wild*cards*
// search root is C:\an
// and patterns are: [abolu*e, dir, with, wild*cards*]
if (Directory.Exists(searchRoot)) {
// use next pattern to search in a search root
var next = searchPatterns[0];
// leave the rest for recursion
var rest = searchPatterns.Skip(1).ToArray();
foreach (var dir in Directory.EnumerateDirectories(searchRoot, next, SearchOption.AllDirectories)) {
// if nothing left (last pattern) - return it
if (rest.Length == 0)
yield return dir;
else {
// otherwise search with rest patterns in freshly found directory
foreach (var sub in ResolveWildcards(dir, rest))
yield return sub;
}
}
}
}
This is not properly tested, so take care.
'Evk' posted his/her answer a few minutes before I was about to post this, so I have not evaluated Evk's answer.
After finding this question+answer which might have worked fine but I didn't test it, I stumbled across Michael Ganss's Glob.cs NuGet package, and the results are very nice:
// Requires Glob.cs, (c) 2013 Michael Ganss, downloaded via the NuGet package manager:
// https://www.nuget.org/packages/Glob.cs
// (In Visual Studio, go to Tools->NuGet Package Manager->Package Manager Console.)
// PM> Install-Package Glob.cs -Version 1.3.0
using Glob;
namespace StackOverflow_cs
{
class Program
{
static void Main(string[] args)
{
List<string> directories = new List<string>();
foreach (string arg in args)
{
var dirs = Glob.Glob.Expand(arg, true, true);
foreach (var dir in dirs)
{
directories.Add(dir.FullName);
}
}
Console.WriteLine("Full list of directories specified by the command line args:");
foreach (string dir in directories)
{
Console.WriteLine(" " + dir);
}
// Now go do what I want to do for each of these directories......
}
}
}
But I don't know why I have to say "Glob.Glob.Expand()" instead of simply "Glob.Expand()". Anyway, it works beautifully.

C# Move files inside dialog's path

I'm struggling with this basic operation.It will be nice if someone can write a working code.So let's say I got folder "AB" on desktop.Folder AB contains subfolder A and subfolder B.Subfolder A contains A.txt and Subfolder B contains B.txt.I want the user to simply choose folder AB via a browser dialog(I did that already) and then,when he clicks on a checkbox,file A.txt will go on subfolder B and B.txt will go on subfolder A.
I will do this for simple folders A and B. You will have to consider the chances of sub-folders as well.
string[] filesA = System.IO.Directory.GetFiles(AsourcePath);
string[] filesB = System.IO.Directory.GetFiles(BsourcePath);
foreach (string s in filesA)
{
System.IO.File.Move(s, AsourcePath);
}
foreach (string s in filesB)
{
System.IO.File.Move(s, BsourcePath);
}
Please Note: You will have consider so many scenarios for this including sub-folders, overwriting, existing files or folders etc.
Assuming that you have the folder path for both A and B folders,
var Afolder = #"D:\AB\A";
var Bfolder = #"D:\AB\B";
SwapFolderFiles(Afolder, Bfolder);
Pass the folder path for both A and B to SwapFolderFiles,
private static void SwapFolderFiles(string AFolder, string BFolder)
{
var AFolderfiles = System.IO.Directory.GetFiles(AFolder);
var BFolderfiles = System.IO.Directory.GetFiles(BFolder);
MoveFiles(AFolder, BFolder, AFolderfiles);
MoveFiles(BFolder, AFolder, BFolderfiles);
}
private static void MoveFiles(string sourceFolder, string destinationFolder, string[] folderfiles)
{
foreach (var file in folderfiles)
{
var filename = file.Substring(file.LastIndexOf("\\")+1);
var source = System.IO.Path.Combine(sourceFolder, filename);
var destination = System.IO.Path.Combine(destinationFolder, filename);
System.IO.File.Move(source, destination);
}
}

How to create a path using the Windows.Storage API?

System.IO.CreateDirectory() is not available on .NET for Windows Store Apps.
How can I implement this equivalent method? StorageFolder.CreateFolderAsync() creates a subfolder inside the current folder, but in my case I have a path like and need to create all folders that doesn't exist in this path.
The path is inside the app's sandbox in windows.
There's no API with exactly the same behaviour of System.IO.CreateDirectory(), so I implemented it using Windows.Storage classes:
// Any and all directories specified in path are created, unless they already exist or unless
// some part of path is invalid. If the directory already exists, this method does not create a
// new directory.
// The path parameter specifies a directory path, not a file path, and it must in
// the ApplicationData domain.
// Trailing spaces are removed from the end of the path parameter before creating the directory.
public static void CreateDirectory(string path)
{
path = path.Replace('/', '\\').TrimEnd('\\');
StorageFolder folder = null;
foreach(var f in new StorageFolder[] {
ApplicationData.Current.LocalFolder,
ApplicationData.Current.RoamingFolder,
ApplicationData.Current.TemporaryFolder } )
{
string p = ParsePath(path, f);
if (f != null)
{
path = p;
folder = f;
break;
}
}
if(path == null)
throw new NotSupportedException("This method implementation doesn't support " +
"parameters outside paths accessible by ApplicationData.");
string[] folderNames = path.Split('\\');
for (int i = 0; i < folderNames.Length; i++)
{
var task = folder.CreateFolderAsync(folderNames[i], CreationCollisionOption.OpenIfExists).AsTask();
task.Wait();
if (task.Exception != null)
throw task.Exception;
folder = task.Result;
}
}
private static string ParsePath(string path, StorageFolder folder)
{
if (path.Contains(folder.Path))
{
path = path.Substring(path.LastIndexOf(folder.Path) + folder.Path.Length + 1);
return path;
}
return null;
}
To create folders outside your app's sandbox in windows store app, you'll have to add the document library in app-manifest, along with file permissions.
For a much more detailed explanation regarding library and documents, Refer this blog

Categories

Resources