Advertisement
Guest User

Untitled

a guest
Mar 28th, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5. namespace project1
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.  
  12.  
  13.             if (args.Length != 0)
  14.             {
  15.                 Console.WriteLine("argument is not null");
  16.                 int argvalue;
  17.  
  18.                 if (int.TryParse(args[0], out argvalue))
  19.                 {
  20.                     // argument is an integer
  21.                     int keepFiles = Int32.Parse(args[0]);
  22.                     Console.WriteLine("arg is an int: " + keepFiles);
  23.  
  24.                     // get current directory
  25.                     string curPath = Directory.GetCurrentDirectory();
  26.                     // Console.Write("Current Path: " + curPath + "\n");
  27.  
  28.                     // save array of all folders in current directory
  29.                     string[] folderArray = System.IO.Directory.GetDirectories(curPath, "*", System.IO.SearchOption.AllDirectories);
  30.  
  31.                     //loop over each folder
  32.                     foreach (var curfolder in folderArray)
  33.                     {
  34.  
  35.                         // get files array from current folder
  36.                         DirectoryInfo di = new DirectoryInfo(curfolder);
  37.                         FileSystemInfo[] files = di.GetFileSystemInfos("*.rpp-bak");
  38.                         var orderedFiles = files.OrderByDescending(f => f.LastWriteTime);
  39.  
  40.                         // loop over ordered files in current folder
  41.                         int count = 1;
  42.                         foreach (var curfile in orderedFiles)
  43.                         {
  44.                             DateTime lastModified = System.IO.File.GetLastWriteTime(curfile.ToString());
  45.  
  46.                             if (count > keepFiles)
  47.                             {
  48.                                 string FileToDelete = System.IO.Path.Combine(curfolder.ToString(), curfile.ToString());
  49.  
  50.                                 // delete files above keepFiles count
  51.                                 Console.WriteLine(curfolder.ToString());
  52.                                 System.IO.File.Delete(FileToDelete);
  53.                                 Console.WriteLine("Deleted: " + FileToDelete);
  54.                                 Console.WriteLine("    " + lastModified.ToString());
  55.                             }
  56.                             else
  57.                             {
  58.                                 // do nothing to files
  59.                                 Console.WriteLine("Not Deleted: " + curfile.ToString());
  60.                                 Console.WriteLine("    " + lastModified.ToString());
  61.                             }
  62.  
  63.                             count++;
  64.                         }
  65.                     }
  66.  
  67.                 }
  68.                 else
  69.                 {
  70.                     // argument is not an integer
  71.                     string argument = args[0];
  72.                     Console.WriteLine("argument is NOT an int: " + argument);
  73.                     Console.WriteLine("Delete_Rpp_Bak.exe is a utility that deletes \n older backup files and keeps x number of files.");
  74.                     Console.WriteLine("");
  75.                     Console.WriteLine("Usage: 'Delete_Rpp_Bak x' where x is the \n number of most recent *.rpp-bak files to keep");
  76.                     Console.WriteLine("Must include only 1 integer argument for number \n of *.rpp-bak files to keep in each directory.");
  77.                     Console.WriteLine("");
  78.                     Console.WriteLine("Run 'Delete_Rpp_Bak.exe' in the directory above \n the directories you wish to clean");
  79.                 }
  80.  
  81.             }
  82.             else
  83.             {
  84.                 Console.WriteLine("argument is null");
  85.                 Console.WriteLine("Delete_Rpp_Bak.exe is a utility that deletes \n older backup files and keeps x number of files.");
  86.                 Console.WriteLine("");
  87.                 Console.WriteLine("Usage: 'Delete_Rpp_Bak x' where x is the \n number of most recent *.rpp-bak files to keep");
  88.                 Console.WriteLine("Must include only 1 integer argument for number \n of *.rpp-bak files to keep in each directory.");
  89.                 Console.WriteLine("");
  90.                 Console.WriteLine("Run 'Delete_Rpp_Bak.exe' in the directory above \n the directories you wish to clean");
  91.             }
  92.  
  93.  
  94.  
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement