查找所有Access数据库,以及压缩和修复 [英] Find all Access databases, and Compact and Repair

查看:91
本文介绍了查找所有Access数据库,以及压缩和修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在目录中查找所有Access数据库,并且每次都压缩和修复而不每次都打开Access.我发现以下文章解释了如何找到它们并将其写入文件: 用于查找所有Access数据库的批处理文件,但理想情况下,我想只是找到它们并压缩而不写入文件.

I'm trying to find all Access databases in a directory and Compact and Repair each without opening Access each time. I found the following article explaining how to find them and write to a file: Batch file to find all Access Databases, but ideally I'd like to just find them and compact without writing to a file.

我已经搜索了如何调用/compact命令行功能,但是我不知道如何在找到的数据库上执行该操作.可以写入.bat文件来执行此操作吗?像这样:

I have searched how to call the /compact command line functionality, but I don't know how to do it on the databases I find. Can a .bat file be written to do this? Something like:

@echo off

"C:\Program Files\Microsoft Office\Office14\MSACCESS.EXE" "C:\Databases\ /s" *.accdb /compact

非常感谢您提供有关语法的帮助.

Any help with the syntax is very much appreciated.

推荐答案

考虑使用Access专用的 CompactRepair 方法,您可以在VBA中运行该方法(在Access数据库内部或外部,例如在Excel宏中).要注意的是紧凑型和紧凑型.修复实际上会创建一个现有数据库的副本并将其替换为原始数据库,因此需要一些文件处理.

Consider using Access' dedicated CompactRepair method which you can run in VBA (inside an Access database or outside like in an Excel macro). The thing to note is Compact & Repair actually creates a copy of existing database and replaces it with original, so some file handling is needed.

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 (MSAccess.exe外部)

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


没有理由坚持使用VBA.可以与Access对象库建立COM接口的任何语言都可以运行紧凑和修复过程,例如开源语言:


And there's no reason to stick with VBA. Any language that can make a COM interface to the Access object library can run the compact and repair procedure like open-source languages:

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()

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);

这篇关于查找所有Access数据库,以及压缩和修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆