【问题标题】:MS Access VBA lookup image file names from table, search for them and copy themMS Access VBA 从表中查找图像文件名,搜索并复制它们
【发布时间】:2016-12-24 02:03:21
【问题描述】:

我使用的是 Access 2013。我在表格中有一个图像列表。然后我需要搜索一组文件夹(包括子文件夹),找到图像并将它们复制到新目录。包含图像名称的表不引用文件路径。

实现这一目标的最佳方法是什么?是否可以遍历表并执行搜索,或者我是否可以将其分解并手动定位每个文件并更新一个标志以表明它存在,然后返回并复制它们?

不确定如何执行此操作,但希望有任何想法。

谢谢。

【问题讨论】:

  • 文件系统对象+循环遍历记录集。
  • 好的,我试试看

标签: ms-access vba


【解决方案1】:

对于某些导入程序,我必须操作文件、创建副本等,并且我创建了一些函数来帮助处理,您可能会发现它们有些用处:

从 VBA 创建文件夹:

Public Function folderCreate(filePath As String) As Boolean
    'define variables
    Dim fsoFold As Object

    'set file system object
    Set fsoFold = CreateObject("Scripting.FileSystemObject")

    If Not fsoFold.folderExists(filePath) Then
        'check if folder exists
        fsoFold.CreateFolder (filePath)
    End If

    folderCreate = True

    Set fsoFold = Nothing

End Function

检查文件夹是否存在:

Public Function folderExists(folderPath As String) As Boolean
    'define variables
    Dim fso As Object

    'set file system object
    Set fso = CreateObject("Scripting.FileSystemObject")

    'check if file exists
    If fso.folderExists(folderPath) Then
        folderExists = True
    Else
        folderExists = False
    End If

    Set fso = Nothing

End Function

检查文件是否存在:

Public Function fileExists(filePath As String) As Boolean
     'define variables
     Dim fso As Object

    'set file system object
    Set fso = CreateObject("Scripting.FileSystemObject")

    'check if file exists
    If fso.fileExists(filePath) Then
        fileExists = True
    Else
        fileExists = False
    End If

    Set fso = Nothing

End Function

与此类似,使用 movefile 将其移动到新位置。

fso.movefile strFullPath, strFullBackUp

编辑:下面的 sub 将遍历给定文件夹并列出所有 JPG 图像 - 此代码只是示例如何查找文件、文件夹以及如何递归遍历它们。

Public Sub listImages(folderPath As String)

    'define variables
    Dim fso As Object
    Dim objFolder As Object
    Dim objFolders As Object
    Dim objF As Object
    Dim objFile As Object
    Dim objFiles As Object
    Dim strFileName As String
    Dim strFilePath As String
    Dim myList As String

    'set file system object
    Set fso = CreateObject("Scripting.FileSystemObject")

    'set folder object
    Set objFolder = fso.GetFolder(folderPath)

    'set files
    Set objFiles = objFolder.files
    Set objFolders = objFolder.subfolders

    'list all images in folder
    For Each objFile In objFiles

        If Right(objFile.Name, 4) = ".jpg" Then
            strFileName = objFile.Name
            strFilePath = objFile.Path
            myList = myList & strFileName & " - " & strFilePath & vbNewLine
        End If

    Next

    'go through all subflders
    For Each objF In objFolders

        'call same procedure for each subfolder
        Call listImages(objF.Path)

     Next

     Debug.Print myList

     Set objFolder = Nothing
     set objFolders = Nothing
     Set objFile = Nothing
     set objF = Nothing
     Set fso = Nothing


End Sub

【讨论】:

  • “文件存在”功能是否也搜索子文件夹?我需要这样做。
  • 文件存在正在查看文件是否存在并提供完整路径。像 fileExists("c:\test\image.jpg") - 只有当 C 盘上的 test 文件夹中有 image.jpg 时才会返回 true。
  • 我还需要搜索“c:\test\”中的所有子文件夹。
  • 您是否只有文件名,或者您想列出文件夹和子文件夹中的所有 JPG 文件?
  • 他们需要从表中查找图像名称,但也需要通配符搜索
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-10
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多