123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- using System;
- using System.IO;
- namespace DirEx
- {
- internal class Program
- {
- private static int Main(string[] args)
- {
- if (args.Length == 0)
- {
- Console.Error.WriteLine("Usage DirEx </f|/d> [/day:<num>] [/dir:<path>] [/pattern:<pattern>] /s");
- Console.Error.WriteLine(" /f limit result to files");
- Console.Error.WriteLine(" /d limit result to directories");
- Console.Error.WriteLine(" /day:<num> limit to older <num> days");
- Console.Error.WriteLine(" /dir:<path> path to list, default on working directory");
- Console.Error.WriteLine(" /pattern:<pattern> filter pattern default is *");
- Console.Error.WriteLine(" /s search in sub directories");
- Environment.ExitCode = -1;
- Environment.Exit(1);
- return -1;
- }
- var dir = Environment.CurrentDirectory;
- var showFile = false;
- var showDir = false;
- DateTime? day = null;
- var pattern = "*";
- SearchOption option = SearchOption.TopDirectoryOnly;
- foreach (var arg in args)
- {
- var argKv = arg.Split(new[] { ':' }, 2);
- var k = argKv[0].ToLower();
- var v = argKv.Length == 2 ? argKv[1] : null;
- switch (k)
- {
- default:
- Console.Error.WriteLine($"Unrecognizable param: {k}");
- Environment.ExitCode = -1;
- Environment.Exit(1);
- return -1;
- case "/dir":
- if (string.IsNullOrWhiteSpace(v))
- {
- Console.Error.WriteLine("Missing argument in /dir");
- Environment.ExitCode = -1;
- Environment.Exit(1);
- return -1;
- }
- dir = v;
- break;
- case "/f": showFile = true; break;
- case "/d": showDir = true; break;
- case "/pattern":
- if (string.IsNullOrWhiteSpace(v))
- {
- Console.Error.WriteLine("Missing argument in /pattern");
- Environment.ExitCode = -1;
- Environment.Exit(1);
- return -1;
- }
- pattern = v;
- break;
- case "/day":
- if (string.IsNullOrWhiteSpace(v))
- {
- Console.Error.WriteLine("Missing argument in /day");
- Environment.ExitCode = -1;
- Environment.Exit(1);
- return -1;
- }
- if (int.TryParse(v, out var days))
- {
- day = DateTime.Now.AddDays(-days);
- }
- else
- {
- Console.Error.WriteLine("Invalid argument in /day");
- Environment.ExitCode = -1;
- Environment.Exit(1);
- return -1;
- }
- break;
- case "/s":
- option = SearchOption.AllDirectories;
- break;
- }
- }
- if (showFile && showDir)
- {
- Console.Error.WriteLine("Conflicted between /f and /d");
- Environment.ExitCode = -1;
- Environment.Exit(1);
- return -1;
- }
- if (!showFile && !showDir)
- {
- Console.Error.WriteLine("Missing /f or /d");
- Environment.ExitCode = -1;
- Environment.Exit(1);
- return -1;
- }
- if (showFile)
- {
- var entries = Directory.GetFiles(dir, pattern, option);
- foreach (var item in entries)
- {
- if (day.HasValue)
- {
- var info = new FileInfo(item);
- if (info.LastWriteTime < day) Console.WriteLine(item);
- }
- else
- {
- Console.WriteLine(item);
- }
- }
- }
- if (showDir)
- {
- var entries = Directory.GetDirectories(dir, pattern, option);
- foreach (var item in entries)
- {
- if (day.HasValue)
- {
- var info = new DirectoryInfo(item);
- if (info.LastWriteTime < day) Console.WriteLine(item);
- }
- else
- {
- Console.WriteLine(item);
- }
- }
- }
- return 0;
- }
- }
- }
|