我想对某一个文件夹中的文件,包括子目录中的*.txt文件进行处理,打开一个用
    Open ("c:\test\001.txt") For Input As #1
    Open ("c:\test2\001.txt")  For Output As #2
    
    '读入文件内容
    Do Until EOF(1)
      Line Input #1, temp
      s = s & temp
    Loop
    Print #2, s
    Close #2
    Close #1
但是不会把说有的文件,包括子目录的读出来循环呀什么的.希望高手指点

解决方案 »

  1.   

    http://vbeden.xg88.com/vbtech/api/page_1/file23.htm
      

  2.   

    'Create a form with a command button (command1), a list box (list1)
    'and four text boxes (text1, text2, text3 and text4).Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As LongConst MAX_PATH = 260
    Const MAXDWORD = &HFFFF
    Const INVALID_HANDLE_VALUE = -1
    Const FILE_ATTRIBUTE_ARCHIVE = &H20
    Const FILE_ATTRIBUTE_DIRECTORY = &H10
    Const FILE_ATTRIBUTE_HIDDEN = &H2
    Const FILE_ATTRIBUTE_NORMAL = &H80
    Const FILE_ATTRIBUTE_READONLY = &H1
    Const FILE_ATTRIBUTE_SYSTEM = &H4
    Const FILE_ATTRIBUTE_TEMPORARY = &H100Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
    End TypePrivate Type WIN32_FIND_DATA
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        nFileSizeHigh As Long
        nFileSizeLow As Long
        dwReserved0 As Long
        dwReserved1 As Long
        cFileName As String * MAX_PATH
        cAlternate As String * 14
    End Type
    Function StripNulls(OriginalStr As String) As String
        If (InStr(OriginalStr, Chr(0)) > 0) Then
            OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
        End If
        StripNulls = OriginalStr
    End FunctionFunction FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
        'KPD-Team 1999
        'E-Mail: [email protected]
        'URL: http://www.allapi.net/    Dim FileName As String ' Walking filename variable...
        Dim DirName As String ' SubDirectory Name
        Dim dirNames() As String ' Buffer for directory name entries
        Dim nDir As Integer ' Number of directories in this path
        Dim i As Integer ' For-loop counter...
        Dim hSearch As Long ' Search Handle
        Dim WFD As WIN32_FIND_DATA
        Dim Cont As Integer
        If Right(path, 1) <> "\" Then path = path & "\"
        ' Search for subdirectories.
        nDir = 0
        ReDim dirNames(nDir)
        Cont = True
        hSearch = FindFirstFile(path & "*", WFD)
        If hSearch <> INVALID_HANDLE_VALUE Then
            Do While Cont
            DirName = StripNulls(WFD.cFileName)
            ' Ignore the current and encompassing directories.
            If (DirName <> ".") And (DirName <> "..") Then
                ' Check for directory with bitwise comparison.
                If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
                    dirNames(nDir) = DirName
                    DirCount = DirCount + 1
                    nDir = nDir + 1
                    ReDim Preserve dirNames(nDir)
                End If
            End If
            Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
            Loop
            Cont = FindClose(hSearch)
        End If
        ' Walk through this directory and sum file sizes.
        hSearch = FindFirstFile(path & SearchStr, WFD)
        Cont = True
        If hSearch <> INVALID_HANDLE_VALUE Then
            While Cont
                FileName = StripNulls(WFD.cFileName)
                If (FileName <> ".") And (FileName <> "..") Then
                    FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
                    FileCount = FileCount + 1
                    List1.AddItem path & FileName
                End If
                Cont = FindNextFile(hSearch, WFD) ' Get next file
            Wend
            Cont = FindClose(hSearch)
        End If
        ' If there are sub-directories...
        If nDir > 0 Then
            ' Recursively walk into them...
            For i = 0 To nDir - 1
                FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
            Next i
        End If
    End Function
    Sub Command1_Click()
        Dim SearchPath As String, FindStr As String
        Dim FileSize As Long
        Dim NumFiles As Integer, NumDirs As Integer
        Screen.MousePointer = vbHourglass
        List1.Clear
        SearchPath = Text1.Text
        FindStr = Text2.Text
        FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
        Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
        Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
        Screen.MousePointer = vbDefault
    End SubPrivate Sub Form_Load()
    Text1.Text = "c:\aaa"
    Text2.Text = "*.txt"
    End Sub
      

  3.   

    利用Dir函数:参考下面的例子Dim MyFile, MyPath, MyName' 返回带指定扩展名的文件名。如果超过一个 *.txt 文件存在,
    ' 函数将返回按条件第一个找到的文件名。
    MyFile = Dir("C:\WINDOWS\*.txt")' 若第二次调用 Dir 函数,但不带任何参数,则函数将返回同一目录下的下一个 *.txt 文件。
    MyFile = Dir' 返回找到的第一个隐式 *.TXT 文件。
    MyFile = Dir("*.TXT", vbHidden)' 显示 C:\ 目录下的名称。
    MyPath = "c:\"   ' 指定路径。
    MyName = Dir(MyPath, vbDirectory)   ' 找寻第一项。
    Do While MyName <> ""   ' 开始循环。
       ' 跳过当前的目录及上层目录。
       If MyName <> "." And MyName <> ".." Then
          ' 使用位比较来确定 MyName 代表一目录。
          If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
             Debug.Print MyName   ' 如果它是一个目录,将其名称显示出来。
          End If
       End If
       MyName = Dir   ' 查找下一个目录。
    Loop
      

  4.   

    参考:
    http://www.codesky.net/article/list.asp?id=2751
      

  5.   

    Private Sub Command1_Click()
        Dim ff() As String
        Dim fn As Long
        Dim i As Long
        
        fn = TreeSearch("c:\aaa", "*.txt", ff())
        
        Debug.Print "找到文件数目为" & fn
        For i = 1 To fn
            Debug.Print ff(i)
        Next
    End Sub
    Private Function TreeSearch(ByVal sPath As String, ByVal sFileSpec As String, sFiles() As String) As Long
        Static lngFiles As Long
        Dim lngIndex As Long
        Dim strDir As String
        Dim strSubDirs() As String
        
        If Right(sPath, 1) <> "\" Then
            sPath = sPath & "\"
        End If
        
        strDir = Dir(sPath & sFileSpec)
        Do While Len(strDir)
            lngFiles = lngFiles + 1
            ReDim Preserve sFiles(1 To lngFiles)
            sFiles(lngFiles) = sPath & strDir
            strDir = Dir
        Loop
        
        lngIndex = 0
        strDir = Dir(sPath & "*.*", 16)
        
        Do While Len(strDir)
            If Left(strDir, 1) <> "." Then
                If GetAttr(sPath & strDir) And vbDirectory Then
                    lngIndex = lngIndex + 1
                    ReDim Preserve strSubDirs(1 To lngIndex)
                    strSubDirs(lngIndex) = sPath & strDir & "\"
                End If
            End If
            strDir = Dir
        Loop
        
        For lngIndex = 1 To lngIndex
            Call TreeSearch(strSubDirs(lngIndex), sFileSpec, sFiles())
        Next lngIndex
        
        TreeSearch = lngFiles
    End Function