Загрузка данных


[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# Пути (скрипт ищет папки там же, где лежит сам)
$scriptPath = $PSScriptRoot
if (!$scriptPath) { $scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition }

$oldDir = Join-Path $scriptPath "Old_Doc_Files"
$convertedDir = Join-Path $scriptPath "Converted_Docx_Files"

# Проверка папки-источника
if (!(Test-Path $oldDir)) {
    Write-Host "[!] Папка с исходниками '$oldDir' не найдена!" -ForegroundColor Red
    Read-Host "Нажми Enter"; exit
}

# Создаем папку для результата
if (!(Test-Path $convertedDir)) { New-Item -ItemType Directory -Path $convertedDir | Out-Null }

$files = Get-ChildItem -Path $oldDir -Filter "*.doc"

if ($files.Count -eq 0) {
    Write-Host "В папке Old_Doc_Files нет файлов .doc для обработки." -ForegroundColor Yellow
    Read-Host "Нажми Enter"; exit
}

Write-Host "Найдено файлов для конвертации: $($files.Count)" -ForegroundColor Cyan

try {
    $word = New-Object -ComObject Word.Application
    $word.Visible = $false
    $i = 0

    foreach ($file in $files) {
        $i++
        $newPath = Join-Path $convertedDir ($file.BaseName + ".docx")
        
        Write-Host "[$i/$($files.Count)] Конвертирую: $($file.Name)" -ForegroundColor White
        
        $doc = $word.Documents.Open($file.FullName)
        # 16 = формат .docx
        $doc.SaveAs([ref]$newPath, [ref]16)
        $doc.Close()
    }
    
    Write-Host "`n--- ГОТОВО! ---" -ForegroundColor Green
    Write-Host "Все .docx лежат здесь: $convertedDir"
} catch {
    Write-Host "`n[!] Ошибка: $($_.Exception.Message)" -ForegroundColor Red
} finally {
    if ($word) {
        $word.Quit()
        [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
    }
}

Read-Host "`nГотово, нажми Enter"