I need to send a file from one folder to the next by date,

Answered I need to send a file from one folder to the next by date,

  • Wednesday, March 07, 2012 6:31 AM
     
     

    I am a newb :) Anyways thanks in advance. I am trying to figure this out I am sorting through text files in folder 1 in which it seems to work fine. But when I add this line of code in

    File.Move(fileNames, filetransfered); It errors out giving me "The best overloaded method match for 'System.IO.File.Move(string, string)' has some invalid arguments "

    From what I read the file.move( , ); supposed to move the file from folder 1 into the second folder

    Here is the code. Thank you for any help.

        class Program
        {
            static void Main(string[] args)
            {

                string[] fileNames = Directory.GetFiles(@"c:\Users\Rob\Desktop\1", "*.txt");
                string filetransfered = @"c:\Users\Rob\Desktop\2";

                // Now read the creation time for each file
                DateTime[] creationTimes = new DateTime[fileNames.Length];
                for (int i = 0; i < fileNames.Length; i++)
                    creationTimes[i] = new FileInfo(fileNames[i]).CreationTime;

                // sort it
                Array.Sort(creationTimes, fileNames);

                File.Move(fileNames, filetransfered);






    • Edited by blehugh Wednesday, March 07, 2012 6:46 AM
    •  

All Replies

  • Wednesday, March 07, 2012 7:05 AM
     
     Proposed Has Code

    HI Blehugh,

             The syntax of  File.move is 

    file.move(path, destinationPath);

    your string "filetransfered" is correct but in "fileNames" u r using array instead of a single string path.
    ur code should like this:

    class Program
        {
            static void Main(string[] args)
            {
    
                string fileName = @"c:\Users\Rob\Desktop\1";
                string filetransfered = @"c:\Users\Rob\Desktop\2";
    
                // Now read the creation time for each file
                DateTime[] creationTimes = new DateTime[fileNames.Length];
                for (int i = 0; i < fileNames.Length; i++)
                    creationTimes[i] = new FileInfo(fileNames[i]).CreationTime;
    
                // sort it
                Array.Sort(creationTimes, fileNames);
    
                File.Move(fileNames, filetransfered);
    

    remember  File.move required two arguments one for source file path and second for destination file path  "file.move(sourceFilePath , destinationFilePath)"

    if u can't understand anything.....plz reply.... i will try to help u
    regards,

    I3S


    • Proposed As Answer by I3S Wednesday, March 07, 2012 7:05 AM
    • Edited by I3S Wednesday, March 07, 2012 7:06 AM
    •  
  • Wednesday, March 07, 2012 7:07 AM
     
     Proposed

    using System;
    using System.IO;

    namespace MoveFiles
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] fileNames = Directory.GetFiles(@"C:\Users\Rob\Desktop\1", "*.txt");
                string filetransfered = @"C:\Users\Rob\Desktop\2";

                // Now read the creation time for each file
                DateTime[] creationTimes = new DateTime[fileNames.Length];
                for (int i = 0; i < fileNames.Length; i++)
                    creationTimes[i] = new FileInfo(fileNames[i]).CreationTime;

                // sort it
                Array.Sort(creationTimes, fileNames);

                if (false == Directory.Exists(filetransfered))
                {
                    Directory.CreateDirectory(filetransfered);
                }
                //File.Move(fileNames, filetransfered);
                foreach (string filename in fileNames)
                {
                    File.Move(filename, String.Format(@"{0}\{1}", filetransfered, Path.GetFileName(filename)));
                }
            }
        }
    }
    • Proposed As Answer by Sam Vishwas Wednesday, March 07, 2012 7:07 AM
    • Edited by Sam Vishwas Wednesday, March 07, 2012 7:47 AM Provided complete code listing.
    •  
  • Wednesday, March 07, 2012 1:47 PM
     
     

    Ya I figured it was the single string part but I am still erroring on lines   new FileInfo(fileName[i])  and  Array.Sort(creationTimes, fileName);

      The best overloaded method match for 'System.Array.Sort<System.DateTime>(System.DateTime[], System.Comparison<System.DateTime>)' has some invalid arguments   

    using System;
    using System.IO;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {

                 string fileName = @"c:\Users\RStamper\Desktop\A";
                string filetransfered = @"c:\Users\RStamper\Desktop\B";

                // Now read the creation time for each file
                DateTime[] creationTimes = new DateTime[fileName.Length];
                for (int i = 0; i < fileName.Length; i++)
                    creationTimes[i] = new FileInfo(fileName[i]).CreationTime;

                // sort it
                Array.Sort(creationTimes, fileName);

                File.Move(fileName, filetransfered);
           }
       }
    }

  • Wednesday, March 07, 2012 2:26 PM
     
     
    Dear Sam this works but its transferring all the files not the last one last added. I think it has something to do with the loop around moves.
    • Edited by blehugh Wednesday, March 07, 2012 2:38 PM
    •  
  • Wednesday, March 07, 2012 7:21 PM
     
     Answered

    using System;
    using System.IO;

    namespace MoveFiles
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] fileNames = Directory.GetFiles(@"C:\Users\Rob\Desktop\1", "*.txt");
                string filetransfered = @"C:\Users\Rob\Desktop\2";

                // Now read the creation time for each file
                DateTime[] creationTimes = new DateTime[fileNames.Length];
                for (int i = 0; i < fileNames.Length; i++)
                    creationTimes[i] = new FileInfo(fileNames[i]).CreationTime;

                // sort it
                Array.Sort(creationTimes, fileNames);

                if (false == Directory.Exists(filetransfered))
                {
                    Directory.CreateDirectory(filetransfered);
                }
                //File.Move(fileNames, filetransfered);
                foreach (string filename in fileNames)
                {
                    File.Move(filename, String.Format(@"{0}\{1}", filetransfered, Path.GetFileName(filename)));
                }
            }
        }
    }

    You almost had it Sam, thanks a ton I got it figured out now took about 4 hours but oh well :) Thanks again.... here it is if anyone needs it. It will transfer a file by date from one folder to the next. Very cool.. Thanks again

    using System;
    using System.IO;

    namespace ConsoleApplication1
    {
        class Program
        {
           // public static void transferFiles()
            public static void Main()
            {
                string[] fileNames = Directory.GetFiles(@"C:\Users\RStamper\Desktop\A", "*.xml");
                string filetransfered = @"C:\Users\RStamper\Desktop\B";

                DateTime[] creationTimes = new DateTime[fileNames.Length];
                for (int i = 0; i < fileNames.Length; i++)
                    creationTimes[i] = new FileInfo(fileNames[i]).CreationTime;

                   Array.Sort(creationTimes, fileNames);

                if (false == Directory.Exists(filetransfered))
                {
                  Directory.CreateDirectory(filetransfered);
                }

                  String filename = fileNames[fileNames.Length - 1]; //gets the last file
               if (!File.Exists(filetransfered))
                {   
                    File.Move(filename, String.Format(@"{0}\{1}", filetransfered, Path.GetFileName(filename)));
                    File.Delete(filename);              
               }

            }
        }
    }

    • Marked As Answer by blehugh Wednesday, March 07, 2012 7:21 PM
    •