How to find a file based on a keyword in a folder, and move it to another folder with that keyword

Hi there, I am a UiPath newbie really spinning my wheels on my first creation and am wondering if anyone would be able to help get me moving in the right direction? Any thoughts are appreciated!

I have multiple documents folders with 100s of PDF files in each. I would like to go through each of the folders, and look for specific keywords in the file names, and move those specific files to different folders.

For instance, say in Folders 1, 2, and 3, I would like the files with “Aaa” and “Bbb” in the file names to be moved to different folders called “Aaa” and “Bbb”. So I might end up with 3 files with "Aaa’ in the file name in the Aaa folder. But, the full file name might have something more than just “Aaa” in it, and it might be different in each of Folders 1, 2, and 3.

How do I go about running through the different Folders looking for my needed files, and how do I say which files to look for without identifying the full file paths? Again, any thoughts are very much appreciated!

1 Like

Using Dotnet methods and string manipulations.

1 Like

Hope this could help.

Can be easily converted to UIPath.

 var allFolders = System.IO.Directory.GetDirectories(@"C:\Temp\RootFolder");  //Root Dir of Folder1, 2, 3
        var listOfKeywords = new List<string> { "key1", "key2" };  //Keywords
        var resultFolder = @"C:\Temp\ResultFolder";  //Search Result Folder

        foreach (var keyword in listOfKeywords)
            foreach (var folder in allFolders)
                var filteredFiles = System.IO.Directory.GetFiles(folder, keyword);

                foreach( var file in filteredFiles )
                    var keywordSpecificDir = System.IO.Directory.CreateDirectory(System.IO.Path.Combine(resultFolder, keyword)); //Create directory specific to the keyword
                    var destFileName = System.IO.Path.Combine(resultFolder, keyword, System.IO.Path.GetFileName(file));  //Get destination file name

                    System.IO.File.Move(file, destFileName);