0

I want to move from the folder named Main a file and it should go to Folder 1 every 15 seconds and from Main to Folder 2 every 60 seconds.

The problem is that folders 1 and 2 will conflict on 60 seconds tick. For Folder 1 it would be 4th time ticking and taking files from Folder Main and for Folder 2 it will be the first time trying to take files from Main.

Can you advise me on how to block this 4th tick in Folder 1 and allow Folder 2 to take files from folder Main or prioritize it?

public partial class Form1 : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        Timer timer1 = new Timer();
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Interval = 60000; // 60 second
        timer1.Start();
        Timer timer2 = new Timer();
        timer2.Tick += new EventHandler(timer2_Tick);
        timer2.Interval = 15000; // 15 second
        timer2.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        // started code to move the files
        string rootFolderPath = @"C:\Users\Msi\OneDrive\Рабочий стол\Main";
        string destinationPath = @"C:\Users\Msi\OneDrive\Рабочий стол\Direction 1";
        string filesToMove = @"*.*";
        string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToMove);
        foreach (string file in fileList)
        {
            string filename = Path.GetFileName(file);
            File.Move(file, destinationPath + "\\" + filename);
            // place this code where you are actually moving the file
        }
    }

    private void timer2_Tick(object sender, EventArgs e)
    {
        // started code to move the files
        string rootFolderPath = @"C:\Users\Msi\OneDrive\Рабочий стол\Main";
        string destinationPath = @"C:\Users\Msi\OneDrive\Рабочий стол\Direction 2";
        string filesToMove = @"*.*";
        string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToMove);
        
        foreach (string file in fileList)
        {
            string filename = Path.GetFileName(file);
            File.Move(file, destinationPath + "\\" + filename);
            // place this code where you are actually moving the file
        }
    }
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501

1 Answers1

0

"Can you advise me on how to block this 4th tick in Folder 1 and allow Folder 2 to take files from folder Main"

A very basic solution would be to keep a counter of the number of ticks, and ignore the fourth one each time:

private int timer2Iterations = 0;

private void timer2_Tick(object sender, EventArgs e)
{
    // Increment our counter
    timer2Iterations++;

    // If this is the fourth iteration, reset our 
    // counter and return without doing anything
    if (timer2Iterations == 4)
    {
        timer2Iterations = 0;
        return;
    }

    // started code to move the files
    string rootFolderPath = @"C:\Users\Msi\OneDrive\Рабочий стол\Main";
    string destinationPath = @"C:\Users\Msi\OneDrive\Рабочий стол\Direction 2";
    string filesToMove = @"*.*";
    string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToMove);
    
    foreach (string file in fileList)
    {
        string filename = Path.GetFileName(file);
        File.Move(file, destinationPath + "\\" + filename);
        // place this code where you are actually moving the file
    }
}

However, if this is really the solution, then you probably only need one timer, and just change the destination directory on every fourth iteration:

private Timer timer = null;

private void button1_Click(object sender, EventArgs e)
{
    if (timer == null)  // First time clicking we setup the timer
    {
        Timer timer = new Timer();
        timer.Tick += new EventHandler(timer_Tick);
        timer.Interval = TimeSpan.FromSeconds(15).TotalMilliseconds;
        timer.Start();
        button1.Text = "Stop timer";
    }
    else // Subsequent clicks we just start/stop the timer
    {
        timer.Enabled = !timer.Enabled;
        button1.Text = timer.Enabled ? "Stop timer" : "Start timer";
    }
}

private int timerIterations = 0;

private void timer_Tick(object sender, EventArgs e)
{
    string root = @"C:\Users\Msi\OneDrive\Рабочий стол";
    string source = Path.Combine(root, "Main");
    string destination = Path.Combine(root, "Direction 1");
    string filesToMove = @"*.*";   
    string[] fileList = Directory.GetFiles(source, filesToMove);

    // Increment our counter
    timerIterations++;

    // If this is the fourth iteration, reset our counter 
    // and change the destination to folder 2
    if (timerIterations == 4)
    {
        timerIterations = 0;
        destination = Path.Combine(root, "Direction 2");
    }

    foreach (string file in fileList)
    {
        string filename = Path.GetFileName(file);
        File.Move(file, Path.Combine(destination, filename));
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43