Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 查找并列出文件名以增加子文件夹_Vba_Excel - Fatal编程技术网

Vba 查找并列出文件名以增加子文件夹

Vba 查找并列出文件名以增加子文件夹,vba,excel,Vba,Excel,我有两个密码。我们将搜索并命名目录中的每个文件夹。另一个将列出单个文件夹中的文件和文件名。我不精通VBA来解决这个问题,所以我需要StackOverflow 以下是文件名列表程序: Sub Example1() Dim objFSO As Object Dim objFolder As Object Dim objFile As Object Dim i As Integer 'Create an instance of the FileSystemObject Set objFSO

我有两个密码。我们将搜索并命名目录中的每个文件夹。另一个将列出单个文件夹中的文件和文件名。我不精通VBA来解决这个问题,所以我需要StackOverflow

以下是文件名列表程序:

Sub Example1()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer

'Create an instance of the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
    Set objFolder = objFSO.GetFolder("\\fc8fsp01\litho_recipe_amat_data")
    i = 1
'loops through each file in the directory and prints their names and path
    For Each objFile In objFolder.Files
    'print file name
        Cells(i + 1, 1) = objFile.Name
    'print file path
        Cells(i + 1, 2) = objFile.Path
        i = i + 1
Next objFile
End Sub
下面是第二个代码,用于导航子文件夹以写入文件夹名称:

Option Explicit

Dim i As Long, j As Long
Dim searchfolders As Variant
Dim FileSystemObject

    Sub ListOfFolders()
        Dim LookInTheFolder As String

        i = 1
        LookInTheFolder = "\D: ' As you know; you should modificate this row.
        Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
        For Each searchfolders In FileSystemObject.GetFolder(LookInTheFolder).SubFolders
            Cells(i, 1) = searchfolders
            i = i + 1
            SearchWithin searchfolders
        Next searchfolders

    End Sub

Sub SearchWithin(searchfolders)
        On Error GoTo exits
    For Each searchfolders In FileSystemObject.GetFolder(searchfolders).SubFolders
        j = UBound(Split(searchfolders, "\"))
        Cells(i, j) = searchfolders
        i = i + 1
        SearchWithin searchfolders
        Next searchfolders
exits:
End Sub

我需要一个代码,将搜索所有子文件夹,并列出所有包含的文件。请帮助D:

这是我用来查找目录中所有文件的函数

Public Function RecursiveDir(colFiles As Collection, _
                      ByVal strFolder As String, _
                      strFileSpec As String, _
                      bIncludeSubfolders As Boolean)

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

'Add files in strFolder matching strFileSpec to colFiles
 strFolder = TrailingSlash(strFolder)
 strTemp = Dir(strFolder & strFileSpec)
 Do While strTemp <> vbNullString
 colFiles.Add strFolder & strTemp
 strTemp = Dir
 Loop

'Fill colFolders with list of subdirectories of strFolder
 If bIncludeSubfolders Then
     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 RecursiveDir for each subfolder in colFolders
 For Each vFolderName In colFolders
     Call RecursiveDir(colFiles, strFolder & vFolderName, strFileSpec, True)
 Next vFolderName
 End If

'Garbage collection
 Set colFolders = Nothing

End Function

然后只需设置
FileExt=“*.*”
,它将查找每个文件扩展名为的每个文件。希望这能起到更大的帮助。

由于我访问的一些文件夹出现在网络驱动器上时存在速度问题,我编写了一个使用Windows Shell
dir
命令的小型VBA程序。使用正确的参数,这将返回基本目录中的所有文件;以及所有子文件夹和文件等。我让它将结果写入一个文本文件,然后将其读入Excel进行进一步处理

与使用VBA的DIR或FSO相比,当文件位于网络驱动器上时,这种方法的运行速度大约快五倍——在本地计算机上不太明显——但我将其作为另一种方法介绍

必须设置对Windows脚本主机对象模型的引用。
sDrive
sBasePath
用于设置起始文件夹名称。
sFileList
是将结果写入文本文件的地方

/S
参数显示指定目录和所有子目录中的文件。
/B
参数会导致忽略标题信息和摘要

如果运行
CMD.EXE
并查找有关
dir
命令的帮助,您将看到其他参数的解释


publicsdrive作为字符串
公共sBasePath作为字符串
Public Const sFileList As String=“C:\Users\Ron\FileList.txt”
选项显式
子GetDirTree()
将WSH暗显为WshShell
暗淡的勒尔代码,如长
设置WSH=newwshshell
lercode=WSH.Run(“cmd.exe/c dir”“&sDrive&sBasePath&”“/B/S>”&sFileList,0,True)
如果lErrCode为0,则
MsgBox(“GetDirTree中的错误:错误号:&CStr(lercode))
停止
如果结束
端接头


你知道递归的概念吗?我经常会出错。我说不出这显然是更好的选择,尤其是因为我正在尝试映射网络驱动器,但执行时遇到问题。我引用了Windows脚本主机模型对象。我确信我忘记了一些重要的步骤。您已经提到,这将写入一个独立的文本文件。有什么方法可以放置或引用空白文本文件吗?@codingnearoor只需输入一个文件名来代替我在示例中使用的文件名。如果您发布了实际使用的代码、驱动器/文件夹路径的参数以及实际的错误代码,这会有所帮助。@CodingKnool同样,您应该确保shell命令在
cmd
对话框窗口中正常工作。如果它在那里不起作用,它在你的宏中就不起作用了。我确实签了cmd,它看起来确实起作用了。至少,Diogue box会输出大量数据。我收到错误:“GetDirTree中的错误:错误号:1”
Dim colFiles As New Collection ' The collection of files
Dim Path As String ' The parent Directory you want to search
Dim subFold As Boolean ' Search sub folders, yes or no?
Dim FileExt As String ' File extension type to search for
Public sDrive As String
Public sBasePath As String
Public Const sFileList As String = "C:\Users\Ron\FileList.txt"
Option Explicit
Sub GetDirTree()
    Dim WSH As WshShell
    Dim lErrCode As Long

Set WSH = New WshShell
lErrCode = WSH.Run("cmd.exe /c dir """ & sDrive & sBasePath & """/B /S >" & sFileList, 0, True)
If lErrCode <> 0 Then
    MsgBox ("Error in GetDirTree: Error Number: " & CStr(lErrCode))
    Stop
End If

End Sub