Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for powerpoint2any.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Office.Core;
  4. using System.IO;
  5. using PowerPoint = Microsoft.Office.Interop.PowerPoint;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	class PowerPoint2Any
  11. 	{
  12. 		static string progver = "1.00";
  13.  
  14.  
  15. 		// Generate a list of accepted file types
  16. 		static Dictionary<string, int> acceptedtypes = ListWorkingFormats( );
  17.  
  18.  
  19. 		static int Main( string[] args )
  20. 		{
  21. 			#region Initialize Variables
  22.  
  23. 			bool overwrite = false;
  24. 			string inputfilespec = null;
  25. 			string outputfilespec = null;
  26. 			string inputfolder;
  27. 			string outputfolder;
  28. 			string inputfile;
  29. 			string outputfile;
  30. 			string inputfilename;
  31. 			string outputfilename;
  32. 			string inputfileext;
  33. 			string outputfileext = null;
  34. 			int inputformat = -1;
  35. 			int outputformat = -1;
  36. 			#endregion Initialize Variables
  37.  
  38.  
  39. 			#region Command Line Parsing
  40.  
  41. 			if ( args.Length == 0 || ( args.Length == 1 && args[0].ToUpper( ) != "/T" ) )
  42. 			{
  43. 				return ShowHelp( );
  44. 			}
  45. 			foreach ( string arg in args )
  46. 			{
  47. 				if ( arg[0] == '/' )
  48. 				{
  49. 					if ( arg.ToString( ).ToUpper( ) == "/O" )
  50. 					{
  51. 						if ( overwrite )
  52. 						{
  53. 							return ShowHelp( "Duplicate command line switch /O" );
  54. 						}
  55. 						overwrite = true;
  56. 						break;
  57. 					}
  58. 					else if ( arg.ToString( ).ToUpper( ) == "/T" )
  59. 					{
  60. 						ListFormats( );
  61. 						return 0;
  62. 					}
  63. 					else if ( arg.Length > 3 && arg.Substring( 0, 3 ).ToUpper( ) == "/T:" )
  64. 					{
  65. 						if ( outputformat != -1 )
  66. 						{
  67. 							return ShowHelp( "Duplicate command line switch /T" );
  68. 						}
  69. 						outputformat = GetFormat( arg.Substring( 3 ) );
  70. 						if ( outputformat == -1 )
  71. 						{
  72. 							return ShowHelp( "Output file type not recognized, use /T to list all available types" );
  73. 						}
  74. 					}
  75. 					else
  76. 					{
  77. 						return ShowHelp( "Invalid command line switch {0}", arg );
  78. 					}
  79. 				}
  80. 				else
  81. 				{
  82. 					if ( String.IsNullOrEmpty( inputfilespec ) )
  83. 					{
  84. 						inputfilespec = arg;
  85. 					}
  86. 					else if ( String.IsNullOrEmpty( outputfilespec ) )
  87. 					{
  88. 						outputfilespec = arg;
  89. 					}
  90. 					else
  91. 					{
  92. 						return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  93. 					}
  94. 				}
  95. 			}
  96.  
  97. 			#endregion Command Line Parsing
  98.  
  99.  
  100. 			#region Command Line Validation
  101.  
  102. 			// Validate input filespec
  103. 			if ( String.IsNullOrEmpty( inputfilespec ) )
  104. 			{
  105. 				return ShowHelp( "Please specify an input file" );
  106. 			}
  107. 			switch ( ValidateFilespec( inputfilespec ) )
  108. 			{
  109. 				case -1:
  110. 					if ( inputfilespec.IndexOf( '*' ) == -1 )
  111. 					{
  112. 						return ShowHelp( "Parent folder of input file not found" );
  113. 					}
  114. 					else
  115. 					{
  116. 						return ShowHelp( "Parent folder of input files not found" );
  117. 					}
  118. 				case 0:
  119. 					if ( inputfilespec.IndexOf( '*' ) == -1 )
  120. 					{
  121. 						return ShowHelp( "Input file not found" );
  122. 					}
  123. 					else
  124. 					{
  125. 						return ShowHelp( "No matching input files found" );
  126. 					}
  127. 				case 1:
  128. 					break;
  129. 				default:
  130. 					if ( !String.IsNullOrEmpty( outputfilespec ) && Path.GetFileNameWithoutExtension( outputfilespec ) != "*" )
  131. 					{
  132. 						return ShowHelp( "When using wildcards in the input file names,\n\tyou must use wildcard \"*\" for the output file names, if specified" );
  133. 					}
  134. 					break;
  135. 			}
  136. 			inputfolder = Directory.GetParent( inputfilespec ).FullName;
  137. 			inputfile = Path.GetFileName( inputfilespec );
  138. 			inputfilename = Path.GetFileNameWithoutExtension( inputfilespec );
  139. 			inputfileext = Path.GetExtension( inputfilespec );
  140. 			inputformat = GetFormatByExtension( inputfilespec );
  141. 			if ( inputformat == -1 )
  142. 			{
  143. 				return ShowHelp( "Unrecognized input file type" );
  144. 			}
  145.  
  146. 			// Validate or build output filespec
  147. 			if ( String.IsNullOrEmpty( outputfilespec ) )
  148. 			{
  149. 				if ( outputformat == -1 )
  150. 				{
  151. 					return ShowHelp( "Please specify output file(s) and/or valid output file type" );
  152. 				}
  153. 				outputfolder = inputfolder;
  154. 				outputfile = "*";
  155. 				outputfilename = "*";
  156. 				outputfilespec = Path.Combine( outputfolder, outputfile );
  157. 				// Extension will be default extension based on file type
  158. 			}
  159. 			else
  160. 			{
  161. 				outputfile = Path.GetFileName( outputfilespec );
  162. 				outputfilename = Path.GetFileNameWithoutExtension( outputfilespec );
  163. 				outputfileext = Path.GetExtension( outputfilespec );
  164. 				if ( outputfilespec.IndexOf( '\\' ) == -1 ) // e.g. "*.xps" or "filename.pdf"
  165. 				{
  166. 					outputfolder = inputfolder;
  167. 					outputfilespec = Path.Combine( outputfolder, outputfile );
  168. 				}
  169. 				outputfolder = Directory.GetParent( outputfilespec ).FullName;
  170. 				if ( ValidateFilespec( outputfilespec ) == -1 )
  171. 				{
  172. 					if ( outputfilespec.IndexOf( '*' ) == -1 )
  173. 					{
  174. 						return ShowHelp( "Parent folder for output file not found" );
  175. 					}
  176. 					else
  177. 					{
  178. 						return ShowHelp( "Parent folder for output files not found" );
  179. 					}
  180. 				}
  181. 			}
  182. 			if ( outputformat == -1 )
  183. 			{
  184. 				outputformat = GetFormatByExtension( outputfilespec );
  185. 			}
  186. 			if ( outputformat == -1 )
  187. 			{
  188. 				return ShowHelp( "Unrecognized output file type" );
  189. 			}
  190.  
  191. 			// Input and output file types should be different
  192. 			if ( inputformat == outputformat )
  193. 			{
  194. 				return ShowHelp( "Input and output file types should be different" );
  195. 			}
  196. 			// Input and output extensions should be different
  197. 			if ( inputfileext == outputfileext )
  198. 			{
  199. 				return ShowHelp( "Input and output file extensions should be different" );
  200. 			}
  201.  
  202. 			#endregion Command Line Validation
  203.  
  204.  
  205. 			#region Iterate File List and Convert Each File
  206.  
  207. 			int rc = 0;
  208.  
  209. 			foreach ( string file in Directory.GetFiles( inputfolder, inputfile ) )
  210. 			{
  211. 				if ( Path.GetExtension( file ) == inputfileext ) // prevent including *.pptx when *.ppt is specified
  212. 				{
  213. 					string output;
  214. 					if ( inputfilename.IndexOf( '*' ) > -1 )
  215. 					{
  216. 						output = Path.Combine( outputfolder, Path.GetFileNameWithoutExtension( file ) + outputfileext );
  217. 					}
  218. 					else
  219. 					{
  220. 						output = outputfilespec;
  221. 					}
  222. 					if ( File.Exists( output ) && !overwrite )
  223. 					{
  224. 						if ( inputfilename.IndexOf( '*' ) > -1 )
  225. 						{
  226. 							Console.WriteLine( "Skipped \"{0}\" because \"{1}\" already exists", Path.GetFileName( file ), Path.GetFileName( output ) );
  227. 						}
  228. 						else
  229. 						{
  230. 							return ShowHelp( "Output file \"{0}\" already exists, use /O to silently overwrite existing files", Path.GetFileName( output ) );
  231. 						}
  232. 					}
  233. 					else
  234. 					{
  235. 						Console.Write( "Converting \"{0}\" . . . ", Path.GetFileName( file ) );
  236. 						string result = ConvertPowerPoint( file, output, outputformat );
  237. 						Console.WriteLine( result );
  238. 						if ( result != "Success" )
  239. 						{
  240. 							rc += 1;
  241. 						}
  242. 					}
  243. 				}
  244. 			}
  245.  
  246. 			#endregion Iterate File List and Convert Each File
  247.  
  248. 			return rc;
  249. 		}
  250.  
  251.  
  252. 		// Opens the specified presentation and saves it with the specified file name and file type
  253. 		static string ConvertPowerPoint( string inputfile, string outputfile, int outputtype )
  254. 		{
  255. 			string result = "Failed";
  256. 			string outputfilename = Path.GetFileNameWithoutExtension( outputfile );
  257. 			string outputextension = Path.GetExtension( outputfile );
  258. 			string outputfolder;
  259. 			if ( outputfile.IndexOf( '\\' ) == -1 )
  260. 			{
  261. 				outputfolder = Directory.GetParent( inputfile ).FullName;
  262. 			}
  263. 			else
  264. 			{
  265. 				outputfolder = Directory.GetParent( outputfile ).FullName;
  266. 			}
  267. 			if ( outputfilename == "*" )
  268. 			{
  269. 				outputfilename = Path.GetFileNameWithoutExtension( inputfile );
  270. 			}
  271. 			outputfile = Path.Combine( outputfolder, outputfilename + outputextension );
  272.  
  273. 			PowerPoint.Application pptapp = new PowerPoint.Application( );
  274. 			PowerPoint.Presentation pptpres = pptapp.Presentations.Open( inputfile, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse );
  275. 			try
  276. 			{
  277. 				PowerPoint.PpSaveAsFileType filetype = (PowerPoint.PpSaveAsFileType) outputtype;
  278. 				pptpres.SaveAs( outputfile, filetype );
  279. 				result = "Success";
  280. 			}
  281. 			catch
  282. 			{
  283. 				result = "Failed";
  284. 			}
  285. 			pptpres.Close( );
  286. 			pptapp.Quit( );
  287. 			return result;
  288. 		}
  289.  
  290.  
  291. 		// Returns the numeric representation of the PowerPoint file format for the specified extension
  292. 		static int GetFormatByExtension( string file )
  293. 		{
  294. 			string ext = Path.GetExtension( file ).ToLower( ).Substring( 1 );
  295. 			Dictionary<string, int> knownpptexts = new Dictionary<string, int>( );
  296. 			knownpptexts["htm"] = (int) PowerPoint.PpSaveAsFileType.ppSaveAsHTML;
  297. 			knownpptexts["html"] = (int) PowerPoint.PpSaveAsFileType.ppSaveAsHTML;
  298. 			knownpptexts["odp"] = (int) PowerPoint.PpSaveAsFileType.ppSaveAsOpenDocumentPresentation;
  299. 			knownpptexts["pdf"] = (int) PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
  300. 			knownpptexts["pps"] = (int) PowerPoint.PpSaveAsFileType.ppSaveAsShow;
  301. 			knownpptexts["ppsx"] = (int) PowerPoint.PpSaveAsFileType.ppSaveAsOpenXMLShow;
  302. 			knownpptexts["ppt"] = (int) PowerPoint.PpSaveAsFileType.ppSaveAsPresentation;
  303. 			knownpptexts["pptx"] = (int) PowerPoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentation;
  304. 			knownpptexts["xps"] = (int) PowerPoint.PpSaveAsFileType.ppSaveAsXPS;
  305. 			if ( knownpptexts.ContainsKey( ext ) && acceptedtypes.ContainsValue( knownpptexts[ext] ) )
  306. 			{
  307. 				return knownpptexts[ext];
  308. 			}
  309. 			return -1;
  310. 		}
  311.  
  312.  
  313. 		// Returns the numeric representation for the specified PowerPoint file format
  314. 		static int GetFormat( string format )
  315. 		{
  316. 			// test for numeric fomat (type number)
  317. 			try
  318. 			{
  319. 				int testformat = (int) (PowerPoint.PpSaveAsFileType) Convert.ToInt32( format );
  320. 				if ( acceptedtypes.ContainsValue( testformat ) )
  321. 				{
  322. 					return testformat;
  323. 				}
  324. 				return -1;
  325. 			}
  326. 			catch { }
  327. 			// test for string format (type name)
  328. 			try
  329. 			{
  330. 				foreach ( string test in acceptedtypes.Keys )
  331. 				{
  332. 					if ( test.ToUpper( ) == format.ToUpper( ) )
  333. 					{
  334. 						return acceptedtypes[test];
  335. 					}
  336. 				}
  337. 			}
  338. 			catch { }
  339. 			// return -1 if format not valid
  340. 			return -1;
  341. 		}
  342.  
  343.  
  344. 		// Lists all available file formats as string (name) and number
  345. 		static void ListFormats( )
  346. 		{
  347. 			int maxlen = 0;
  348. 			foreach ( int i in acceptedtypes.Values )
  349. 			{
  350. 				if ( ( (PowerPoint.PpSaveAsFileType) i ).ToString( ).Length > maxlen )
  351. 				{
  352. 					maxlen = ( (PowerPoint.PpSaveAsFileType) i ).ToString( ).Length;
  353. 				}
  354. 			}
  355. 			ConsoleColor bgblue = ConsoleColor.DarkBlue;
  356. 			ConsoleColor bgdefault = Console.BackgroundColor;
  357. 			Console.ForegroundColor = ConsoleColor.White;
  358. 			Console.WriteLine( String.Format( "{0,-" + maxlen + "}  {1}", "File Type", "Number" ) );
  359. 			if ( maxlen > 12 )
  360. 			{
  361. 				Console.WriteLine( new String( '=', 12 ) + new String( ' ', maxlen - 12 + 2 ) + new String( '=', 6 ) );
  362. 			}
  363. 			else
  364. 			{
  365. 				Console.WriteLine( new String( '=', maxlen ) + "  " + new String( '=', 6 ) );
  366. 			}
  367. 			List<ConsoleColor> oddeven = new List<ConsoleColor> { bgdefault, bgblue };
  368. 			int linenum = 0;
  369. 			foreach ( int i in acceptedtypes.Values )
  370. 			{
  371. 				Console.BackgroundColor = oddeven[linenum % 2];
  372. 				foreach ( string type in acceptedtypes.Keys )
  373. 				{
  374. 					if ( acceptedtypes[type] == i )
  375. 					{
  376. 						Console.Write( String.Format( "{0,-" + maxlen + "}  {1,4}  ", type, i ) );
  377. 					}
  378. 				}
  379. 				Console.BackgroundColor = bgdefault;
  380. 				Console.WriteLine( );
  381. 				linenum += 1;
  382. 			}
  383. 			Console.ResetColor( );
  384. 		}
  385.  
  386.  
  387. 		// Create a list of all available formats, removing known problem makers like bitmaps and RTF
  388. 		static Dictionary<string, int> ListWorkingFormats( )
  389. 		{
  390. 			List<int> knownproblemtypes = new List<int>( ); // Available file types that just don't work as expected
  391. 			knownproblemtypes.Add( (int) PowerPoint.PpSaveAsFileType.ppSaveAsBMP );
  392. 			knownproblemtypes.Add( (int) PowerPoint.PpSaveAsFileType.ppSaveAsEMF );
  393. 			knownproblemtypes.Add( (int) PowerPoint.PpSaveAsFileType.ppSaveAsGIF );
  394. 			knownproblemtypes.Add( (int) PowerPoint.PpSaveAsFileType.ppSaveAsJPG );
  395. 			knownproblemtypes.Add( (int) PowerPoint.PpSaveAsFileType.ppSaveAsMetaFile );
  396. 			knownproblemtypes.Add( (int) PowerPoint.PpSaveAsFileType.ppSaveAsPNG );
  397. 			knownproblemtypes.Add( (int) PowerPoint.PpSaveAsFileType.ppSaveAsRTF );
  398. 			knownproblemtypes.Add( (int) PowerPoint.PpSaveAsFileType.ppSaveAsTIF );
  399. 			Dictionary<string, int> acceptedtypes = new Dictionary<string, int>( );
  400. 			acceptedtypes.Clear( );
  401. 			for ( int i = 0; i < 64; i++ )
  402. 			{
  403. 				if ( ( (PowerPoint.PpSaveAsFileType) i ).ToString( ) != i.ToString( ) )
  404. 				{
  405. 					if ( !knownproblemtypes.Contains( i ) )
  406. 					{
  407. 						acceptedtypes.Add( ( (PowerPoint.PpSaveAsFileType) i ).ToString( ), i );
  408. 					}
  409. 				}
  410. 			}
  411. 			return acceptedtypes;
  412. 		}
  413.  
  414.  
  415. 		// Displays help text
  416. 		static int ShowHelp( params string[] errmsg )
  417. 		{
  418. 			#region Help Text
  419.  
  420. 			/*
  421. 			PowerPoint2Any,  Version 1.00
  422. 			Open a presentation in Microsoft PowerPoint and save it in "any" (known) format
  423.  
  424. 			Usage:     POWERPOINT2ANY  "presentation"  [ "outfile" ]  [ options ]
  425.  
  426. 			Where:     "presentation"  PowerPoint presentation(s) to be converted
  427. 			                           (wildcards allowed in file name, e.g. "name*.pptx")
  428. 			           "outfile"       output file(s) to be created (wildcard "*" allowed
  429. 			                           for file name, e.g. "*.odp" or "*.pdf")
  430. 			Options:   /O              silently overwrite existing output file(s)
  431. 			                           (default: abort or skip if output file exists)
  432. 			           /T              list accepted output file types
  433. 			           /T:type         set output file type (required if "outfile" is not
  434. 			                           specified; type may be number or string)
  435.  
  436. 			Notes: [1] This program requires a "regular" (MSI based) Microsoft PowerPoint
  437. 			           (2007 or later) installation, it will fail on an MS Office
  438. 			           "click-to-run" installation.
  439. 			       [2] For PowerPoint 2007, to save as PDF or XPS, this program requires
  440. 			           the "Microsoft Save as PDF or XPS Add-in for 2007 Microsoft Office
  441. 			           programs", available at:
  442. 			           http://www.microsoft.com/en-us/download/details.aspx?id=7
  443. 			       [3] If wildcards are used in the PowerPoint file names, and the output
  444. 			           file path is not specified, /T:type must be used, and the input
  445. 			           file names should not contain dots.
  446. 			       [4] If wildcards are used in the PowerPoint file names, and the output
  447. 			           file path is specified, the output file name must be "*".
  448. 			       [5] If wildcards are used in the PowerPoint file names, and the /O
  449. 			           switch is not used, the program will display an error message in
  450. 			           case an output file already exists, but it will then continue to
  451. 			           convert the next file instead of aborting.
  452. 			       [6] If PowerPoint was already active when this program is started,
  453. 			           any other opened presentation(s) will be left alone, and only
  454. 			           the presenation(s) opened by this program will be closed.
  455.  
  456. 			Examples:  EXCEL2ANY "D:\folder\myfile.ppt" *.pdf
  457. 			           will save to "D:\folder\myfile.pdf"
  458.  
  459. 			           EXCEL2ANY "D:\folder\myfile.pptx" "D:\otherfolder\*.odp"
  460. 			           will save to "D:\otherfolder\myfile.odp"
  461.  
  462. 			           EXCEL2ANY "D:\folder\myfile.pptx" "D:\elsewhere\newfile.xps"
  463. 			           will save to "D:\elsewhere\newfile.xps"
  464.  
  465. 			           EXCEL2ANY "D:\folder\name*.pptx" *.html
  466. 			           will save all matching files as HTML to "D:\folder\"
  467. 			           recognized extensions: htm(l), odp, pdf, pps(x), ppt(x), xps
  468.  
  469. 			           EXCEL2ANY "D:\folder\*.pptx" /T:12
  470. 			           like previous example, but more file types available
  471.  
  472. 			           EXCEL2ANY /T
  473. 			           will list all available file types
  474.  
  475. 			Written by Rob van der Woude
  476. 			http://www.robvanderwoude.com
  477. 			*/
  478.  
  479. 			if ( errmsg.Length > 0 )
  480. 			{
  481. 				List<string> errargs = new List<string>( errmsg );
  482. 				errargs.RemoveAt( 0 );
  483. 				Console.Error.WriteLine( );
  484. 				Console.ForegroundColor = ConsoleColor.Red;
  485. 				Console.Error.Write( "ERROR:\t" );
  486. 				Console.ForegroundColor = ConsoleColor.White;
  487. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  488. 				Console.ResetColor( );
  489. 			}
  490.  
  491. 			Console.Error.WriteLine( );
  492.  
  493. 			Console.Error.WriteLine( "PowerPoint2Any,  Version {0}", progver );
  494.  
  495. 			Console.Error.WriteLine( "Open a presentation in Microsoft PowerPoint and save it in \"any\" (known) format" );
  496.  
  497. 			Console.Error.WriteLine( );
  498.  
  499. 			Console.Error.Write( "Usage:     " );
  500. 			Console.ForegroundColor = ConsoleColor.White;
  501. 			Console.Error.WriteLine( "POWERPOINT2ANY  \"presentation\"  [ \"outfile\" ]  [ options ]" );
  502. 			Console.ResetColor( );
  503.  
  504. 			Console.Error.WriteLine( );
  505.  
  506. 			Console.Error.Write( "Where:     " );
  507. 			Console.ForegroundColor = ConsoleColor.White;
  508. 			Console.Error.Write( "\"presentation\"" );
  509. 			Console.ResetColor( );
  510. 			Console.Error.WriteLine( "  PowerPoint presentation(s) to be converted" );
  511.  
  512. 			Console.Error.WriteLine( "                           (wildcards allowed in file name, e.g. \"name*.pptx\")" );
  513.  
  514. 			Console.ForegroundColor = ConsoleColor.White;
  515. 			Console.Error.Write( "           \"outfile\"" );
  516. 			Console.ResetColor( );
  517. 			Console.Error.WriteLine( "       output file(s) to be created (wildcard \"*\" allowed" );
  518.  
  519. 			Console.Error.WriteLine( "                           for file name, e.g. \"*.odp\" or \"*.pdf\")" );
  520.  
  521. 			Console.Error.Write( "Options:   " );
  522. 			Console.ForegroundColor = ConsoleColor.White;
  523. 			Console.Error.Write( "/O" );
  524. 			Console.ResetColor( );
  525. 			Console.Error.Write( "              silently " );
  526. 			Console.ForegroundColor = ConsoleColor.White;
  527. 			Console.Error.Write( "O" );
  528. 			Console.ResetColor( );
  529. 			Console.Error.WriteLine( "verwrite existing output file(s)" );
  530.  
  531. 			Console.Error.WriteLine( "                           (default: abort or skip if output file exists)" );
  532.  
  533. 			Console.ForegroundColor = ConsoleColor.White;
  534. 			Console.Error.Write( "           /T" );
  535. 			Console.ResetColor( );
  536. 			Console.Error.Write( "              list accepted output file " );
  537. 			Console.ForegroundColor = ConsoleColor.White;
  538. 			Console.Error.Write( "T" );
  539. 			Console.ResetColor( );
  540. 			Console.Error.WriteLine( "ypes" );
  541.  
  542.  
  543. 			Console.ForegroundColor = ConsoleColor.White;
  544. 			Console.Error.Write( "           /T:type" );
  545. 			Console.ResetColor( );
  546. 			Console.Error.Write( "         set output file " );
  547. 			Console.ForegroundColor = ConsoleColor.White;
  548. 			Console.Error.Write( "T" );
  549. 			Console.ResetColor( );
  550. 			Console.Error.Write( "ype (required if " );
  551. 			Console.ForegroundColor = ConsoleColor.White;
  552. 			Console.Error.Write( "\"outfile\"" );
  553. 			Console.ResetColor( );
  554. 			Console.Error.WriteLine( " is not" );
  555.  
  556. 			Console.Error.Write( "                           specified; " );
  557. 			Console.ForegroundColor = ConsoleColor.White;
  558. 			Console.Error.Write( "type" );
  559. 			Console.ResetColor( );
  560. 			Console.Error.WriteLine( " may be number or string)" );
  561.  
  562. 			Console.Error.WriteLine( );
  563.  
  564. 			Console.Error.WriteLine( "Notes: [1] This program requires a \"regular\" (MSI based) Microsoft PowerPoint" );
  565.  
  566. 			Console.Error.WriteLine( "           (2007 or later) installation, it will fail on an MS Office" );
  567.  
  568. 			Console.Error.WriteLine( "           \"click-to-run\" installation" );
  569.  
  570. 			Console.Error.WriteLine( "       [2] For PowerPoint 2007, to save as PDF or XPS, this program requires" );
  571.  
  572. 			Console.Error.WriteLine( "           the \"Microsoft Save as PDF or XPS Add-in for 2007 Microsoft Office" );
  573.  
  574. 			Console.Error.WriteLine( "           programs\", available at:" );
  575.  
  576. 			Console.Error.WriteLine( "           http://www.microsoft.com/en-us/download/details.aspx?id=7" );
  577.  
  578. 			Console.Error.WriteLine( "       [3] If wildcards are used in the PowerPoint file names, and the output" );
  579.  
  580. 			Console.Error.Write( "           file path is not specified, " );
  581. 			Console.ForegroundColor = ConsoleColor.White;
  582. 			Console.Error.Write( "/T:type" );
  583. 			Console.ResetColor( );
  584. 			Console.Error.WriteLine( " must be used, and the input" );
  585.  
  586. 			Console.Error.WriteLine( "           file names should not contain dots." );
  587.  
  588. 			Console.Error.WriteLine( "       [4] If wildcards are used in the PowerPoint file names, and the output" );
  589.  
  590. 			Console.Error.Write( "           file path is specified, the output file " );
  591. 			Console.ForegroundColor = ConsoleColor.White;
  592. 			Console.Error.Write( "name" );
  593. 			Console.ResetColor( );
  594. 			Console.Error.WriteLine( " must be \"*\"." );
  595.  
  596. 			Console.Error.Write( "       [5] If wildcards are used in the PowerPoint file names, and the " );
  597. 			Console.ForegroundColor = ConsoleColor.White;
  598. 			Console.Error.WriteLine( "/O" );
  599. 			Console.ResetColor( );
  600.  
  601. 			Console.Error.WriteLine( "           switch is not used, the program will display an error message in" );
  602.  
  603. 			Console.Error.WriteLine( "           case an output file already exists, but it will then continue to" );
  604.  
  605. 			Console.Error.WriteLine( "           convert the next file instead of aborting." );
  606.  
  607. 			Console.Error.WriteLine( "       [6] If PowerPoint was already active when this program is started," );
  608.  
  609. 			Console.Error.WriteLine( "           any other opened presentation(s) will be left alone, and only" );
  610.  
  611. 			Console.Error.WriteLine( "           the presentation(s) opened by this program will be closed." );
  612.  
  613. 			Console.Error.WriteLine( );
  614.  
  615. 			Console.Error.Write( "Examples:  " );
  616. 			Console.ForegroundColor = ConsoleColor.White;
  617. 			Console.Error.WriteLine( "POWERPOINT2ANY \"D:\\folder\\myfile.ppt\" *.pdf" );
  618. 			Console.ResetColor( );
  619.  
  620. 			Console.Error.WriteLine( "           will save to \"D:\\folder\\myfile.pdf\"" );
  621.  
  622. 			Console.Error.WriteLine( );
  623.  
  624. 			Console.ForegroundColor = ConsoleColor.White;
  625. 			Console.Error.WriteLine( "           POWERPOINT2ANY \"D:\\folder\\myfile.pptx\" \"D:\\otherfolder\\*.odp\"" );
  626. 			Console.ResetColor( );
  627.  
  628. 			Console.Error.WriteLine( "           will save to \"D:\\otherfolder\\myfile.odp\"" );
  629.  
  630. 			Console.Error.WriteLine( );
  631.  
  632. 			Console.ForegroundColor = ConsoleColor.White;
  633. 			Console.Error.WriteLine( "           POWERPOINT2ANY \"D:\\folder\\myfile.pptx\" \"D:\\elsewhere\\newfile.xps\"" );
  634. 			Console.ResetColor( );
  635.  
  636. 			Console.Error.WriteLine( "           will save to \"D:\\elsewhere\\newfile.xps\"" );
  637.  
  638. 			Console.Error.WriteLine( );
  639.  
  640. 			Console.ForegroundColor = ConsoleColor.White;
  641. 			Console.Error.WriteLine( "           POWERPOINT2ANY \"D:\\folder\\name*.pptx\" *.html" );
  642. 			Console.ResetColor( );
  643.  
  644. 			Console.Error.WriteLine( "           will save all matching files as HTML to \"D:\\folder\\\"" );
  645.  
  646. 			Console.Error.WriteLine( "           recognized extensions: htm(l), odp, pdf, pps(x), ppt(x), xps" );
  647.  
  648. 			Console.Error.WriteLine( );
  649.  
  650. 			Console.ForegroundColor = ConsoleColor.White;
  651. 			Console.Error.WriteLine( "           POWERPOINT2ANY \"D:\\folder\\*.pptx\" /T:12" );
  652. 			Console.ResetColor( );
  653.  
  654. 			Console.Error.WriteLine( "           like previous example, but more file types available" );
  655.  
  656. 			Console.Error.WriteLine( );
  657.  
  658. 			Console.ForegroundColor = ConsoleColor.White;
  659. 			Console.Error.WriteLine( "           POWERPOINT2ANY /T" );
  660. 			Console.ResetColor( );
  661.  
  662. 			Console.Error.WriteLine( "           will list all accepted file types" );
  663.  
  664. 			Console.Error.WriteLine( );
  665.  
  666. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  667.  
  668. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  669.  
  670. 			#endregion Help Text
  671.  
  672. 			return 1;
  673. 		}
  674.  
  675.  
  676. 		// Returns -1 if folder not found, 0 if no matching files found, otherwise the matching file count
  677. 		static int ValidateFilespec( string filespec )
  678. 		{
  679. 			int matchingfiles = -1;
  680. 			try
  681. 			{
  682. 				string parentfolder = Directory.GetParent( filespec ).FullName;
  683. 				if ( Directory.Exists( parentfolder ) )
  684. 				{
  685. 					matchingfiles = 0;
  686. 					foreach ( string matchingfile in Directory.GetFiles( parentfolder, Path.GetFileName( filespec ) ) )
  687. 					{
  688. 						matchingfiles += 1;
  689. 					}
  690. 				}
  691. 			}
  692. 			catch { };
  693. 			return matchingfiles;
  694. 		}
  695. 	}
  696. }
  697.  

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