在文件夹和子文件夹中列出文件,路径为.txt文件 [英] List files in folder and subfolder with path to .txt file

查看:110
本文介绍了在文件夹和子文件夹中列出文件,路径为.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个excel表格,其中包含一个目录的路径,我想要一个宏,搜索目录和任何子目录,并列出.txt文件中的文件与每个文件的完整路径。



这是目前我发现看起来像它应该会找到文件,除了路径是硬编码,它不会做任何结果。 p>

任何想法如何改变它以适应我的需要?

  Sub LoopThroughFiles()
Dim StrFile As String
StrFile = Dir(C:\Work\NCL\\\
CLs\histogram_addition\TestData\Input\RTE\)
Do While Len(StrFile)> 0
Debug.Print StrFile
StrFile = Dir
循环
End Sub


解决方案

这是一个使用递归调用从FileSystemObject()示例中拼凑起来的方法。如果需要,可以对结果进行排序。您也可以使用其他FileSystemObject()方法过滤.txt扩展名:

  Sub Sample()
ShowFolderList( C:\temp)
End Sub

Sub ShowFolderList(folderspec)
Dim fs,f,f1,fc,s,sFldr
设置fs = CreateObject (Scripting.FileSystemObject)
设置f = fs.GetFolder(folderspec)
设置fc = f.SubFolders
对于每个f1在fc
如果右(f1,1) <> \然后ShowFolderList f1& \Else ShowFolderList f1
下一个
设置fc = f.Files
对于每个f1在fc
Debug.Print folderspec& f1.Name
下一个
End Sub

写入文件:

  Option Explicit 

Dim file As Object
Dim fs As Object

Sub go()
设置fs = CreateObject(Scripting.FileSystemObject)
设置文件= fs.OpenTextFile(C:\temp2\results3.txt,2,True)'2 = ForWriting,替换
ShowFolderListC:\temp\
file.Close
MsgBoxdone
End Sub

Sub ShowFolderList folderpec)
错误GoTo local_err
Dim f,f1,fc,s,sFldr
设置f = fs.GetFolder(folderspec)
设置fc = f.SubFolders
对于每个f1在fc
如果右(f1,1)<> \然后ShowFolderList f1& \Else ShowFolderList f1
下一个
设置fc = f.Files
对于每个f1在fc
file.writeline folderspec& f1.Name
下一个
local_exit:
Exit Sub
local_err:
MsgBox Err& & Err.Description
简历local_exit
简历
结束子


I have an excel sheet that has a cell that contains the path to a directory, i want a macro that searches the directory and any sub directories and lists the files in a .txt file, with the full path of each file.

This is currently what i have found that looks like it should find the files except the path is hard-coded and it doesn't do anything with the results.

Any ideas how i can change it to fit my needs?

Sub LoopThroughFiles()
Dim StrFile As String
StrFile = Dir("C:\Work\NCL\nCLs\histogram_addition\TestData\Input\RTE\")
Do While Len(StrFile) > 0
    Debug.Print StrFile
    StrFile = Dir
Loop
End Sub

解决方案

Here's a method cobbled together from the FileSystemObject() examples using a recursive call. Apply a sort to the results if needed. You can also filter by .txt extension using other FileSystemObject() methods:

    Sub Sample()
        ShowFolderList ("C:\temp")
    End Sub

    Sub ShowFolderList(folderspec)
        Dim fs, f, f1, fc, s, sFldr
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set f = fs.GetFolder(folderspec)
        Set fc = f.SubFolders
         For Each f1 In fc
            If Right(f1, 1) <> "\" Then ShowFolderList f1 & "\" Else ShowFolderList f1
        Next
        Set fc = f.Files
        For Each f1 In fc
            Debug.Print folderspec & f1.Name
        Next
    End Sub

Write to file:

    Option Explicit

    Dim file As Object
    Dim fs As Object

    Sub go()
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set file = fs.OpenTextFile("C:\temp2\results3.txt", 2, True) ' 2=ForWriting, replace
        ShowFolderList "C:\temp\"
        file.Close
        MsgBox "done"
    End Sub

    Sub ShowFolderList(folderspec)
    On Error GoTo local_err
        Dim f, f1, fc, s, sFldr
        Set f = fs.GetFolder(folderspec)
        Set fc = f.SubFolders
         For Each f1 In fc
            If Right(f1, 1) <> "\" Then ShowFolderList f1 & "\" Else ShowFolderList f1
        Next
        Set fc = f.Files
        For Each f1 In fc
            file.writeline folderspec & f1.Name
        Next
     local_exit:
        Exit Sub
     local_err:
        MsgBox Err & " " & Err.Description
        Resume local_exit
        Resume
    End Sub

这篇关于在文件夹和子文件夹中列出文件,路径为.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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