-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from stephenjannin/master
PstFileAttachmentExporter : Export all file attachments from command line
- Loading branch information
Showing
8 changed files
with
248 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PstFileAttachmentExporter", "PstFileAttachmentExporter\PstFileAttachmentExporter.csproj", "{9F60A682-39DE-4E68-B44D-05254DBDEB7F}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{9F60A682-39DE-4E68-B44D-05254DBDEB7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{9F60A682-39DE-4E68-B44D-05254DBDEB7F}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{9F60A682-39DE-4E68-B44D-05254DBDEB7F}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{9F60A682-39DE-4E68-B44D-05254DBDEB7F}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
using XstReader; | ||
|
||
namespace PstFileExporter | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
if (args.Length != 1) | ||
{ | ||
Console.WriteLine("Export all attachments from a given pst file"); | ||
Console.WriteLine("- Give the path of a pst file as first argument"); | ||
Console.WriteLine("- All attachments will be extracted in the same directory, respecting the directory structure inside the pst"); | ||
return; | ||
} | ||
|
||
var fileName = args[0]; | ||
var exportDirectory = CreateDirectoryIfNeeded(System.IO.Path.GetDirectoryName(fileName), | ||
System.IO.Path.GetFileNameWithoutExtension(fileName) + "_Export"); | ||
|
||
var xstView = new XstReader.View(); | ||
var xstFile = new XstReader.XstFile(xstView, fileName); | ||
xstFile.ReadFolderTree(); | ||
foreach (var folder in xstView.RootFolders) | ||
{ | ||
ExtractAttachmentsInFolder(xstView, xstFile, folder, exportDirectory); | ||
} | ||
|
||
Console.WriteLine("Done!"); | ||
|
||
} | ||
|
||
private static string CreateDirectoryIfNeeded(string rootDirName, string dirName) | ||
{ | ||
string exportDirectory = System.IO.Path.Combine(rootDirName, | ||
RemoveInvalidChars(System.IO.Path.GetFileName(dirName))); | ||
if (!System.IO.Directory.Exists(exportDirectory)) | ||
System.IO.Directory.CreateDirectory(exportDirectory); | ||
return exportDirectory; | ||
} | ||
|
||
|
||
|
||
private static string RemoveInvalidChars(string filename) | ||
{ | ||
return string.Concat(filename.Split(System.IO.Path.GetInvalidFileNameChars())).TrimEnd().TrimEnd('.'); | ||
} | ||
|
||
private static void ExtractAttachmentsInFolder(View xstView, XstFile xstFile, XstReader.Folder folder, string exportDirectoryBase) | ||
{ | ||
var exportDirectory = CreateDirectoryIfNeeded(exportDirectoryBase, RemoveInvalidChars(folder.Name)); | ||
|
||
xstFile.ReadMessages(folder); | ||
foreach (var message in folder.Messages) | ||
{ | ||
xstFile.ReadMessageDetails(message); | ||
foreach(var att in message.Attachments) | ||
{ | ||
if (att.IsFile) | ||
{ | ||
var attachmentExpectedName = System.IO.Path.Combine(exportDirectory, att.FileName); | ||
var fi = new System.IO.FileInfo(attachmentExpectedName); | ||
var actionName = string.Empty; | ||
|
||
if (!fi.Exists) | ||
{ | ||
actionName = "Create"; | ||
} else | ||
{ | ||
if (fi.CreationTime < message.Received) | ||
{ | ||
actionName = "CreateNewer"; | ||
} | ||
else { | ||
actionName = "Skip"; | ||
} | ||
} | ||
Console.WriteLine(String.Format("{0} : {1}" , actionName, attachmentExpectedName)); | ||
switch (actionName) | ||
{ | ||
case "Create": | ||
case "CreateNewer": | ||
xstFile.SaveAttachment(attachmentExpectedName, message.Received, att); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
foreach(var subFolder in folder.Folders) | ||
{ | ||
ExtractAttachmentsInFolder(xstView, xstFile, subFolder, exportDirectory); | ||
} | ||
|
||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
PstFileAttachmentExporter/PstFileAttachmentExporter.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\BTree.cs"> | ||
<Link>XstReader\BTree.cs</Link> | ||
</Compile> | ||
<Compile Include="..\XstFile.cs"> | ||
<Link>XstReader\XstFile.cs</Link> | ||
</Compile> | ||
<Compile Include="..\View.cs"> | ||
<Link>XstReader\View.cs</Link> | ||
</Compile> | ||
<Compile Include="..\StandardProperties.cs"> | ||
<Link>XstReader\StandardProperties.cs</Link> | ||
</Compile> | ||
<Compile Include="..\RtfDecompressor.cs"> | ||
<Link>XstReader\RtfDecompressor.cs</Link> | ||
</Compile> | ||
<Compile Include="..\Property.cs"> | ||
<Link>XstReader\Property.cs</Link> | ||
</Compile> | ||
<Compile Include="..\NamedProperties.cs"> | ||
<Link>XstReader\NamedProperties.cs</Link> | ||
</Compile> | ||
<Compile Include="..\NDB.cs"> | ||
<Link>XstReader\NDB.cs</Link> | ||
</Compile> | ||
<Compile Include="..\Message.cs"> | ||
<Link>XstReader\Message.cs</Link> | ||
</Compile> | ||
<Compile Include="..\Map.cs"> | ||
<Link>XstReader\Map.cs</Link> | ||
</Compile> | ||
<Compile Include="..\LayoutsU4K.cs"> | ||
<Link>XstReader\LayoutsU4K.cs</Link> | ||
</Compile> | ||
<Compile Include="..\LayoutsU.cs"> | ||
<Link>XstReader\LayoutsU.cs</Link> | ||
</Compile> | ||
<Compile Include="..\LayoutsA.cs"> | ||
<Link>XstReader\LayoutsA.cs</Link> | ||
</Compile> | ||
<Compile Include="..\Layouts.cs"> | ||
<Link>XstReader\Layouts.cs</Link> | ||
</Compile> | ||
<Compile Include="..\LTP.cs"> | ||
<Link>XstReader\LTP.cs</Link> | ||
</Compile> | ||
<Compile Include="..\Extensions.cs"> | ||
<Link>XstReader\Extensions.cs</Link> | ||
</Compile> | ||
<Compile Include="..\Crypto.cs"> | ||
<Link>XstReader\Crypto.cs</Link> | ||
</Compile> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters