VBA在字段中输出先前的文件夹名称

PMcMillen

我在弄清楚如何获取文件夹名称以输出到MS Access中的单独字段时遇到了麻烦。到目前为止,我已经设法使用此Allen Browne脚本并对其进行了调整,以将文件名和路径作为一个字段输出。

此搜索中的所有文件都包含在子文件夹中,对于我而言,在单独的字段中输出最后一个子文件夹名称很重要。像这样:示例表

这是代码:

Option Compare Database
Option Explicit

'list files to tables
'http://allenbrowne.com/ser-59alt.html

Dim gCount As Long ' added by Crystal

Sub runListFiles()
    'Usage example.
    Dim strPath As String _
    , strFileSpec As String _
    , booIncludeSubfolders As Boolean

    strPath = "H:\Pictures\2019"
    strFileSpec = "*.*"
    booIncludeSubfolders = True

    ListFilesToTable strPath, strFileSpec, booIncludeSubfolders
End Sub

'crystal modified parameter specification for strFileSpec by adding default value
Public Function ListFilesToTable(strPath As String _
    , Optional strFileSpec As String = "*.*" _
    , Optional bIncludeSubfolders As Boolean _
    )
On Error GoTo Err_Handler
    'Purpose:   List the files in the path.
    'Arguments: strPath = the path to search.
    '           strFileSpec = "*.*" unless you specify differently.
    '           bIncludeSubfolders: If True, returns results from subdirectories of strPath as well.
    'Method:    FilDir() adds items to a collection, calling itself recursively for subfolders.

    Dim colDirList As New Collection
    Dim varitem As Variant
    Dim rst As DAO.Recordset

   Dim mStartTime As Date _
      , mSeconds As Long _
      , mMin As Long _
      , mMsg As String

   mStartTime = Now()
   '--------

    Call FillDirToTable(colDirList, strPath, strFileSpec, bIncludeSubfolders)

   mSeconds = DateDiff("s", mStartTime, Now())

   mMin = mSeconds \ 60
   If mMin > 0 Then
      mMsg = mMin & " min "
      mSeconds = mSeconds - (mMin * 60)
   Else
      mMsg = ""
   End If

   mMsg = mMsg & mSeconds & " seconds"

   MsgBox "Done adding " & Format(gCount, "#,##0") & " files from " & strPath _
      & IIf(Len(Trim(strFileSpec)) > 0, " for file specification --> " & strFileSpec, "") _
      & vbCrLf & vbCrLf & mMsg, , "Done"

Exit_Handler:
   SysCmd acSysCmdClearStatus
   '--------

    Exit Function

Err_Handler:
    MsgBox "Error " & Err.Number & ": " & Err.Description, , "ERROR"

    'remove next line after debugged -- added by Crystal


    Resume Exit_Handler
End Function

Private Function FillDirToTable(colDirList As Collection _
    , ByVal strFolder As String _
    , strFileSpec As String _
    , bIncludeSubfolders As Boolean)

    'Build up a list of files, and then add add to this list, any additional folders
    On Error GoTo Err_Handler

    Dim strTemp As String
    Dim colFolders As New Collection
    Dim vFolderName As Variant
    Dim strSQL As String

    'Add the files to the folder.
    strFolder = TrailingSlash(strFolder)
    strTemp = Dir(strFolder & strFileSpec)
    Do While strTemp <> vbNullString
         gCount = gCount + 1
         SysCmd acSysCmdSetStatus, gCount
         strSQL = "INSERT INTO Files " _
          & " (FPath) " _
          & " SELECT """ & strFolder & """" _
          & "& """ & strTemp & """;"
         CurrentDb.Execute strSQL
        colDirList.Add strFolder & strTemp
        strTemp = Dir
    Loop

    If bIncludeSubfolders Then
        'Build collection of additional subfolders.
        strTemp = Dir(strFolder, vbDirectory)
        Do While strTemp <> vbNullString
            If (strTemp <> ".") And (strTemp <> "..") Then
                If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0& Then
                    colFolders.Add strTemp
                End If
            End If
            strTemp = Dir
        Loop
        'Call function recursively for each subfolder.
        For Each vFolderName In colFolders
            Call FillDirToTable(colDirList, strFolder & TrailingSlash(vFolderName), strFileSpec, True)
        Next vFolderName
    End If

Exit_Handler:

    Exit Function

Err_Handler:
    strSQL = "INSERT INTO Files " _
    & " (FPath) " _
    & " SELECT ""  ~~~ ERROR ~~~""" _
    & ", """ & strFolder & """;"
    CurrentDb.Execute strSQL

    Resume Exit_Handler
End Function

Public Function TrailingSlash(varIn As Variant) As String
    If Len(varIn) > 0& Then
        If Right(varIn, 1&) = "\" Then
            TrailingSlash = varIn
        Else
            TrailingSlash = varIn & "\"
        End If
    End If
End Function
六月7

声明一个新变量:
Dim strLocation As String

设置变量并修改您的SQL语句。

    'Add the files to the folder.
    strLocation = Mid(strFolder, InStrRev(strFolder, "\") + 1)
    strFolder = TrailingSlash(strFolder)
    strTemp = Dir(strFolder & strFileSpec)
    Do While strTemp <> vbNullString
         gCount = gCount + 1
         SysCmd acSysCmdSetStatus, gCount
         strSQL = "INSERT INTO Files (FPath, Location) " _
          & " SELECT '" & strFolder & strTemp & "','" & strLocation & "'"
         CurrentDb.Execute strSQL
         colDirList.Add strFolder & strTemp
         strTemp = Dir
    Loop

假定在提取最后一个子文件夹时,strFolder不会有斜杠。所以也许去这个版本。

strFolder = TrailingSlash(strFolder)
strLocation = Left(strFolder, Len(strFolder) - 1)
strLocation = Mid(strLocation, InStrRev(strLocation, "\") + 1)

如果您希望注释中的最终文件夹名称始终为9个字符,则不需要strLocation变量。

strSQL = "INSERT INTO Files (FPath, Location) " & _
  "SELECT '" & strFolder & strTemp & "','" & Left(Mid(strFolder, Len(strFolder) - 9), 9) & "'"

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

VBA-在文件夹中打开文件并打印名称

来自分类Dev

VBA-在文件夹中打开文件并打印名称

来自分类Dev

修改输出文件夹的名称

来自分类Dev

Excel VBA - 从静态文件夹名称复制文件并粘贴到 ActiveCell 中的文件夹名称中

来自分类Dev

Powershell:如何在一行中输出文件夹名称,lastwritetime和文件夹大小?

来自分类Dev

如何将上传的文件存储在名称来自 django 模型字段的文件夹中?

来自分类Dev

如何在VBA中打开具有特定名称的文件夹中的文件?

来自分类Dev

DocumentRoot文件夹名称中的空格

来自分类Dev

目录中的Python文件夹名称

来自分类Dev

禁止在apache中的文件夹名称?

来自分类Dev

禁止在apache中的文件夹名称?

来自分类Dev

获取函数中的文件夹名称

来自分类Dev

Powershell删除名称不在sql输出中的子文件夹

来自分类Dev

无法使用文件夹名称字段来过滤HP QC REST API中的资源

来自分类Dev

VBA 更改文件夹中所有文件的名称

来自分类Dev

从excel文件vba循环文件夹名称

来自分类Dev

将带有 unix 时间戳文件夹名称的文件合并到带有单个输出文件的日期文件夹中

来自分类Dev

将文件夹名称存储到数组中并按字母顺序对VBA进行排序

来自分类Dev

在Access VBA中搜索和匹配部分文件夹名称

来自分类Dev

VBA根据字符串名称从文件夹中获取图片。包含通配符

来自分类Dev

使用 VBA,如何从路径字符串中获取直接父文件夹名称?

来自分类Dev

Excel VBA:文件夹名称未知但扩展名已知的文件夹路径

来自分类Dev

写文件夹名称和子文件夹名称被删除到输出文件

来自分类Dev

VBA-从路径中提取特定的文件夹名称

来自分类Dev

循环浏览每个文件夹并连接文件(在每个文件夹中输出组合文件)

来自分类Dev

循环浏览每个文件夹并连接文件(在每个文件夹中输出组合文件)

来自分类Dev

遍历文件夹并使用文件夹名称顺序重命名每个文件夹中的所有文件

来自分类Dev

比较不同目录中的文件夹名称并在csv中输出两个结果-PowerShell

来自分类Dev

如何使JFileChooser记住先前的文件夹?

Related 相关文章

  1. 1

    VBA-在文件夹中打开文件并打印名称

  2. 2

    VBA-在文件夹中打开文件并打印名称

  3. 3

    修改输出文件夹的名称

  4. 4

    Excel VBA - 从静态文件夹名称复制文件并粘贴到 ActiveCell 中的文件夹名称中

  5. 5

    Powershell:如何在一行中输出文件夹名称,lastwritetime和文件夹大小?

  6. 6

    如何将上传的文件存储在名称来自 django 模型字段的文件夹中?

  7. 7

    如何在VBA中打开具有特定名称的文件夹中的文件?

  8. 8

    DocumentRoot文件夹名称中的空格

  9. 9

    目录中的Python文件夹名称

  10. 10

    禁止在apache中的文件夹名称?

  11. 11

    禁止在apache中的文件夹名称?

  12. 12

    获取函数中的文件夹名称

  13. 13

    Powershell删除名称不在sql输出中的子文件夹

  14. 14

    无法使用文件夹名称字段来过滤HP QC REST API中的资源

  15. 15

    VBA 更改文件夹中所有文件的名称

  16. 16

    从excel文件vba循环文件夹名称

  17. 17

    将带有 unix 时间戳文件夹名称的文件合并到带有单个输出文件的日期文件夹中

  18. 18

    将文件夹名称存储到数组中并按字母顺序对VBA进行排序

  19. 19

    在Access VBA中搜索和匹配部分文件夹名称

  20. 20

    VBA根据字符串名称从文件夹中获取图片。包含通配符

  21. 21

    使用 VBA,如何从路径字符串中获取直接父文件夹名称?

  22. 22

    Excel VBA:文件夹名称未知但扩展名已知的文件夹路径

  23. 23

    写文件夹名称和子文件夹名称被删除到输出文件

  24. 24

    VBA-从路径中提取特定的文件夹名称

  25. 25

    循环浏览每个文件夹并连接文件(在每个文件夹中输出组合文件)

  26. 26

    循环浏览每个文件夹并连接文件(在每个文件夹中输出组合文件)

  27. 27

    遍历文件夹并使用文件夹名称顺序重命名每个文件夹中的所有文件

  28. 28

    比较不同目录中的文件夹名称并在csv中输出两个结果-PowerShell

  29. 29

    如何使JFileChooser记住先前的文件夹?

热门标签

归档