Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- namespace project1
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length != 0)
- {
- Console.WriteLine("argument is not null");
- int argvalue;
- if (int.TryParse(args[0], out argvalue))
- {
- // argument is an integer
- int keepFiles = Int32.Parse(args[0]);
- Console.WriteLine("arg is an int: " + keepFiles);
- // get current directory
- string curPath = Directory.GetCurrentDirectory();
- // Console.Write("Current Path: " + curPath + "\n");
- // save array of all folders in current directory
- string[] folderArray = System.IO.Directory.GetDirectories(curPath, "*", System.IO.SearchOption.AllDirectories);
- //loop over each folder
- foreach (var curfolder in folderArray)
- {
- // get files array from current folder
- DirectoryInfo di = new DirectoryInfo(curfolder);
- FileSystemInfo[] files = di.GetFileSystemInfos("*.rpp-bak");
- var orderedFiles = files.OrderByDescending(f => f.LastWriteTime);
- // loop over ordered files in current folder
- int count = 1;
- foreach (var curfile in orderedFiles)
- {
- DateTime lastModified = System.IO.File.GetLastWriteTime(curfile.ToString());
- if (count > keepFiles)
- {
- string FileToDelete = System.IO.Path.Combine(curfolder.ToString(), curfile.ToString());
- // delete files above keepFiles count
- Console.WriteLine(curfolder.ToString());
- System.IO.File.Delete(FileToDelete);
- Console.WriteLine("Deleted: " + FileToDelete);
- Console.WriteLine(" " + lastModified.ToString());
- }
- else
- {
- // do nothing to files
- Console.WriteLine("Not Deleted: " + curfile.ToString());
- Console.WriteLine(" " + lastModified.ToString());
- }
- count++;
- }
- }
- }
- else
- {
- // argument is not an integer
- string argument = args[0];
- Console.WriteLine("argument is NOT an int: " + argument);
- Console.WriteLine("Delete_Rpp_Bak.exe is a utility that deletes \n older backup files and keeps x number of files.");
- Console.WriteLine("");
- Console.WriteLine("Usage: 'Delete_Rpp_Bak x' where x is the \n number of most recent *.rpp-bak files to keep");
- Console.WriteLine("Must include only 1 integer argument for number \n of *.rpp-bak files to keep in each directory.");
- Console.WriteLine("");
- Console.WriteLine("Run 'Delete_Rpp_Bak.exe' in the directory above \n the directories you wish to clean");
- }
- }
- else
- {
- Console.WriteLine("argument is null");
- Console.WriteLine("Delete_Rpp_Bak.exe is a utility that deletes \n older backup files and keeps x number of files.");
- Console.WriteLine("");
- Console.WriteLine("Usage: 'Delete_Rpp_Bak x' where x is the \n number of most recent *.rpp-bak files to keep");
- Console.WriteLine("Must include only 1 integer argument for number \n of *.rpp-bak files to keep in each directory.");
- Console.WriteLine("");
- Console.WriteLine("Run 'Delete_Rpp_Bak.exe' in the directory above \n the directories you wish to clean");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement