mardi 16 août 2011

rename file and count number in folder c#

to count files
  public int count_file(String folder)
        {
            int fileCount = 0;
              fileCount = Directory.GetFiles(folder, "*.*", SearchOption.AllDirectories).Length;
            return fileCount;
        }


to rename files :

    static void rename(string args)
        {

            try
            {

                DirectoryInfo di = new DirectoryInfo(@"C:\Users\");

                // Get a reference to each file in that directory.

                FileInfo[] fiArr = di.GetFiles("*.jpg");

                // Define an Integer Counter

                int i = 0;

                string path;

                // Display the names of the files.

                foreach (FileInfo fri in fiArr)
                {

                    //Console.WriteLine(fri.Name);

                    //Change the prefix to something that is not already there

                    path = @"C:\Users\" + i.ToString() + ".jpg";

                    fri.MoveTo(path);

                    i++;

                }


                Console.WriteLine("Done");

            }


            catch (Exception e)
            {

                Console.WriteLine("The process failed: {0}", e.ToString());

            }
            Console.Read();

        }


Aucun commentaire:

Enregistrer un commentaire

Nombre total de pages vues