Condensing repetitive if statements <p>I have the following code, which scans a directory and puts files containing "a" within its filename to a new folder A. Similarly, it puts files with "b" within its filename to a new folder called B. Since the if statements are basically the same, with the only thing that changes being the letter "a" or "b" and being sent to either destA or destb (desitinations), how can I trim this code down? I know there is a better way because much of the code is repeated... Thanks.</p> <pre><code>static void Main() { string path = @"C:\Users\me\Desktop\FOLDER"; string destA = @"C:\Users\me\Desktop\FOLDER\A"; string destB = @"C:\Users\me\Desktop\FOLDER\B"; DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] filesxx = dir.GetFiles(); foreach (FileInfo filexx in filesxx) { if (filexx.Name.Contains("a")) { if (!Directory.Exists(destA)) Directory.CreateDirectory(destA); Console.WriteLine(filexx); filexx.CopyTo(Path.Combine(destA, filexx.Name), true); } else if (filexx.Name.Contains("b")) { if (!Directory.Exists(destB)) Directory.CreateDirectory(destB); Console.WriteLine(filexx); filexx.CopyTo(Path.Combine(destB, filexx.Name), true); } else { Console.WriteLine("Other: ", filexx); } } Console.Read(); } </code></pre>