File extension parameters do include a dot
Posted by jpluimers on 2011/07/20
This is from a long time ago, but still fun:
Sometimes simple things in life are hard do remember.
For instance, I always forgot if a file extension parameter should have a dot in it or not.
Normally it should!
But for clearing an extension, you should use a blank string.
Be aware though that empty extensions look differently depending where in the process you look at them:
C# example:
using System; using System.IO; public class Test { public static void Main() { string extensionLess = Path.ChangeExtension(@"C:\mydir\myfile.com.extension", ""); Console.WriteLine(extensionLess); string extension = Path.GetExtension(extensionLess); Console.WriteLine(extension); } }
Outputs:
C:\mydir\myfile.com.
Delphi example:
program Demo; {$APPTYPE CONSOLE} uses SysUtils; var extensionLess: string; extension: string; begin extensionLess := ChangeFileExt('C:\mydir\myfile.com.extension', ''); Writeln(extensionLess); extension := ExtractFileExt(extensionLess); Writeln(extension); end.
Outputs:
C:\mydir\myfile.com .com
Don’t you love differences in your platforms :)
–jeroen
petermoor said
Interesting indeed…
Maybe, if file systems would be re-designed completely from scratch, the extension (or let’s say “file type”) should be a completely different field from its name. When you think about it, it’s actually quite silly that we have to parse a name to retrieve the file type, completely based on conventions. And it makes room for problems like this one.
jpluimers said
Actually, back in the DOS 8.3, era it was a field,and not a convention ;-)
And sometimes the file extensions isn’t all there is; for instance an .xml file can be just XML, but add a [?mso-application progid=”Excel.Sheet”?] (replace [] by angle brackets) in the second line, and the Windows Explorer happily opens it using Excel.
–jeroen
Pawel said
Interesting. Shouldn’t the output from the C# example still have two lines?
jpluimers said
It does; the second line is empty.