Advertisement
jyoung12387

File Renaming App v1.0

Feb 24th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.56 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4.  
  5. namespace FebTwoTwo
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //Ask user for path. Verify that path exists.
  12.             bool invalidPath;
  13.             string rootPath;
  14.             do
  15.             {
  16.                 Console.Write("Please enter the location of the files you would like to rename: ");
  17.                 rootPath = Console.ReadLine();
  18.                 invalidPath = false;
  19.  
  20.                 if ( !(Directory.Exists(rootPath)) )
  21.                 {
  22.                     invalidPath = true;
  23.                     Console.Clear();
  24.                     Console.WriteLine("You entered {0}. This file path does not exist.", rootPath);
  25.                 }
  26.             }
  27.             while (invalidPath);
  28.  
  29.             //define DirectoryInfo and FileInfo
  30.             DirectoryInfo info = new DirectoryInfo(rootPath);
  31.             FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
  32.  
  33.             //inform user of which files are at the specified location
  34.             Console.WriteLine("You entered \"{0}\". Flies at current location:", rootPath);
  35.             foreach (FileInfo file in files)
  36.             {
  37.                 Console.WriteLine(file);
  38.             }
  39.  
  40.             //Get file name to batch rename, make sure file name is valid.
  41.             bool fileNameError;
  42.             string batchFileName;
  43.             do
  44.             {
  45.                 Console.WriteLine("Please enter name you would like to rename files to:");
  46.                 batchFileName = Console.ReadLine();
  47.                 fileNameError = false;
  48.                 if (ContainsInvalidChars(batchFileName))  // invalid chars are \/:*?"<>|
  49.                 {
  50.                     fileNameError = true;
  51.                     Console.Clear();
  52.                     Console.WriteLine("The file name {0} contains an invalid character. \nPlease use another name.", batchFileName);
  53.                 }
  54.             }
  55.             while (fileNameError);
  56.  
  57.             //verify user would like to continue, get input whether or not to continue.
  58.             Console.WriteLine("You entered \"{0}\". Would you like to continue? (Type 'Y' or 'N')", batchFileName);
  59.             string proceedInput = Console.ReadLine();
  60.             bool charParseSuccess = char.TryParse(proceedInput, out char proceedAsChar);
  61.  
  62.             //if user types 'Y' then rename files, else exit program
  63.             if ( (charParseSuccess && proceedAsChar == 'Y') || (charParseSuccess && proceedAsChar == 'y'))
  64.             {
  65.                 //Change file names
  66.                 int counter = 1;
  67.                 foreach (FileInfo file in files)
  68.                 {
  69.                     string name = file.Name;            //file name with extension, without path
  70.                     string extension = file.Extension;  //extension
  71.                     string fullName = file.FullName;    //full file path including extension
  72.                                                         //Console.WriteLine(file.Name);
  73.  
  74.                     File.Move(file.FullName, file.FullName.Replace(file.Name, batchFileName + counter.ToString() + file.Extension));
  75.  
  76.                     //Console.WriteLine(oldDate.ToString());
  77.                     counter++;
  78.  
  79.                     //File.Move takes the path of the original file (file.FullName) and moves it to the destination path (File.FullName.Replace)
  80.                     //Replace takes the old string (original path) and replaces it with a new path
  81.                     //Example it changes C:\temp\picture.jpeg to C:\temp\MyFile1.jpeg
  82.                 }
  83.                 Console.WriteLine("{0} Files Renamed", counter - 1);
  84.             }
  85.              else
  86.             {
  87.                 Console.WriteLine("Rename Cancelled. Press any key to exit");
  88.             }
  89.             Console.Read();
  90.         }
  91.         public static bool ContainsInvalidChars(string userInput) // Method to check if file contains any invalid chars
  92.         {
  93.             if ((userInput.Contains('/')) ||
  94.                  (userInput.Contains('\\')) ||
  95.                  (userInput.Contains(':')) ||
  96.                  (userInput.Contains('*')) ||
  97.                  (userInput.Contains('?')) ||
  98.                  (userInput.Contains('"')) ||
  99.                  (userInput.Contains('<')) ||
  100.                  (userInput.Contains('>')) ||
  101.                  (userInput.Contains('|'))
  102.                  )
  103.             {
  104.                 return true;
  105.             }
  106.             else
  107.             {
  108.                 return false;
  109.             }
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement