Do you mean you are going to have 20 folders?
...and is this a copy or a MOVE?

Edited 4 Years Ago by thines01

hi i need that every time i start my program it moves the first 3files in a specific folder to another folder
Thank you so much for your reply

Edited 4 Years Ago by shandoosheri

How do you recognize the files as the first three files?:
- by name
- by timestamp
- something else

Here is an example using just the first 3 files encountered:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace DW_420841_CS_CON
{
   class CGroupFilesAcrossDirectories
   {
      private static string _strFileLoc = @"c:\science\DaniWeb\DW_420841_CS_CON\DATA\Files\";
      private static string _strDirLoc = @"c:\science\DaniWeb\DW_420841_CS_CON\DATA\Directories\";

      private static bool MoveFirstThreeFiles(ref string strError)
      {
         bool blnRetVal = true;
         //
         try
         {
            Directory.GetFiles(_strFileLoc).Take(3).ToList()
               .ForEach(strFile =>
                  File.Move(strFile, Path.Combine(_strDirLoc, Path.GetFileName(strFile))));
         }
         catch (Exception exc)
         {
            blnRetVal = false;
            strError = exc.Message;
         }
         //
         return blnRetVal;
      }

      static void Main(string[] args)
      {
         string strError = "";
         if (!MoveFirstThreeFiles(ref strError))
         {
            System.Diagnostics.Debug.WriteLine("Could not move: " + strError);
            return;
         }
      }
   }
}

Edited 4 Years Ago by thines01

hi thank you so much for your reply . I want to recognize the first 3 files by their name

hi thank you for your fast reply and i need to recognize the first 3 files by their name

OK then just change the GetFiles line to this:

Directory.GetFiles(_strFileLoc).OrderBy(s => s).Take(3).ToList()
           .ForEach(strFile =>
              File.Move(strFile, Path.Combine(_strDirLoc, Path.GetFileName(strFile))));
This question has already been answered. Start a new discussion instead.