需要帮助处理子文件夹

时间:2016-11-07 20:23:55

标签: text vbscript

所以我想创建一个.vbs来编辑文件夹中的所有.txt。这是我使用的代码,文件夹是C:\test folder

Const ForReading = 1
Const ForWriting = 2

newline = ""
line = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

objStartFolder = "C:\test folder\"

Dim lineCount : lineCount = 0
Dim firstContent : firstContent = ""

Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files

For Each objFile in colFiles
    If LCase(objFSO.GetExtensionName(objFile)) = "txt" Then
        lineCount = 0
        firstContent = ""

        FileName = objStartFolder & objFile.Name

        Set objStream = objFSO.OpenTextFile(FileName, ForReading)
        Do Until objStream.AtEndOfStream
            lineCount = lineCount + 1
            firstContent = firstContent & objStream.ReadLine & vbCrLf
            If lineCount = line Then 
                firstContent = firstContent & newline & vbCrLf
            End If
        Loop
        Set objStream = objFSO.OpenTextFile(FileName, ForWriting)
        objStream.WriteLine firstContent
        objStream.Close 
    End If
Next

有效。并将所有文本文件更改为我想要他们说的内容,但当我在C:\test folder中创建一个名为SFC:\test folder\SF)的文件夹时,SF中的所有文本文件不要改变。如何让它与子文件夹一起使用?

1 个答案:

答案 0 :(得分:1)

递归是一个调用自身的函数。它用于走树。

ProcessFolder DirName

Sub ProcessFolder(FolderPath)
'    On Error Resume Next
    Set fldr = fso.GetFolder(FolderPath)

    Set Fls = fldr.files

    For Each thing in Fls
         msgbox Thing.Name & " " & Thing.path 
    Next

    Set fldrs = fldr.subfolders
    For Each thing in fldrs
        ProcessFolder thing.path
    Next

End Sub