Program.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Batch Rename v1.0.1
  2. // Made by mishaor with thanks to SharpDevelop
  3. using System;
  4. using System.IO;
  5. namespace BatchRename
  6. {
  7. class Program
  8. {
  9. public static int Main(string[] args)
  10. {
  11. if(args.Length == 0)
  12. {
  13. // If no arguments, we printing help message and exitting with return code 0
  14. Console.WriteLine("Batch Rename v1");
  15. Console.WriteLine("Made with love by mishaor (Michael Orishich)");
  16. Console.WriteLine("Usage: BatchRename [folder path] [what to replace] [with what replace]");
  17. Console.WriteLine("Folder path can be:");
  18. Console.WriteLine("- folder path");
  19. Console.WriteLine("- .\\, which means current directory");
  20. return 0;
  21. }
  22. else
  23. {
  24. string[] fileList = null; // we creating array with null because we can't get variable from try
  25. // we writing information about program and file path
  26. Console.WriteLine("Batch Rename v1");
  27. Console.WriteLine("Made with love by mishaor (Michael Orishich)");
  28. Console.WriteLine("Folder Path: {0}", args[0]);
  29. // we getting file list from specified directory
  30. try
  31. {
  32. fileList = Directory.GetFiles(args[0]); // we getting file list from directory in arguments
  33. }
  34. catch(Exception error)
  35. {
  36. Console.WriteLine("Error when getting file list of {0}: {1}", args[0], error.Message); // if error happened, we printing message
  37. Environment.Exit(-1); // ...aaaand exiting program with code -1
  38. }
  39. foreach(string filePath in fileList) // for each file in file list we executing this code
  40. {
  41. string fileName = Path.GetFileName(filePath); // we getting file name from file list
  42. if(fileName.Contains(args[1])) // if name contains piece to replace
  43. {
  44. // ...then we replacing it!
  45. try
  46. {
  47. File.Move(filePath, filePath.Replace(args[1], args[2])); // we renaming file from original to original with needed replacement (sorry for my english)
  48. }
  49. catch(Exception error)
  50. {
  51. Console.WriteLine("Error when renaming file {0}: {1}", fileName, error.Message); // if error happened, we printing messsage
  52. break; // ...and ignore it by doing break which get us back
  53. }
  54. Console.WriteLine("Successfully renamed {0} with {1}", fileName, fileName.Replace(args[1], args[2])); // if we renamed sucessfully, we printing message
  55. }
  56. }
  57. return 0; // we returning 0 if all is OK
  58. }
  59. }
  60. }
  61. }