Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for openfilebox.cs

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

  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using System.Windows.Forms;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	class OpenFile
  11. 	{
  12. 		static string progver = "1.04";
  13.  
  14. 		[STAThread]
  15. 		static int Main( string[] args )
  16. 		{
  17. 			foreach ( string arg in args )
  18. 			{
  19. 				if ( arg == "/?" )
  20. 				{
  21. 					return ShowHelp( );
  22. 				}
  23. 			}
  24.  
  25. 			using ( OpenFileDialog dialog = new OpenFileDialog( ) )
  26. 			{
  27. 				string filter = "All files (*.*)|*.*";
  28. 				string folder = Directory.GetCurrentDirectory( );
  29. 				string title = String.Format( "OpenFileBox,  Version {0}", progver );
  30.  
  31. 				if ( args.Length > 3 )
  32. 				{
  33. 					return ShowHelp( "Too many command line arguments" );
  34. 				}
  35. 				if ( args.Length > 0 )
  36. 				{
  37. 					filter = args[0];
  38. 					// If only "*.ext" is specified, use "ext files (*.ext)|*.ext" instead
  39. 					if ( Regex.IsMatch( filter, @"^\*\.(\*|\w+)$" ) )
  40. 					{
  41. 						string ext = filter.Substring( 2 ).ToLower( );
  42. 						if ( ext == ".*" )
  43. 						{
  44. 							filter = String.Format( "All files (*.{0})|*.{0}", ext );
  45. 						}
  46. 						else
  47. 						{
  48. 							filter = String.Format( "{0} files (*.{0})|*.{0}", ext );
  49. 						}
  50. 					}
  51. 					// Append "All files" filter if not specified
  52. 					if ( !Regex.IsMatch( filter, @"All files\s+\(\*\.\*\)\|\*\.\*", RegexOptions.IgnoreCase ) )
  53. 					{
  54. 						if ( String.IsNullOrWhiteSpace( filter ) )
  55. 						{
  56. 							filter = "All files (*.*)|*.*";
  57. 						}
  58. 						else
  59. 						{
  60. 							filter = filter + "|All files (*.*)|*.*";
  61. 						}
  62. 					}
  63. 					// Optional second command line argument is start folder
  64. 					if ( args.Length > 1 )
  65. 					{
  66. 						try
  67. 						{
  68. 							folder = Path.GetFullPath( args[1] );
  69. 						}
  70. 						catch ( ArgumentException )
  71. 						{
  72. 							// Assuming the error is caused by a trailing backslash in doublequotes
  73. 							folder = args[1].Substring( 0, args[1].IndexOf( '"' ) );
  74. 							folder = Path.GetFullPath( folder + "." );
  75. 						}
  76. 						if ( !Directory.Exists( folder ) )
  77. 						{
  78. 							return ShowHelp( "Invalid folder \"{0}\"", folder );
  79. 						}
  80. 						// Optional third command line argument is dialog title
  81. 						if ( args.Length > 2 )
  82. 						{
  83. 							title = args[2];
  84. 						}
  85. 					}
  86. 				}
  87. 				dialog.Filter = filter;
  88. 				dialog.FilterIndex = 1;
  89. 				dialog.InitialDirectory = folder;
  90. 				dialog.Title = title;
  91. 				dialog.RestoreDirectory = true;
  92. 				if ( dialog.ShowDialog( ) == DialogResult.OK )
  93. 				{
  94. 					Console.WriteLine( dialog.FileName );
  95. 					return 0;
  96. 				}
  97. 				else
  98. 				{
  99. 					// Cancel was clicked
  100. 					return 2;
  101. 				}
  102. 			}
  103. 		}
  104.  
  105. 		static int ShowHelp( params string[] errmsg )
  106. 		{
  107. 			/*
  108. 			OpenFileBox.exe,  Version 1.04
  109. 			Batch tool to present an Open File Dialog and return the selected file path
  110.  
  111. 			Usage:  OPENFILEBOX  [ "filetypes"  [ "startfolder"  [ "title" ] ] ]
  112.  
  113. 			Where:  filetypes    file type(s) in format "description (*.ext)|*.ext"
  114. 			                     or just "*.ext" (default: "All files (*.*)|*.*")
  115. 			        startfolder  the initial folder the dialog will show on opening
  116. 			                     (default: current directory)
  117. 			        title        the caption in the dialog's title bar
  118. 			                     (default: program name and version)
  119.  
  120. 			Notes:  This batch tool does not actually open the selected file, it is only
  121. 			        intended to interactively select a file, which can be used by the
  122. 			        calling batch file.
  123. 			        Multiple file types can be used for the filetypes filter; use "|" as a
  124. 			        separator, e.g. "PDF files (*.pdf)|*.txt|Word documents (*.doc)|*.doc".
  125. 			        If the filetypes filter is in "*.ext" format, "ext files (*.ext)|*.ext"
  126. 			        will be used instead.
  127. 			        Unless the filetypes filter specified is "All files (*.*)|*.*" or
  128. 			        "*.*", the filetypes filter "|All files (*.*)|*.*" will be appended.
  129. 			        The full path of the selected file is written to Standard Output
  130. 			        if OK was clicked, or an empty string if Cancel was clicked.
  131. 			        The return code will be 0 on success, 1 in case of (command line)
  132. 			        errors, or 2 if Cancel was clicked.
  133.  
  134. 			Written by Rob van der Woude
  135. 			http://www.robvanderwoude.com
  136. 			*/
  137.  
  138. 			#region Error Message
  139.  
  140. 			if ( errmsg.Length > 0 )
  141. 			{
  142. 				List<string> errargs = new List<string>( errmsg );
  143. 				errargs.RemoveAt( 0 );
  144. 				Console.Error.WriteLine( );
  145. 				Console.ForegroundColor = ConsoleColor.Red;
  146. 				Console.Error.Write( "ERROR:\t" );
  147. 				Console.ForegroundColor = ConsoleColor.White;
  148. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  149. 				Console.ResetColor( );
  150. 			}
  151.  
  152. 			#endregion Error Message
  153.  
  154. 			Console.Error.WriteLine( );
  155.  
  156. 			Console.Error.WriteLine( "OpenFileBox.exe,  Version {0}", progver );
  157.  
  158. 			Console.Error.WriteLine( "Batch tool to present an Open File Dialog and return the selected file path" );
  159.  
  160. 			Console.Error.WriteLine( );
  161.  
  162. 			Console.Error.Write( "Usage:  " );
  163. 			Console.ForegroundColor = ConsoleColor.White;
  164. 			Console.Error.WriteLine( "OPENFILEBOX  [ \"filetypes\"  [ \"startfolder\"  [ \"title\" ] ] ]" );
  165. 			Console.ResetColor( );
  166.  
  167. 			Console.Error.WriteLine( );
  168.  
  169. 			Console.Error.Write( "Where:  " );
  170. 			Console.ForegroundColor = ConsoleColor.White;
  171. 			Console.Error.Write( "filetypes" );
  172. 			Console.ResetColor( );
  173. 			Console.Error.Write( "    file type(s) in format " );
  174. 			Console.ForegroundColor = ConsoleColor.White;
  175. 			Console.Error.WriteLine( "\"description (*.ext)|*.ext\"" );
  176. 			Console.ResetColor( );
  177.  
  178. 			Console.Error.Write( "                     or just " );
  179. 			Console.ForegroundColor = ConsoleColor.White;
  180. 			Console.Error.Write( "\"*.ext\"" );
  181. 			Console.ResetColor( );
  182. 			Console.Error.Write( " (default: " );
  183. 			Console.ForegroundColor = ConsoleColor.White;
  184. 			Console.Error.Write( "\"All files (*.*)|*.*\"" );
  185. 			Console.ResetColor( );
  186. 			Console.Error.WriteLine( ")" );
  187.  
  188. 			Console.ForegroundColor = ConsoleColor.White;
  189. 			Console.Error.Write( "        startfolder" );
  190. 			Console.ResetColor( );
  191. 			Console.Error.WriteLine( "  the initial folder the dialog will show on opening" );
  192. 			Console.Error.WriteLine( "                     (default: current directory)" );
  193.  
  194. 			Console.ForegroundColor = ConsoleColor.White;
  195. 			Console.Error.Write( "        title" );
  196. 			Console.ResetColor( );
  197.  
  198. 			Console.Error.WriteLine( "        the caption in the dialog's title bar" );
  199.  
  200. 			Console.Error.WriteLine( "                     (default: \"OpenFileBox,  Version {0})\"", progver );
  201.  
  202. 			Console.Error.WriteLine( );
  203.  
  204. 			Console.Error.WriteLine( "Notes:  This batch tool does not actually open the selected file, it is only" );
  205.  
  206. 			Console.Error.WriteLine( "        intended to interactively select a file, which can be used by the" );
  207.  
  208. 			Console.Error.WriteLine( "        calling batch file." );
  209.  
  210. 			Console.Error.WriteLine( "        Multiple file types can be used for the filetypes filter; use \"|\" as a" );
  211.  
  212. 			Console.Error.Write( "        separator, e.g. " );
  213. 			Console.ForegroundColor = ConsoleColor.White;
  214. 			Console.Error.WriteLine( "\"PDF files (*.pdf)|*.txt|Word documents (*.doc)|*.doc\"." );
  215. 			Console.ResetColor( );
  216.  
  217. 			Console.Error.Write( "        If the filetypes filter is in " );
  218. 			Console.ForegroundColor = ConsoleColor.White;
  219. 			Console.Error.Write( "\"*.ext\"" );
  220. 			Console.ResetColor( );
  221. 			Console.Error.Write( " format, " );
  222. 			Console.ForegroundColor = ConsoleColor.White;
  223. 			Console.Error.WriteLine( "\"ext files (*.ext)|*.ext\"" );
  224. 			Console.ResetColor( );
  225.  
  226. 			Console.Error.WriteLine( "        will be used instead." );
  227.  
  228. 			Console.Error.Write( "        Unless the filetypes filter specified is " );
  229. 			Console.ForegroundColor = ConsoleColor.White;
  230. 			Console.Error.Write( "\"All files (*.*)|*.*\"" );
  231. 			Console.ResetColor( );
  232. 			Console.Error.WriteLine( " or" );
  233.  
  234. 			Console.ForegroundColor = ConsoleColor.White;
  235. 			Console.Error.Write( "        \"*.*\"" );
  236. 			Console.ResetColor( );
  237. 			Console.Error.Write( ", the filetypes filter " );
  238. 			Console.ForegroundColor = ConsoleColor.White;
  239. 			Console.Error.Write( "\"|All files (*.*)|*.*\"" );
  240. 			Console.ResetColor( );
  241. 			Console.Error.WriteLine( " will be appended." );
  242.  
  243.  
  244. 			Console.Error.WriteLine( "        The full path of the selected file is written to Standard Output" );
  245.  
  246. 			Console.Error.WriteLine( "        if OK was clicked, or an empty string if Cancel was clicked." );
  247.  
  248. 			Console.Error.WriteLine( "        The return code will be 0 on success, 1 in case of (command line)" );
  249.  
  250. 			Console.Error.WriteLine( "        errors, or 2 if Cancel was clicked." );
  251.  
  252. 			Console.Error.WriteLine( );
  253.  
  254. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  255.  
  256. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  257.  
  258. 			return 1;
  259. 		}
  260. 	}
  261. }
  262.  

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