1

我可以找到示例 VBA 和 C# 代码来执行此操作,但没有Powershell 片段(谷歌对此保持沉默,Stackoverflow 也是如此)。谁能指出我这样做的一些Powershell(或提供一些)?我期待它会通过 COM 访问 Access 的 API。

谢谢!

4

1 回答 1

1

我应该知道最好不要在圣诞节前一天询问并希望得到快速响应,所以我自己做了。事实证明,无论如何,答案都非常简单。下面的顶部函数完成了我需要的操作,驱动程序函数将遍历目录中的所有访问数据库并压缩它们中的每一个。

function compactDatabaseFile($sourceFilename, $destinationFileName)
{

    $application =  New-Object -ComObject Access.Application

    Write-Host $($(Get-Date).ToString() + ": Starting compacting of $sourceFilename to $destinationFileName")

    $application.CompactRepair($sourceFilename,$destinationFileName, $true)

    Write-Host $($(Get-Date).ToString() + ": Finished compacting of $sourceFilename to $destinationFileName")

    $application.Quit()
}

function driver($sourceDirectory, $newDestinationDirectory, $filePatterns = @("*.mdb","*.accdb"))
{
    if (Test-Path $newDestinationDirectory) {
        Write-Host "Destination Directory $newDestinationDirectory exists.  Exiting without doing anything."
        Exit
    }

    if (!$(Test-Path $sourceDirectory)) {
        Write-Host "SourceDirectory $sourceDirectory not found.  Exiting without doing anything."
        Exit
    }

    mkdir $newDestinationDirectory | Out-Null

    gci -path $($sourceDirectory + "\*") -include $filePatterns | % {
        $sourceFileName = $($sourceDirectory + "\" + $_.Name);
        $destinationFileName = $($newDestinationDirectory + "\" + $_.Name);
        compactDatabaseFile $sourceFileName $destinationFilename
    }
}
于 2012-12-24T21:24:32.793 回答