images and PDF compression

one batch for compress images and PDF. (for backup only)

@echo off
setlocal

if "%1"=="" (
    set /p "source_folder=Please enter the source folder path: "
) else (
    set "source_folder=%~1"
)

if not exist "%source_folder%" (
    echo Source folder does not exist.
    pause
    exit /b
)

set "quality=30"

echo Image compression in progress, please wait...

powershell -NoProfile -ExecutionPolicy Bypass -Command "& {param($SourceFolder, $Quality) Add-Type -AssemblyName System.Drawing; $destFolder = Join-Path $SourceFolder 'output'; if (!(Test-Path $destFolder)) { New-Item -ItemType Directory -Path $destFolder | Out-Null }; $images = Get-ChildItem -Path $SourceFolder -Include '*.jpg', '*.png' -Recurse; $qualityEncoder = [System.Drawing.Imaging.Encoder]::Quality; $encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1); $encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($qualityEncoder, $Quality); $images | ForEach-Object { $image = [System.Drawing.Image]::FromFile($_.FullName); if ($image -ne $null) { $outputPath = Join-Path $destFolder ($_.BaseName + '_compressed' + $_.Extension); if ($_.Extension -eq '.jpg') { $codec = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where-Object { $_.FormatDescription -eq 'JPEG' }; } elseif ($_.Extension -eq '.png') { $codec = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where-Object { $_.FormatDescription -eq 'PNG' }; }; $image.Save($outputPath, $codec, $encoderParams); $image.Dispose(); } } }" "%source_folder%" "%quality%" && echo Image compression completed.

explorer "%source_folder%\output"

REM PDF压缩部分
REM powershell -NoProfile -ExecutionPolicy Bypass -Command "& {param($SourceFolder) $destFolder = Join-Path $SourceFolder 'output'; $pdfs = Get-ChildItem -Path $SourceFolder -Include '*.pdf' -Recurse; $pdfs | ForEach-Object { $inputFile = $_.FullName; $outputFile = Join-Path $destFolder ($_.BaseName + '_compressed.pdf'); $reader = New-Object System.IO.FileStream($inputFile, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read); $document = New-Object System.IO.MemoryStream; $reader.CopyTo($document); $reader.Close(); $document.Position = 0; $writer = New-Object System.IO.FileStream($outputFile, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write); $document.WriteTo($writer); $writer.Close(); $document.Close(); } }" "%source_folder%" && echo PDF compression completed.
pause>nul
Wat L Wat L 2025年10月11日
    7天前 almost。。。, 高血压还是老实吃药,适量运动,降低血脂最安全。
    1. 没有了
    2. PowerShell - 列大文件or 文件夹
    1 of 4