Program.cs (7521B)
1 using Microsoft.Win32; 2 using System; 3 using System.Collections.Generic; 4 using System.IO; 5 using System.Linq; 6 using System.Reflection; 7 8 namespace JpegDivider 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 if (args.Length > 2) 15 { 16 Console.WriteLine("Leave args blank or give exactly one dir!"); 17 } 18 19 switch ((args.FirstOrDefault() ?? string.Empty).ToLower()) 20 { 21 case "help": 22 ShowHelp(); 23 break; 24 case "register": 25 RegisterShellExt(); 26 break; 27 case "deregister": 28 DeregisterShellExt(); 29 break; 30 case "setfolder": 31 if (args.Length < 2) { Console.WriteLine("No folder given"); } 32 SetFolder(args[1]); 33 break; 34 default: 35 string dir; 36 if (args.Length > 0 && Directory.Exists(args[0])) 37 { 38 dir = args[0]; 39 } 40 else 41 { 42 dir = Directory.GetCurrentDirectory(); 43 } 44 45 Divide(dir); 46 47 break; 48 } 49 50 Console.WriteLine(""); 51 Console.WriteLine("Press any key..."); 52 Console.ReadKey(); 53 } 54 55 private static void SetFolder(string folder) 56 { 57 if (string.IsNullOrWhiteSpace(folder) || folder.IndexOfAny(Path.GetInvalidPathChars()) >= 0) 58 { 59 Console.WriteLine("Invalid folder name."); 60 return; 61 } 62 63 Registry.SetValue(@"HKEY_CURRENT_USER\Software\JpegDivider", "folder", folder); 64 Console.WriteLine("Custom folder name successfully set."); 65 } 66 67 private static void Divide(string dir) 68 { 69 Console.WriteLine($"Working directory: {dir}"); 70 71 string subfolder = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\JpegDivider", "folder", "JPEG") ?? "JPEG"; 72 Console.WriteLine($"Jpeg subfolder: {subfolder}"); 73 74 // create 75 string sepDir = Path.Combine(dir, subfolder); 76 if (!Directory.Exists(sepDir)) 77 { 78 Directory.CreateDirectory(sepDir); 79 } 80 81 // find jpg files in args or current folder 82 List<string> files = Directory.GetFiles(dir, "*.jpg", SearchOption.TopDirectoryOnly).ToList<string>(); 83 files.AddRange(Directory.GetFiles(dir, "*.jpeg", SearchOption.TopDirectoryOnly).ToList<string>()); 84 85 Console.WriteLine($"Found {files.Count} files."); 86 foreach (string file in files) 87 { 88 Console.Write($"Moving file {Path.GetFileName(file)}..."); 89 try 90 { 91 File.Move(file, Path.Combine(sepDir, Path.GetFileName(file))); 92 Console.WriteLine("done."); 93 } 94 catch 95 { 96 Console.WriteLine("failed!"); 97 } 98 } 99 100 Console.WriteLine("Finished."); 101 } 102 103 private static void DeregisterShellExt() 104 { 105 if (!UacCheck) { return; } 106 107 string MenuName = "Folder\\shell\\JpegDivider"; 108 string Command = "Folder\\shell\\JpegDivider\\command"; 109 110 try 111 { 112 RegistryKey reg = Registry.ClassesRoot.OpenSubKey(Command); 113 if (reg != null) 114 { 115 reg.Close(); 116 Registry.ClassesRoot.DeleteSubKey(Command); 117 } 118 reg = Registry.ClassesRoot.OpenSubKey(MenuName); 119 if (reg != null) 120 { 121 reg.Close(); 122 Registry.ClassesRoot.DeleteSubKey(MenuName); 123 } 124 125 Console.WriteLine("Successfully registered to explorer context menu"); 126 } 127 catch (Exception ex) 128 { 129 Console.WriteLine($"Failed to remove application from Windows Explorer context menu:{Environment.NewLine}{ex.Message}"); 130 } 131 } 132 133 private static void RegisterShellExt() 134 { 135 if (!UacCheck) { return; } 136 137 string MenuName = "Folder\\shell\\JpegDivider"; 138 string Command = "Folder\\shell\\JpegDivider\\command"; 139 140 RegistryKey regmenu = null; 141 RegistryKey regcmd = null; 142 try 143 { 144 regmenu = Registry.ClassesRoot.CreateSubKey(MenuName); 145 if (regmenu != null) 146 { 147 regmenu.SetValue("", "JpegDivider"); 148 } 149 regcmd = Registry.ClassesRoot.CreateSubKey(Command); 150 if (regcmd != null) 151 { 152 regcmd.SetValue("", $"\"{Assembly.GetEntryAssembly().Location}\" \"%L\""); 153 } 154 155 Console.WriteLine("Successfully registered from explorer context menu"); 156 } 157 catch (Exception ex) 158 { 159 Console.WriteLine($"Failed to register application to Windows Explorer context menu:{Environment.NewLine}{ex.Message}"); 160 } 161 finally 162 { 163 if (regmenu != null) 164 { 165 regmenu.Close(); 166 } 167 if (regcmd != null) 168 { 169 regcmd.Close(); 170 } 171 } 172 } 173 174 private static void ShowHelp() 175 { 176 Console.WriteLine($"{Assembly.GetExecutingAssembly().GetName().Name} - Version {Assembly.GetExecutingAssembly().GetName().Version}"); 177 Console.WriteLine("published under BSD 3 - Clause License"); 178 Console.WriteLine("Copyright(c) 2018-2019, René Wagner"); 179 Console.WriteLine("https://gitlab.com/guzzisti1/rawfilewizard"); 180 Console.WriteLine(""); 181 Console.WriteLine("divide jpegs from raw files by moving them to a specified subfolder (default: JPEG)"); 182 Console.WriteLine("the subfolder is created if not existing"); 183 Console.WriteLine(""); 184 Console.WriteLine("commandline:"); 185 Console.WriteLine("jpegdivider.exe - move jpegs from current working dir to subfolder"); 186 Console.WriteLine("jpgedivider.exe <path_to_folder> - move jpegs from specified folder to subfolder"); 187 Console.WriteLine("jpegdivider.exe setfolder <folder_name> - set a custom folder name, default is JPEG"); 188 Console.WriteLine("jpegdivider.exe register - register the program to folder context menu, run JpegDivider on a selected folder from Windows Explorer"); 189 Console.WriteLine("jpegdivider.exe deregister - remove the programm from folder context menu"); 190 Console.WriteLine("jpgedivider.exe help - show this help"); 191 } 192 193 private static bool UacCheck 194 { 195 get 196 { 197 if (UacHelper.IsProcessElevated) 198 { 199 return true; 200 } 201 else 202 { 203 Console.WriteLine("Need to run as administrator / with elevated UAC rights!"); 204 return false; 205 } 206 } 207 } 208 } 209 }