2
Contributors
10
Replies
11
Views
4 Years
Discussion Span
Last Post by shandoosheri
Featured Replies
  • 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 = … Read More

  • 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)))); Read More

0

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

Edited by thines01

0

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 by shandoosheri

1

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 by thines01

0

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

0

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

1

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.