Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for savefilebox.cs

(view source code of savefilebox.cs as plain text)

  1. using System;
  2. using System.IO;
  3. using System.Text.RegularExpressions;
  4. using System.Windows.Forms;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	class SaveFileBox
  10. 	{
  11. 		static string progver = "1.00";
  12.  
  13. 		[STAThread]
  14. 		static int Main( string[] args )
  15. 		{
  16. 			try
  17. 			{
  18. 				using ( SaveFileDialog dialog = new SaveFileDialog( ) )
  19. 				{
  20. 					string filter = "All files (*.*)|*.*";
  21. 					string ext = ".*";
  22. 					string folder = Directory.GetCurrentDirectory( );
  23. 					string title = "SaveFileBox,  Version " + progver;
  24. 					bool forceext = false;
  25. 					bool overwrite = false;
  26. 					bool verbose = false;
  27. 					bool forcespec = false;
  28. 					bool promptspec = false;
  29.  
  30. 					if ( args.Length > 5 )
  31. 					{
  32. 						return WriteError( "Too many command line arguments" );
  33. 					}
  34. 					if ( args.Length > 0 )
  35. 					{
  36. 						filter = args[0];
  37. 						if ( filter == "/?" )
  38. 						{
  39. 							return WriteError( );
  40. 						}
  41. 						ext = Regex.Match( filter, @"(\.[^.]+)$", RegexOptions.IgnoreCase ).Groups[0].ToString( );
  42. 						if ( String.IsNullOrWhiteSpace( ext ) )
  43. 						{
  44. 							return WriteError( "Invalid filetype specification" );
  45. 						}
  46. 						// If only "*.ext" is specified, use "ext files (*.ext)|*.ext" instead
  47. 						if ( Regex.IsMatch( filter, @"^\*\.(\*|\w+)$" ) )
  48. 						{
  49. 							if ( ext == ".*" )
  50. 							{
  51. 								filter = "All files (*." + ext + ")|*." + ext;
  52. 							}
  53. 							else
  54. 							{
  55. 								filter = ext + " files (*." + ext + ")|*." + ext;
  56. 							}
  57. 						}
  58. 						// Use "All files" filter if not specified
  59. 						if ( String.IsNullOrWhiteSpace( filter ) )
  60. 						{
  61. 							filter = "All files (*.*)|*.*";
  62. 						}
  63. 						// Optional second command line argument is start folder
  64. 						if ( args.Length > 1 )
  65. 						{
  66. 							folder = args[1];
  67. 							if ( !Directory.Exists( folder ) )
  68. 							{
  69. 								return WriteError( "Invalid folder \"" + folder + "\"" );
  70. 							}
  71. 							// Optional third command line argument is dialog title
  72. 							if ( args.Length > 2 )
  73. 							{
  74. 								title = args[2];
  75. 								if ( args.Length > 3 )
  76. 								{
  77. 									switch ( args[3].ToUpper( ) )
  78. 									{
  79. 										case "/F":
  80. 											forceext = true;
  81. 											forcespec = true;
  82. 											break;
  83. 										case "/Q":
  84. 											overwrite = true;
  85. 											promptspec = true;
  86. 											break;
  87. 										case "/V":
  88. 											verbose = true;
  89. 											promptspec = true;
  90. 											break;
  91. 										default:
  92. 											return WriteError( "Invalid command line argument(s)" );
  93. 									}
  94. 									if ( args.Length > 4 )
  95. 									{
  96. 										switch ( args[4].ToUpper( ) )
  97. 										{
  98. 											case "/F":
  99. 												if ( forcespec )
  100. 												{
  101. 													return WriteError( "Duplicate command line switch \"/F\"" );
  102. 												}
  103. 												forceext = true;
  104. 												forcespec = true;
  105. 												break;
  106. 											case "/Q":
  107. 												if ( promptspec )
  108. 												{
  109. 													return WriteError( "Duplicate command line switches or invalid combination" );
  110. 												}
  111. 												overwrite = true;
  112. 												promptspec = true;
  113. 												break;
  114. 											case "/V":
  115. 												if ( promptspec )
  116. 												{
  117. 													return WriteError( "Duplicate command line switches or invalid combination" );
  118. 												}
  119. 												verbose = true;
  120. 												promptspec = true;
  121. 												break;
  122. 											default:
  123. 												return WriteError( "Invalid command line argument" );
  124. 										}
  125. 									}
  126. 								}
  127. 							}
  128. 						}
  129. 					}
  130. 					dialog.Filter = filter;
  131. 					dialog.FilterIndex = 1;
  132. 					dialog.InitialDirectory = folder;
  133. 					dialog.AddExtension = forceext;
  134. 					dialog.DefaultExt = ext;
  135. 					dialog.CheckFileExists = false;
  136. 					dialog.CheckPathExists = !overwrite;
  137. 					dialog.CreatePrompt = false;
  138. 					dialog.OverwritePrompt = false;
  139. 					dialog.SupportMultiDottedExtensions = true;
  140. 					dialog.Title = title;
  141. 					dialog.RestoreDirectory = true;
  142. 					if ( dialog.ShowDialog( ) == DialogResult.OK )
  143. 					{
  144. 						string filename = dialog.FileName;
  145. 						if ( forceext )
  146. 						{
  147. 							string newext = Path.GetExtension( filename );
  148. 							if ( String.Compare( newext, ext, true ) != 0 )
  149. 							{
  150. 								filename += ext;
  151. 							}
  152. 						}
  153. 						if ( File.Exists( filename ) )
  154. 						{
  155. 							// File exists
  156. 							if ( !overwrite )
  157. 							{
  158. 								string prompt = "The file \"" + Path.GetFileName( filename ) + "\" already exists.\n\nDo you want to replace it?";
  159. 								string caption = "Overwrite File?";
  160. 								MessageBoxButtons buttons = MessageBoxButtons.YesNo;
  161. 								MessageBoxIcon icon = MessageBoxIcon.Warning;
  162. 								DialogResult result = MessageBox.Show( prompt, caption, buttons, icon, MessageBoxDefaultButton.Button2 );
  163. 								if ( result != DialogResult.Yes )
  164. 								{
  165. 									// Canceled
  166. 									return 2;
  167. 								}
  168. 							}
  169. 							// Overwrite approved
  170. 							Console.WriteLine( filename );
  171. 							return 3;
  172. 						}
  173. 						else
  174. 						{
  175. 							// File does not exist
  176. 							if ( verbose )
  177. 							{
  178. 								string prompt = "The file \"" + Path.GetFileName( filename ) + "\" does not exist.\n\nDo you want to create it?";
  179. 								string caption = "Create File?";
  180. 								MessageBoxButtons buttons = MessageBoxButtons.YesNo;
  181. 								MessageBoxIcon icon = MessageBoxIcon.Question;
  182. 								DialogResult result = MessageBox.Show( prompt, caption, buttons, icon, MessageBoxDefaultButton.Button1 );
  183. 								if ( result != DialogResult.Yes )
  184. 								{
  185. 									// Canceled
  186. 									return 2;
  187. 								}
  188. 							}
  189. 							// OK
  190. 							Console.WriteLine( filename );
  191. 							return 0;
  192. 						}
  193. 					}
  194. 					else
  195. 					{
  196. 						// Canceled
  197. 						return 2;
  198. 					}
  199. 				}
  200. 			}
  201. 			catch ( Exception e )
  202. 			{
  203. 				return WriteError( e.Message );
  204. 			}
  205. 		}
  206.  
  207. 		public static int WriteError( string errorMessage = null )
  208. 		{
  209. 			/*
  210. 			SaveFileBox.exe,  Version 1.00
  211. 			Batch tool to present a Save File dialog and return the selected file path
  212.  
  213. 			Usage:  SAVEFILEBOX  "filetypes"  "startfolder"  "title"  options
  214.  
  215. 			Where:  filetypes    file type(s) in format "description (*.ext)|*.ext"
  216. 			                     or just "*.ext" (default: "All files (*.*)|*.*")
  217. 			        startfolder  the initial folder the dialog will show on opening
  218. 			                     (default: current directory)
  219. 			        title        the caption in the dialog's title bar
  220. 			                     (default: program name and version)
  221. 			        options      /F    Force specified extension
  222. 			                     /Q    Quiet mode: do not check if the file exists
  223. 			                     /V    Verbose mode: prompt for confirmation
  224. 			                     (default: prompt only if file exists)
  225.  
  226. 			Notes:  All command line arguments are optional, but each argument requires
  227. 			        the ones preceeding it, e.g. "startfolder" requires "filetypes" but
  228. 			        not necessarily "title" and options.
  229. 			        Options /Q and /V are mutually exclusive.
  230. 			        If the filetypes filter is in "*.ext" format, "ext files (*.ext)|*.ext"
  231. 			        will be used instead.
  232. 			        The full path of the selected file is written to Standard Output
  233. 			        if OK was clicked, or an empty string if Cancel was clicked.
  234. 			        The return code will be 0 on success, 1 in case of (command line)
  235. 			        errors, 2 on Cancel, 3 if not in Quiet mode and file exists.
  236.  
  237. 			Written by Rob van der Woude
  238. 			http://www.robvanderwoude.com
  239. 			*/
  240.  
  241. 			if ( !string.IsNullOrWhiteSpace( errorMessage ) )
  242. 			{
  243. 				Console.Error.WriteLine( );
  244. 				Console.ForegroundColor = ConsoleColor.Red;
  245. 				Console.Error.Write( "ERROR: " );
  246. 				Console.ForegroundColor = ConsoleColor.White;
  247. 				Console.Error.WriteLine( errorMessage );
  248. 				Console.ResetColor( );
  249. 			}
  250. 			Console.Error.WriteLine( );
  251. 			Console.Error.WriteLine( "SaveFileBox.exe,  Version {0}", progver );
  252. 			Console.Error.WriteLine( "Batch tool to present a Save File dialog and return the selected file path" );
  253. 			Console.Error.WriteLine( );
  254. 			Console.Error.Write( "Usage:  " );
  255. 			Console.ForegroundColor = ConsoleColor.White;
  256. 			Console.Error.WriteLine( "SAVEFILEBOX  \"filetypes\"  \"startfolder\"  \"title\"  options" );
  257. 			Console.ResetColor( );
  258. 			Console.Error.WriteLine( );
  259. 			Console.Error.Write( "Where:  " );
  260. 			Console.ForegroundColor = ConsoleColor.White;
  261. 			Console.Error.Write( "filetypes" );
  262. 			Console.ResetColor( );
  263. 			Console.Error.Write( "    file type(s) in format " );
  264. 			Console.ForegroundColor = ConsoleColor.White;
  265. 			Console.Error.WriteLine( "\"description (*.ext)|*.ext\"" );
  266. 			Console.ResetColor( );
  267. 			Console.Error.Write( "                     or just " );
  268. 			Console.ForegroundColor = ConsoleColor.White;
  269. 			Console.Error.Write( "\"*.ext\"" );
  270. 			Console.ResetColor( );
  271. 			Console.Error.Write( " (default: " );
  272. 			Console.ForegroundColor = ConsoleColor.White;
  273. 			Console.Error.Write( "\"All files (*.*)|*.*\"" );
  274. 			Console.ResetColor( );
  275. 			Console.Error.WriteLine( ")" );
  276. 			Console.ForegroundColor = ConsoleColor.White;
  277. 			Console.Error.Write( "        startfolder" );
  278. 			Console.ResetColor( );
  279. 			Console.Error.WriteLine( "  the initial folder the dialog will show on opening" );
  280. 			Console.Error.WriteLine( "                     (default: current directory)" );
  281. 			Console.ForegroundColor = ConsoleColor.White;
  282. 			Console.Error.Write( "        title" );
  283. 			Console.ResetColor( );
  284. 			Console.Error.WriteLine( "        the caption in the dialog's title bar" );
  285. 			Console.Error.WriteLine( "                     (default: \"SaveFileBox,  Version {0})\"", progver );
  286. 			Console.ForegroundColor = ConsoleColor.White;
  287. 			Console.Error.Write( "        options      /F    F" );
  288. 			Console.ResetColor( );
  289. 			Console.Error.WriteLine( "orce specified extension" );
  290. 			Console.ForegroundColor = ConsoleColor.White;
  291. 			Console.Error.Write( "                     /Q    Q" );
  292. 			Console.ResetColor( );
  293. 			Console.Error.WriteLine( "uiet mode: do not check if the file exists" );
  294. 			Console.ForegroundColor = ConsoleColor.White;
  295. 			Console.Error.Write( "                     /V    V" );
  296. 			Console.ResetColor( );
  297. 			Console.Error.WriteLine( "erbose mode: prompt for confirmation" );
  298. 			Console.Error.WriteLine( "                     (default: prompt only if file exists)" );
  299. 			Console.Error.WriteLine( );
  300. 			Console.Error.WriteLine( "Notes:  All command line arguments are optional, but each argument requires" );
  301. 			Console.Error.Write( "        the ones preceeding it, e.g. " );
  302. 			Console.ForegroundColor = ConsoleColor.White;
  303. 			Console.Error.Write( "\"startfolder\"" );
  304. 			Console.ResetColor( );
  305. 			Console.Error.Write( " requires " );
  306. 			Console.ForegroundColor = ConsoleColor.White;
  307. 			Console.Error.Write( "\"filetypes\"" );
  308. 			Console.ResetColor( );
  309. 			Console.Error.WriteLine( " but" );
  310. 			Console.Error.Write( "        not necessarily " );
  311. 			Console.ForegroundColor = ConsoleColor.White;
  312. 			Console.Error.Write( "\"title\"" );
  313. 			Console.ResetColor( );
  314. 			Console.Error.Write( " and " );
  315. 			Console.ForegroundColor = ConsoleColor.White;
  316. 			Console.Error.Write( "options" );
  317. 			Console.ResetColor( );
  318. 			Console.Error.WriteLine( "." );
  319. 			Console.Error.Write( "        Options " );
  320. 			Console.ForegroundColor = ConsoleColor.White;
  321. 			Console.Error.Write( "/Q" );
  322. 			Console.ResetColor( );
  323. 			Console.Error.Write( " and " );
  324. 			Console.ForegroundColor = ConsoleColor.White;
  325. 			Console.Error.Write( "/V" );
  326. 			Console.ResetColor( );
  327. 			Console.Error.WriteLine( " are mutually exclusive." );
  328. 			Console.Error.Write( "        If the filetypes filter is in " );
  329. 			Console.ForegroundColor = ConsoleColor.White;
  330. 			Console.Error.Write( "\"*.ext\"" );
  331. 			Console.ResetColor( );
  332. 			Console.Error.Write( " format, " );
  333. 			Console.ForegroundColor = ConsoleColor.White;
  334. 			Console.Error.WriteLine( "\"ext files (*.ext)|*.ext\"" );
  335. 			Console.ResetColor( );
  336. 			Console.Error.WriteLine( "        will be used instead." );
  337. 			Console.Error.WriteLine( "        The full path of the selected file is written to Standard Output" );
  338. 			Console.Error.WriteLine( "        if OK was clicked, or an empty string if Cancel was clicked." );
  339. 			Console.Error.WriteLine( "        The return code will be 0 on success, 1 in case of (command line)" );
  340. 			Console.Error.WriteLine( "        errors, 2 on Cancel, 3 if not in Quiet mode and file exists." );
  341. 			Console.Error.WriteLine( );
  342. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  343. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  344. 			return 1;
  345. 		}
  346. 	}
  347. }
  348.  

page last uploaded: 2017-08-21, 14:26