Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.IO;
- namespace FebTwoTwo
- {
- class Program
- {
- static void Main(string[] args)
- {
- //Ask user for path. Verify that path exists.
- bool invalidPath;
- string rootPath;
- do
- {
- Console.Write("Please enter the location of the files you would like to rename: ");
- rootPath = Console.ReadLine();
- invalidPath = false;
- if ( !(Directory.Exists(rootPath)) )
- {
- invalidPath = true;
- Console.Clear();
- Console.WriteLine("You entered {0}. This file path does not exist.", rootPath);
- }
- }
- while (invalidPath);
- //define DirectoryInfo and FileInfo
- DirectoryInfo info = new DirectoryInfo(rootPath);
- FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
- //inform user of which files are at the specified location
- Console.WriteLine("You entered \"{0}\". Flies at current location:", rootPath);
- foreach (FileInfo file in files)
- {
- Console.WriteLine(file);
- }
- //Get file name to batch rename, make sure file name is valid.
- bool fileNameError;
- string batchFileName;
- do
- {
- Console.WriteLine("Please enter name you would like to rename files to:");
- batchFileName = Console.ReadLine();
- fileNameError = false;
- if (ContainsInvalidChars(batchFileName)) // invalid chars are \/:*?"<>|
- {
- fileNameError = true;
- Console.Clear();
- Console.WriteLine("The file name {0} contains an invalid character. \nPlease use another name.", batchFileName);
- }
- }
- while (fileNameError);
- //verify user would like to continue, get input whether or not to continue.
- Console.WriteLine("You entered \"{0}\". Would you like to continue? (Type 'Y' or 'N')", batchFileName);
- string proceedInput = Console.ReadLine();
- bool charParseSuccess = char.TryParse(proceedInput, out char proceedAsChar);
- //if user types 'Y' then rename files, else exit program
- if ( (charParseSuccess && proceedAsChar == 'Y') || (charParseSuccess && proceedAsChar == 'y'))
- {
- //Change file names
- int counter = 1;
- foreach (FileInfo file in files)
- {
- string name = file.Name; //file name with extension, without path
- string extension = file.Extension; //extension
- string fullName = file.FullName; //full file path including extension
- //Console.WriteLine(file.Name);
- File.Move(file.FullName, file.FullName.Replace(file.Name, batchFileName + counter.ToString() + file.Extension));
- //Console.WriteLine(oldDate.ToString());
- counter++;
- //File.Move takes the path of the original file (file.FullName) and moves it to the destination path (File.FullName.Replace)
- //Replace takes the old string (original path) and replaces it with a new path
- //Example it changes C:\temp\picture.jpeg to C:\temp\MyFile1.jpeg
- }
- Console.WriteLine("{0} Files Renamed", counter - 1);
- }
- else
- {
- Console.WriteLine("Rename Cancelled. Press any key to exit");
- }
- Console.Read();
- }
- public static bool ContainsInvalidChars(string userInput) // Method to check if file contains any invalid chars
- {
- if ((userInput.Contains('/')) ||
- (userInput.Contains('\\')) ||
- (userInput.Contains(':')) ||
- (userInput.Contains('*')) ||
- (userInput.Contains('?')) ||
- (userInput.Contains('"')) ||
- (userInput.Contains('<')) ||
- (userInput.Contains('>')) ||
- (userInput.Contains('|'))
- )
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement