Hi I have a folder containing about 60 files and I need to copy each 3 files to a separate folder

Thank you

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

Also, do you need this to be explained in loops or do you need it "done"?

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

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;
         }
      }
   }
}

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

Sorted alphabetically or three specific names?

yes i want it to be sorted alphabetically

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))));

thank you man this helped me alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.