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

page last modified: 2024-04-16; loaded in 0.0207 seconds