VBA(Accessデータベース内またはExcelマクロのような外部)で実行できるAccessの専用CompactRepairメソッドを使用することを検討してください。注意すべき点は、Compact &です。実際には、既存のデータベースのコピーが作成され、元のデータベースに置き換えられるため、ファイル処理が必要になります。
VBA(MSACCESS.EXE内側)
Sub RunCompactDBs()
Dim path As String
Dim accfile As Variant
path = "C:\Databases\"
accfile = Dir(path & "*.accdb", vbDirectory)
Do While Len(accfile) > 0
bkfile = Replace(accfile, ".accdb", "_bk.accdb")
' CREATE COMPACTED BACKUP
Application.CompactRepair path & accfile, path & bkfile, False
' COPY TO ORIGINAL PATH
FileCopy path & bkfile, path & accfile
' DESTROY COMPACTED BACKUP
Kill path & bkfile
accfile = Dir
Loop
Set accApp = Nothing
End Sub
VBA
Sub RunCompactDBs()
Dim path As String
Dim accfile As Variant
Dim accApp As Object
Set accApp = CreateObject("Access.Application")
path = "C:\Databases\"
accfile = Dir(path & "*.accdb", vbDirectory)
Do While Len(accfile) > 0
bkfile = Replace(accfile, ".accdb", "_bk.accdb")
accApp.CompactRepair path & accfile, path & bkfile, False
FileCopy path & bkfile, path & accfile
Kill path & bkfile
accfile = Dir
Loop
Set accApp = Nothing
End Sub
(MSACCESS.EXE外)とに固執する理由はありませんVBA。
Pythonの
import os, glob, shutil
import win32com.client
# LAUNCH ACCESS APP
oApp = win32com.client.Dispatch("Access.Application")
for file in glob.glob("C:\\Databases\\*.accdb"):
bkfile = file.replace(".accdb", "_bk.accdb")
oApp.CompactRepair(file, bkfile, False)
shutil.copyfile(bkfile, file)
os.remove(bkfile)
oApp = None
R
library(RDCOMClient)
# LAUNCH ACCESS APP
oApp = COMCreate("Access.Application")
accfiles <- list.files(path="C:\\Databases\\", pattern="\\.accdb", full.names=TRUE)
for (file in accfiles){
bkfile = sub(".accdb", "_bk.accdb", file)
oApp$CompactRepair(file, bkfile, FALSE)
file.copy(bkfile, file, overwrite = TRUE)
file.remove(bkfile)
}
oApp <- NULL
gc()
:AccessオブジェクトライブラリへのCOMインターフェイスを作ることができる任意の言語は、オープンソースの言語のようにコンパクトと修復手順を実行することができますPHP
# LAUNCH ACCESS APP
$acc = new COM("Access.Application", NULL, CP_UTF8) or Die ("Did not instantiate Access");
foreach (glob("C:\\Databases\\*.accdb") as $file) {
$bkfile = str_replace(".accdb", "_bk.accdb", $file);
$acc->Application->CompactRepair($file, $bkfile, false);
copy($bkfile, $file);
unlink($bkfile);
}
$acc = NULL;
unset($acc);
http://ss64.com/nt/forfiles.htmlがおそらく最も簡単な方法です。 – Andre
または標準の['for'ループ](http://ss64.com/nt/for.html)... – aschipfl