ai-business-write/批量转换doc到docx.bat

55 lines
1.8 KiB
Batchfile

@echo off
chcp 65001 >nul
echo ========================================
echo 批量转换 .doc 文件到 .docx 格式
echo ========================================
echo.
REM 检查是否安装了 Word
where winword.exe >nul 2>&1
if %errorlevel% neq 0 (
echo 错误: 未找到 Microsoft Word
echo 请确保已安装 Microsoft Word
pause
exit /b 1
)
echo 正在查找 .doc 文件...
echo.
REM 设置源目录和目标目录
set "SOURCE_DIR=模板\原始模板"
set "TARGET_DIR=模板"
REM 使用 PowerShell 脚本进行转换
powershell -ExecutionPolicy Bypass -Command ^
"$sourceDir = '%SOURCE_DIR%'; ^
$targetDir = '%TARGET_DIR%'; ^
$word = New-Object -ComObject Word.Application; ^
$word.Visible = $false; ^
Get-ChildItem -Path $sourceDir -Filter '*.doc' -Recurse | ForEach-Object { ^
$docxPath = $_.FullName -replace '\.doc$', '.docx'; ^
$relativePath = $_.FullName.Replace((Resolve-Path $sourceDir).Path + '\', ''); ^
$targetPath = Join-Path $targetDir $relativePath; ^
$targetPath = $targetPath -replace '\.doc$', '.docx'; ^
$targetFolder = Split-Path $targetPath -Parent; ^
if (-not (Test-Path $targetFolder)) { New-Item -ItemType Directory -Path $targetFolder -Force | Out-Null }; ^
Write-Host \"转换: $($_.Name) -> $(Split-Path $targetPath -Leaf)\"; ^
try { ^
$doc = $word.Documents.Open($_.FullName, $false, $true); ^
$doc.SaveAs2($targetPath, 16); ^
$doc.Close($false); ^
Write-Host \" ✓ 成功\" -ForegroundColor Green; ^
} catch { ^
Write-Host \" ✗ 失败: $_\" -ForegroundColor Red; ^
} ^
}; ^
$word.Quit(); ^
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null"
echo.
echo ========================================
echo 转换完成!
echo ========================================
pause