Advertisement
jyoung12387

If statement for listing files

Feb 24th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace FebTwoTwo
  9. {
  10.     class Program
  11.     {
  12.         //set path, get files and order by file creation time
  13.         public static string rootPath = @"C:\temp";
  14.         public static DirectoryInfo info = new DirectoryInfo(rootPath);
  15.         public static FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
  16.         static void Main(string[] args)
  17.         {
  18.             #region
  19.            
  20.            if(files.Count() <= 3)
  21.             {
  22.                 foreach (FileInfo file in files)
  23.                 {
  24.                     Console.WriteLine(file);
  25.                 }
  26.             }
  27.            else
  28.             {
  29.                 int i = 0;
  30.                 for (i = 0; i <2; i++)
  31.                 {
  32.                     Console.WriteLine(files[i]);
  33.                 }
  34.                 Console.WriteLine("Plus {0} more files", files.Length - i);
  35.             }
  36.            
  37.             #endregion
  38.             Console.Read();
  39.  
  40.             //Change file names
  41.             int counter = 1;
  42.             foreach (FileInfo file in files)
  43.             {
  44.                 string formatName = "My Files";
  45.                 string name = file.Name;
  46.                 string extension = file.Extension;
  47.                 string fullName = file.FullName;
  48.  
  49.                 File.Move(file.FullName, file.FullName.Replace(file.Name, formatName + counter.ToString() + file.Extension));
  50.                 counter++;
  51.  
  52.                 //File.Move takes the path of the original file (file.FullName) and moves it to the destination path (File.FullName.Replace)
  53.                 //Replace takes the old string (original path) and replaces it with a new path
  54.                 //Example it changes C:\temp\picture.jpeg to C:\temp\MyFile1.jpeg
  55.             }
  56.             Console.WriteLine("Files Renamed");
  57.             Console.Read();
  58.         }
  59.  
  60.  
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement