Ad Code

Responsive Advertisement

Console App Task 02: Folders having many files, get only file contains name "TS" & "ECU" in C#

Console App Task 02: Folders having many files, get only file contains name "TS" & "ECU" in C# 

Here is an enhanced C# Console Application that transfers only files containing "TS" or "ECU" in their name from folders created in the last 30 days.


C# Console Application - Selective File Transfer

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string sourcePath = @"C:\SourceFolder"; // Change this
        string destinationPath = @"D:\DestinationFolder"; // Change this

        try
        {
            if (!Directory.Exists(sourcePath))
            {
                Console.WriteLine("Source directory does not exist!");
                return;
            }

            if (!Directory.Exists(destinationPath))
            {
                Directory.CreateDirectory(destinationPath);
            }

            DateTime oneMonthAgo = DateTime.Now.AddMonths(-1); // 30 days ago

            string[] folders = Directory.GetDirectories(sourcePath);

            foreach (string folder in folders)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(folder);

                if (dirInfo.CreationTime >= oneMonthAgo)  // Only process folders created in last 30 days
                {
                    string[] files = Directory.GetFiles(folder);

                    foreach (string file in files)
                    {
                        string fileName = Path.GetFileName(file);

                        if (fileName.Contains("TS") || fileName.Contains("ECU"))  // Filter files
                        {
                            string targetFolder = Path.Combine(destinationPath, dirInfo.Name);

                            if (!Directory.Exists(targetFolder))
                            {
                                Directory.CreateDirectory(targetFolder);
                            }

                            string destinationFile = Path.Combine(targetFolder, fileName);
                            File.Move(file, destinationFile);

                            Console.WriteLine($"Moved: {fileName} from {dirInfo.Name}");
                        }
                    }
                }
            }

            Console.WriteLine("Filtered file transfer completed successfully!");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }
}

How It Works:

  1. Gets all folders in sourcePath.
  2. Checks if the folder was created in the last 30 days.
  3. Looks for files that contain "TS" or "ECU" in their name.
  4. Moves those files to destinationPath, maintaining folder structure.
  5. If a folder does not exist in the destination, it creates it before moving files.

Example File Transfer

Before Running the Program:

C:\SourceFolder
 ├── FolderA (Created: Today)
 │    ├── data1.txt
 │    ├── ECU_Report.pdf  ✅
 │    ├── TS_Config.xml   ✅
 │
 ├── FolderB (Created: 15 days ago)
 │    ├── TS_Log.txt      ✅
 │    ├── random.docx
 │
 ├── FolderC (Created: 45 days ago)
      ├── ECU_Error.log
      ├── TS_Info.docx

After Running the Program:

D:\DestinationFolder
 ├── FolderA
 │    ├── ECU_Report.pdf
 │    ├── TS_Config.xml
 │
 ├── FolderB
 │    ├── TS_Log.txt

Only files containing "TS" or "ECU" were moved.
FolderC was skipped (older than 30 days).


Customization Options

  • Modify filters: Change "TS" and "ECU" in the condition:
    if (fileName.Contains("TS") || fileName.Contains("ECU"))
    
  • Adjust time range: Change:
    DateTime oneMonthAgo = DateTime.Now.AddMonths(-1);
    
    • Use .AddDays(-10) for last 10 days.

Programs relates:

✅ Filters files that contain "TS" or "ECU" in their name.
✅ Moves only filtered files, keeping the folder structure.
✅ Ensures folders are created in the destination before moving files.


If you like comment and share. 🚀

Post a Comment

0 Comments

Ad Code

Responsive Advertisement