Showing posts with label files. Show all posts
Showing posts with label files. Show all posts

Tuesday, August 23, 2016

Extract attachments and inline files from a MIME message

MimeMessage msg;
using (var stream = File.OpenRead(fileName))
{
    msg = MimeMessage.Load(stream);
}
foreach (var part in msg.BodyParts.OfType().Where(part => !string.IsNullOrEmpty(part.FileName)))
{
    using (var stream = File.OpenWrite(Path.Combine(Path.GetDirectoryName(fileName), part.FileName)))
    {
        part.ContentObject.DecodeTo(stream);
    }
}
MimeKit

Sunday, November 24, 2013

Recursively search and replace files from one folder into another

$filter = '*.dll', '*.pdb'
$copied = 0; $skipped = 0; $failed = 0
$sourceFiles = $filter | % { Get-ChildItem $sourceDir -Filter $_ -Recurse } `
                       | sort @{expression={$_.FullName.Length}} -Descending `
                       | group Name -AsHashTable
$targetFiles = $filter | % { Get-ChildItem $targetDir -Filter $_ -Recurse } `
                       | ? { $sourceFiles.ContainsKey($_.Name) } `
                       | sort FullName
$targetFiles | % {
    $target = $_
    $sourceFiles[$target.Name] | % {
        $source = $_
        if ($target.FullName.EndsWith($source.FullName.Substring($sourceDir.Length),
                                      'InvariantCultureIgnoreCase')) {
            if ($target.LastWriteTime.AddSeconds(2) -lt $source.LastWriteTime) {
                Write-Host $target.FullName
                try {
                    Copy-Item $source.FullName $target.FullName -Force
                    $copied++
                }
                catch {
                    Write-Error $_.Message
                    $failed++
                }
            }
            else {
                $skipped++
            }
            return
        }
    }
}
Write-Host "$copied copied, $skipped skipped, $failed failed in $($watch.Elapsed)"