Эти команды PowerShell помогут вам управлять файлами, директориями, службами и получать основную информацию о системе. Примеры подходят как для начинающих, так и для опытных пользователей.
$sourceFolder = "E:\DocumenteWord"
$destFolder = "E:\DocumentePDF"
if (!(Test-Path $destFolder)) {
New-Item -ItemType Directory -Path $destFolder | Out-Null
}
$wdFormatPDF = 17
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$extensions = @("*.doc", "*.docx")
foreach ($ext in $extensions) {
foreach ($file in Get-ChildItem -Path $sourceFolder -Filter $ext) {
try {
$pdfPath = Join-Path $destFolder ($file.BaseName + ".pdf")
Write-Host "Convertim: $($file.FullName) -> $pdfPath"
$doc = $word.Documents.Open($file.FullName, $false, $true)
# Conversie cu string .NET pur
$doc.SaveAs([string]$pdfPath, [ref]$wdFormatPDF)
$doc.Close($false)
}
catch {
Write-Host "Eroare la fișierul: $($file.FullName) - $($_.Exception.Message)" -ForegroundColor Red
}
}
}
$word.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
Write-Host "Conversia a fost finalizată!"
Комментариев нет:
Отправить комментарий