5
$\begingroup$

I used to find the extensions mapping from System`ConvertersDump`$extensionMappings, which gives a mapping like "{"*.3ds" -> "3DS"...}". This seems in V13 it is removed, though some related contexts remain defined with no results:

Select[Names["System`*`*"], 
 StringMatchQ[#, "*extension*", IgnoreCase -> True] &]

(* {"System`Dump`ensureLibraryFileExtension", 
    "System`Dump`extension", 
    "System`ConvertersDump`extensions", 
    "System`ConvertersDump`extensions$", 
    "System`Dump`extension$", 
    "System`ConvertersDump`Utilities`Private`fileExtension", 
    "System`ConvertersDump`fileExtensions", 
    "System`FEDump`forceFileExtension", 
    "System`ConvertersDump`GetExtensionMappings", 
    "System`Dump`LibraryExtension"} *)

I checked all the above contexts, and none gives what I want.

$\endgroup$

2 Answers 2

4
$\begingroup$

I haven't found extension mappings in the System context, but here's a way to build mappings of file extentions and file formats that shouldn't be affected by version updates.

  • Get file formats from $ImportFormats and $ExportFormats.

  • The function FileFormatProperties (introduced in v13) matches file formats to file extensions (FileNamePatterns). Some file formats have no matching FileNamePatterns so we remove them.

Make separate maps for Import Formats and Export Formats:

imMap = Cases[FileFormatProperties[#, "FileNamePatterns"] -> # &/@
  $ImportFormats, (e_ -> f_)/; Length[e] > 0];
(*{{*.3ds} -> 3DS, {*.7z} -> 7z, {*.aco} -> ACO, <<193>>,
   {*.xyz} -> XYZ, {*.zip, *.jar} -> ZIP, {*.zst} -> ZSTD}*)

exMap = Cases[FileFormatProperties[#, "FileNamePatterns"] -> # &/@
  $ExportFormats, (e_ -> f_)/; Length[e] > 0];
(*{{*.3ds} -> 3DS, {*.aco} -> ACO, {*.aif, *.aiff} -> AIFF, <<151>>,
   {*.zip, *.jar} -> ZIP, {*.zpr} -> ZPR, {*.zst} -> ZSTD}*)

The Import and Export maps could be combined, but test for conflicts.

Many file formats have multiple extensions, for example, {*.zip, *.jar} -> ZIP. Some extensions are used by multiple formats, one example: *.json is used by ExpressionJSON, GeoJSON, JSON, and RawJSON file formats.

$\endgroup$
1
  • 1
    $\begingroup$ The internal variables are internal for a reason and are not guaranteed to stay the same. FileFormatProperties is the correct and documented way to get extension mappings. $\endgroup$ Aug 24, 2023 at 22:26
3
$\begingroup$

By checking the output of

Export["a.txt", Export["a.txt",] // Trace] // SystemOpen

I find the variable

FileFormatDump`$INTERNALEXTENSIONMAPPINGS 

has been used instead of

System`ConvertersDump`$extensionMappings

in Export/Import at least since v13.3. The variable FileFormatDump`$INTERNALEXTENSIONMAPPINGS outputs an Association(<||>) and has been a built-in variable at least since v12.3, BTW.

$\endgroup$
1
  • 1
    $\begingroup$ Those internal details are always subject to change. the correct way to do things is use FileFormatProperties. $\endgroup$ Aug 24, 2023 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.