|
| 1 | +using NotepadStateLibrary; |
| 2 | +using CsvHelper; |
| 3 | +using System.Globalization; |
| 4 | +using CommandLine; |
| 5 | + |
| 6 | +Parser.Default.ParseArguments<Options>(args) |
| 7 | + .WithParsed(options => |
| 8 | + { |
| 9 | + string tabStateLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Packages\Microsoft.WindowsNotepad_8wekyb3d8bbwe\LocalState\TabState"); |
| 10 | + string windowStateLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Packages\Microsoft.WindowsNotepad_8wekyb3d8bbwe\LocalState\WindowState"); |
| 11 | + string outputLocation = Directory.GetCurrentDirectory(); |
| 12 | + |
| 13 | + if (!string.IsNullOrWhiteSpace(options.tabStateLocation)) |
| 14 | + { |
| 15 | + tabStateLocation = options.tabStateLocation; |
| 16 | + } |
| 17 | + if (!string.IsNullOrWhiteSpace(options.windowStateLocation)) |
| 18 | + { |
| 19 | + windowStateLocation = options.windowStateLocation; |
| 20 | + } |
| 21 | + if (!string.IsNullOrWhiteSpace(options.outputLocation)) |
| 22 | + { |
| 23 | + outputLocation = options.outputLocation; |
| 24 | + } |
| 25 | + |
| 26 | + Console.WriteLine("********** Starting **********"); |
| 27 | + Console.WriteLine("TabState Folder Location - {0}", tabStateLocation); |
| 28 | + if (!Directory.Exists(tabStateLocation)) |
| 29 | + { |
| 30 | + Console.WriteLine("!! ERROR: Invalid TabState Folder Location !!"); |
| 31 | + Environment.Exit(3); |
| 32 | + } |
| 33 | + |
| 34 | + Console.WriteLine("WindowState Folder Location - {0}", windowStateLocation); |
| 35 | + if (!Directory.Exists(windowStateLocation)) |
| 36 | + { |
| 37 | + Console.WriteLine("!! ERROR: Invalid WindowState Folder Location !!"); |
| 38 | + Environment.Exit(3); |
| 39 | + } |
| 40 | + |
| 41 | + Console.WriteLine("Output Folder Location - {0}", outputLocation); |
| 42 | + if (!Directory.Exists(outputLocation)) |
| 43 | + { |
| 44 | + Console.WriteLine("!! ERROR: Invalid Output Folder Location !!"); |
| 45 | + Environment.Exit(3); |
| 46 | + } |
| 47 | + |
| 48 | + Console.WriteLine(); |
| 49 | + |
| 50 | + List<NPTabState> fileTabs = new List<NPTabState>(); |
| 51 | + List<NPTabState> noFileTabs = new List<NPTabState>(); |
| 52 | + List<NPTabState> stateTabs = new List<NPTabState>(); |
| 53 | + List<NPWindowState> windowStateTabs = new List<NPWindowState>(); |
| 54 | + |
| 55 | + //Tabstate |
| 56 | + foreach (var path in Directory.EnumerateFiles(tabStateLocation, "*.bin")) |
| 57 | + { |
| 58 | + byte[] b = File.ReadAllBytes(path); |
| 59 | + if (b.Length > 0) |
| 60 | + { |
| 61 | + Console.WriteLine("Processing TabState - {0}", Path.GetFileName(path)); |
| 62 | + NPTabState np = new NPTabState(b, Path.GetFileName(path)); |
| 63 | + |
| 64 | + switch (np.TypeFlag) |
| 65 | + { |
| 66 | + case 0: |
| 67 | + noFileTabs.Add(np); |
| 68 | + break; |
| 69 | + case 1: |
| 70 | + fileTabs.Add(np); |
| 71 | + break; |
| 72 | + default: |
| 73 | + stateTabs.Add(np); |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + //Writing File Tabs |
| 80 | + using (var writer = new StreamWriter(Path.Combine(outputLocation, "FileTabs.csv"))) |
| 81 | + { |
| 82 | + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) |
| 83 | + { |
| 84 | + csv.WriteHeader<NPTabState>(); |
| 85 | + csv.NextRecord(); |
| 86 | + csv.WriteRecords(fileTabs); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + //Writing No File Tabs |
| 91 | + using (var writer = new StreamWriter(Path.Combine(outputLocation, "NoFileTabs.csv"))) |
| 92 | + { |
| 93 | + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) |
| 94 | + { |
| 95 | + csv.WriteHeader<NPTabState>(); |
| 96 | + csv.NextRecord(); |
| 97 | + csv.WriteRecords(noFileTabs); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + //Writing State Tabs |
| 102 | + using (var writer = new StreamWriter(Path.Combine(outputLocation, "StateTabs.csv"))) |
| 103 | + { |
| 104 | + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) |
| 105 | + { |
| 106 | + csv.WriteHeader<NPTabState>(); |
| 107 | + csv.NextRecord(); |
| 108 | + csv.WriteRecords(stateTabs); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + //Windowstate |
| 113 | + foreach (var path in Directory.EnumerateFiles(windowStateLocation, "*.bin")) |
| 114 | + { |
| 115 | + byte[] b = File.ReadAllBytes(path); |
| 116 | + if (b.Length > 0) |
| 117 | + { |
| 118 | + Console.WriteLine("Processing WindowState - {0}", Path.GetFileName(path)); |
| 119 | + NPWindowState np = new NPWindowState(b, Path.GetFileName(path)); |
| 120 | + windowStateTabs.Add(np); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + //Writing Window States |
| 125 | + using (var writer = new StreamWriter(Path.Combine(outputLocation, "WindowStateTabs.csv"))) |
| 126 | + { |
| 127 | + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) |
| 128 | + { |
| 129 | + csv.WriteHeader<NPWindowState>(); |
| 130 | + csv.NextRecord(); |
| 131 | + csv.WriteRecords(windowStateTabs); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + Console.WriteLine("\r\n********** Finished **********"); |
| 136 | + |
| 137 | + }) |
| 138 | + .WithNotParsed(errors => |
| 139 | + { |
| 140 | + foreach (var error in errors) |
| 141 | + { |
| 142 | + if (error is HelpRequestedError || error is VersionRequestedError) |
| 143 | + { |
| 144 | + |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + Console.WriteLine($"Error: {error}"); |
| 149 | + } |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | +public class Options |
| 154 | +{ |
| 155 | + [Option('t', "tabstatelocation", Required = false, HelpText = "Tab State Folder Location. Default value is the system location.")] |
| 156 | + public string tabStateLocation { get; set; } |
| 157 | + |
| 158 | + [Option('w', "windowstatelocation", Required = false, HelpText = "Window State Folder Location. Default value is the system location.")] |
| 159 | + public string windowStateLocation { get; set; } |
| 160 | + |
| 161 | + [Option('o', "outputlocation", Required = false, HelpText = "Output Folder Location for CSV files. Default location is same folder as program.")] |
| 162 | + public string outputLocation { get; set; } |
| 163 | + |
| 164 | + |
| 165 | +} |
0 commit comments