C# windows search and move

ItherNiT

Expert Member
Joined
Jan 31, 2011
Messages
1,080
Reaction score
507
Location
Center Banana Republic (Gauteng)
Hi all,

I'm busy trying to make a program that will do a windows search for a term (a variable) in a directory (saved as a var) and if it finds a results, those files need to be moved to another directory (also a var). Has someone been able to do this successfully before?

This is using C#, I'm trying to do this for a Uni project

Thnx for any help.
 
Code:
string searchTerm = "game of thrones";
var filteredFiles = Directory.GetFiles("C:\\someDirectory\\").Where(f => f.Contains(searchTerm));


Then some of the File objects static methods to move\rename\etc the file.
Just be mindful as you can quickly lock files...
 
You could also use DirectoryInfo to find the files based on a search pattern like you would use in a command line window:

Code:
DirectoryInfo di = new DirectoryInfo("path");
var files = di.GetFiles("*game of thrones*"); // gets all files containing the text "game of thrones"

The benefit of that is you can also specify a flag (second parameter) for .GetFiles to search through all sub directories for the pattern.

Maybe a more complete example of what you might want to achieve:

Code:
string oldPath = "old path",
       newPath = "new path",
       searchPattern = "*game of thrones*";
DirectoryInfo di = new DirectoryInfo(oldPath);
foreach (var file in di.GetFiles(searchPattern, SearchOption.AllDirectories))
{
    file.MoveTo(newPath + "\\" + file.Name);
}
 
Last edited:
Filebot works nicely, and formats the name and moves it to folders :)
 
You could also use DirectoryInfo to find the files based on a search pattern like you would use in a command line window:

Code:
DirectoryInfo di = new DirectoryInfo("path");
var files = di.GetFiles("*game of thrones*"); // gets all files containing the text "game of thrones"

The benefit of that is you can also specify a flag (second parameter) for .GetFiles to search through all sub directories for the pattern.

Maybe a more complete example of what you might want to achieve:

Code:
string oldPath = "old path",
       newPath = "new path",
       searchPattern = "*game of thrones*";
DirectoryInfo di = new DirectoryInfo(oldPath);
foreach (var file in di.GetFiles(searchPattern, SearchOption.AllDirectories))
{
    file.MoveTo(newPath + "\\" + file.Name);
}

Thnx allot code works great, I have now have another problem. The program crashes when the file already exist "newPath", is there a way to just add "(1)" to the file?
 
You can use file.Exists to check whether it already exists. If it does, you can either decide to append a number to it or ignore it completely. I'm on my phone at the moment, so can't help further than that right now.

Besides, exploring the capabilities of the libraries is half the fun... ;)
 
You can use file.Exists to check whether it already exists. If it does, you can either decide to append a number to it or ignore it completely. I'm on my phone at the moment, so can't help further than that right now.

Besides, exploring the capabilities of the libraries is half the fun... ;)

thnx for the advice, used the file.exists delete :D
 
I was Super bored during the weekend, So I tried creating this small program
Note: I had no idea how hard this was.. thought it was easy but I cracked it in 2 hours...Lots of documentation :)
NB: This is a C# console application

Code:
 string filepath = @"C:\Users\CentX_Sales\Desktop\Desktop2\test2\folder 1"; //Getting file path
            string folder2 = @"C:\Users\CentX_Sales\Desktop\Desktop2\test2\folder 2";//Where I want the new files too move to
            string sPattern = "gfh"; // what name should the document contain


            string[] folder1 = Directory.GetFiles(filepath);//get list of items
            foreach (string files in folder1) //Filters through pattern AND moves files too folder 2
                {
                    System.Console.WriteLine("Procesing");
                    if (System.Text.RegularExpressions.Regex.IsMatch(files, sPattern))
                    {

                       if (File.Exists(folder2))
                       {
                           File.Delete(folder2);
                       }
                       
                       File.Copy(files, folder2 +@"\"+ Path.GetFileName(files) , true);//adding too new folder
                       File.Delete(files);//deleting from old list
                        
                    }
                }

            System.Console.WriteLine("Process Complete");
            Console.ReadLine();
 
I take it you are a junior. So:

Don't call your variables filepath and folder2. Rather called it sourceFolder and destinationFolder (self documenting code)
sPattern = searchPattern/filter
folder1 = sourceFiles

And then the variable you use to loop ... foreach(var file in sourceFiles)

You may think it is nitpicking but naming your variables better will avoid things like File.Exists(folder2)

Rather use Path.Combine() instead of string + string



If i was your boss I'd slap you and make you wash my car over the weekend ;)
 
I take it you are a junior. So:

Don't call your variables filepath and folder2. Rather called it sourceFolder and destinationFolder (self documenting code)
sPattern = searchPattern/filter
folder1 = sourceFiles

And then the variable you use to loop ... foreach(var file in sourceFiles)

You may think it is nitpicking but naming your variables better will avoid things like File.Exists(folder2)

Rather use Path.Combine() instead of string + string



If i was your boss I'd slap you and make you wash my car over the weekend ;)

You sound like my senior. Lol gives some really good advice and this is definitely one for them. +1
 
I take it you are a junior. So:

Don't call your variables filepath and folder2. Rather called it sourceFolder and destinationFolder (self documenting code)
sPattern = searchPattern/filter
folder1 = sourceFiles

And then the variable you use to loop ... foreach(var file in sourceFiles)

You may think it is nitpicking but naming your variables better will avoid things like File.Exists(folder2)

Rather use Path.Combine() instead of string + string



If i was your boss I'd slap you and make you wash my car over the weekend ;)

So do you use var for every simple piece of code?
 
So do you use var for every simple piece of code?

Code:
var ignorePersonStirringKak = true;
var kakStirrer = new Person("semaphore");
if(ignorePersonStirringKak)
   KakUitsorteerder.Ignore(kakStirrer);

In other words - yes
 
Top
Sign up to the MyBroadband newsletter
X