Move & Rename Files, Auto

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Axelsson
    New Member
    • Jul 2010
    • 2

    Move & Rename Files, Auto

    Hi all!

    I've just created the following program:

    Code:
    using System;
    
    using System.IO;
    
    
    
    namespace FileRename
    {
    
        class MyClass
        {
    
            static void Main(string[] args)
            {
    
                try
                {
    
                    DirectoryInfo mydir = new DirectoryInfo(@"C:\Users\alexander.axelsson\Documents\Affärssystemet\Scannade filer");
                    FileInfo[] a = mydir.GetFiles("*.txt");
    
    
                    int i = 0;
    
                    string filepath;
    
    
                    foreach (FileInfo fri in a)
                    {
    
    
                        filepath = @"C:\Users\alexander.axelsson\Documents\Scanner\test" + "pic" + i.ToString("D3") + ".txt";
    
                        fri.MoveTo(filepath);
    
                        i++;
    
                    }
    
    
                    Console.WriteLine("Done");
    
                }
    
    
                catch (Exception e)
                {
    
                    Console.WriteLine("The process failed: {0}", e.ToString());
    
                }
    
            }
    
        }
    
    }
    My problem now is that I want to have this program running all the time and when a file is created in the directory I want the program to automatically copy and rename the file.

    Right now I can just rename and move the files one time, when the directory have files in it, I cant move the files there any more. I have no idea why, but maybe you guys can help me with this.

    Thanks!
  • maddyromeo
    New Member
    • Jul 2010
    • 22

    #2
    problem in your code is may be due to "i=0"
    because when you run the code , "filepath" which depends on "i" gets the same name when u run the code any time,, so this causes error (files with same names cannot be copied)

    so my suggestion is try this:




    try
    {

    DirectoryInfo mydir = new DirectoryInfo(" destinationdire ctory");
    FileInfo[] a = mydir.GetFiles( "*.txt");
    int i = a.Length+1; //count number of files in destination directory

    mydir = new DirectoryInfo(@ "C:\Users\alexa nder.axelsson\D ocuments\Affärs systemet\Scanna de filer");
    a = mydir.GetFiles( "*.txt");
    string filepath;
    foreach (FileInfo fri in a)
    {
    filepath = @"C:\Users\alex ander.axelsson\ Documents\Scann er\test" + "pic" + i.ToString("D3" ) + ".txt";

    fri.MoveTo(file path);

    i++;

    }


    Console.WriteLi ne("Done");

    }

    Comment

    • maddyromeo
      New Member
      • Jul 2010
      • 22

      #3
      problem in your code is may be due to "i=0"
      because when you run the code , "filepath" which depends on "i" gets the same name when u run the code any time,, so this causes error (files with same names cannot be copied)

      so my suggestion is try this:




      try
      {

      DirectoryInfo mydir = new DirectoryInfo(@ "C:\Users\alexa nder.axelsson\D ocuments\Scanne r");
      FileInfo[] a = mydir.GetFiles( "*.txt");
      int i = a.Length+1; //count number of files in destination directory

      mydir = new DirectoryInfo(@ "C:\Users\alexa nder.axelsson\D ocuments\Affärs systemet\Scanna de filer");
      a = mydir.GetFiles( "*.txt");
      string filepath;
      foreach (FileInfo fri in a)
      {
      filepath = @"C:\Users\alex ander.axelsson\ Documents\Scann er\test" + "pic" + i.ToString("D3" ) + ".txt";

      fri.MoveTo(file path);

      i++;

      }


      Console.WriteLi ne("Done");

      }

      Comment

      • maddyromeo
        New Member
        • Jul 2010
        • 22

        #4
        to have your code runnig all the time you have to implement it as a windows servies

        or

        you can add your .exe to windows scheduled tasks
        (Start>AllProgr ams>Accessories >System Tools>Schedules tasks) which runs for specific time period.

        Comment

        • Bassem
          Contributor
          • Dec 2008
          • 344

          #5
          Originally posted by maddyromeo
          to have your code runnig all the time you have to implement it as a windows servies

          or

          you can add your .exe to windows scheduled tasks
          (Start>AllProgr ams>Accessories >System Tools>Schedules tasks) which runs for specific time period.
          Beside the two brilliant ideas that maddyromeo wrote. You can add a timer to your application and run the application every while. Be careful in all cases that if your directory (folder) contains many files and the process takes much time on the CPU then you will have to make it wait for the other processes. Here is a good link.

          Comment

          • Alexander Axelsson
            New Member
            • Jul 2010
            • 2

            #6
            @maddyromeo

            Thank you, that did the trick

            @Bassem

            I want to have that timer to run like every 15 min, because then it wont be so many files. But where do I put the code? Have never worked with a timer before. I want to learn so i'm just asking :)

            Comment

            • maddyromeo
              New Member
              • Jul 2010
              • 22

              #7
              to use timer

              drag and drop 'timer' from toolBox on to your form

              change the "timer's interval property" in the Properties pane (1sec = 1000millisec) to your time (say 15min)

              Double-Click on 'timer' control and you get 'timer_tick' method.just paste your code in the method....

              Comment

              Working...