Advertisement
jyoung12387

Basic File Renaming App

Feb 22nd, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 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.         static void Main(string[] args)
  13.         {
  14.             //set path, get files and order by file creation time
  15.             string rootPath = @"C:\temp";
  16.             DirectoryInfo info = new DirectoryInfo(rootPath);
  17.             FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
  18.  
  19.             //Change file names
  20.             int counter = 1;
  21.             foreach (FileInfo file in files)
  22.             {
  23.                 string formatName = "My Files";
  24.                 string name = file.Name;
  25.                 string extension = file.Extension;
  26.                 string fullName = file.FullName;
  27.  
  28.                 File.Move(file.FullName, file.FullName.Replace(file.Name, formatName + counter.ToString() + file.Extension));
  29.                 counter++;
  30.  
  31.                 //File.Move takes the path of the original file (file.FullName) and moves it to the destination path (File.FullName.Replace)
  32.                 //Replace takes the old string (original path) and replaces it with a new path
  33.                 //Example it changes C:\temp\picture.jpeg to C:\temp\MyFile1.jpeg
  34.             }
  35.             Console.WriteLine("Files Renamed");
  36.             Console.Read();
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement