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

page last uploaded: 2021-01-27, 16:12