Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) | Reguli | Inregistrare | Login

POZE MRRRR'S FORUM (VIEW ONLY)

Nu sunteti logat.
Nou pe simpatie:
Andreea_K
Femeie
25 ani
Caras Severin
cauta Barbat
30 - 49 ani
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides / [OUTLOOK] Extract All Attachments from EML Files in Current Folder Moderat de TRaP, TonyTzu
Autor
Mesaj Pagini: 1
Mrrrr
AdMiN

Inregistrat: acum 19 ani
Postari: 2343
The ps1 script below, ran by using a small bat script, will extract all attachments of all the eml files inside the same folder where the ps1+bat scripts are, to subfolders named after the eml files names.

First, the bat file as it is shorter:

@echo off
powershell -ExecutionPolicy Bypass -File "%~dp0Extract-EML-Attachments_to_subfolders.ps1"
pause

Now ps1 file named Extract-EML-Attachments_to_subfolders

$folder = Get-Location
Write-Host "Scanning folder: $folder"

Get-ChildItem -Filter *.eml | ForEach-Object {

    $emlName = $_.BaseName
    Write-Host "`nProcessing: $($_.Name)"

    # Create a subfolder for this email
    $emailFolder = Join-Path $folder $emlName
    New-Item -ItemType Directory -Force -Path $emailFolder | Out-Null

    $text = Get-Content $_.FullName -Raw

    # Detect MIME boundary
    if ($text -match 'boundary="?([^"\r\n;]+)') {
        $boundary = $matches[1]
    } else {
        Write-Host "No MIME boundary found, skipping..."
        return
    }

    $parts = $text -split "--$boundary"

    foreach ($part in $parts) {

        if ($part -match 'filename="?([^"\r\n]+)"?') {

            $filename = $matches[1]

            $bodySplit = $part -split "\r?\n\r?\n", 2
            if ($bodySplit.Count -lt 2) { continue }

            $data = $bodySplit[1].Trim()

            try {
                $clean = ($data -replace "\s","")
                $bytes = [Convert]::FromBase64String($clean)

                # Determine final path
                $path = Join-Path $emailFolder $filename
                if (Test-Path $path) {
                    # Add timestamp in milliseconds if duplicate
                    $timestamp = Get-Date -Format "yyyyMMdd-HHmmss-fff"
                    $name = [System.IO.Path]::GetFileNameWithoutExtension($filename)
                    $ext = [System.IO.Path]::GetExtension($filename)
                    $path = Join-Path $emailFolder ("$name" + "_" + $timestamp + $ext)
                }

                [IO.File]::WriteAllBytes($path, $bytes)
                Write-Host "Extracted: $([System.IO.Path]::GetFileName($path))"

            } catch {
                Write-Host "Skipped $filename (not base64)"
            }
        }
    }
}

Write-Host "`nDone extracting all attachments!"

Source: ChatGPT


_______________________________________


pus acum 2 saptamani
   
Mrrrr
AdMiN

Inregistrat: acum 19 ani
Postari: 2343
Ps1 below to extract all attachments to current folder (no subfolders):

$folder = Get-Location

Write-Host "Scanning $folder"

Get-ChildItem -Filter *.eml | ForEach-Object {

    Write-Host "`nProcessing $($_.Name)"

    $text = Get-Content $_.FullName -Raw

    if ($text -match 'boundary="?([^"\r\n;]+)') {
        $boundary = $matches[1]
    } else {
        Write-Host "No MIME boundary found"
        return
    }

    $parts = $text -split "--$boundary"

    foreach ($part in $parts) {

        if ($part -match 'filename="?([^"\r\n]+)"?') {

            $filename = $matches[1]

            $bodySplit = $part -split "\r?\n\r?\n",2
            if ($bodySplit.Count -lt 2) { continue }

            $data = $bodySplit[1].Trim()

            try {

                $clean = ($data -replace "\s","")
                $bytes = [Convert]::FromBase64String($clean)

                $path = Join-Path $folder $filename
                [IO.File]::WriteAllBytes($path,$bytes)

                Write-Host "Extracted $filename"

            } catch {
                Write-Host "Skipped $filename (not base64)"
            }
        }
    }
}


_______________________________________


pus acum 2 saptamani
   
Pagini: 1  

Mergi la