-1

So I want to make a .vbs that edits all .txt in a folder. This the code I used, and the folder is 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

It works. and changes all the text files to what I want them to say, but when I made a folder in C:\test folder called SF (C:\test folder\SF), all of the text files in SF don't change. How do I get it to work with subfolders?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Kyle TV
  • 45
  • 1
  • 7

1 Answers1

1

Recursion is a function calling itself. It is used to walk trees.

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
  • Do I replace my .vbs with this or add it? – Kyle TV Nov 07 '16 at 20:52
  • Neither, you learn from it. Why would I write your code? –  Nov 07 '16 at 20:56
  • Errors at line 5. "Object required: objFSO". Do I put Set objFSO = CreateObject("Scripting.FileSystemObject")? – Kyle TV Nov 07 '16 at 21:02
  • All non essential parts are omitted. So yes you do. Also you need to set the initial directory (Dirname). And why is that, I'll use C:\windows (always exists) which would give you 10,000 messages boxes to click. You can set to a folder on your machine. –  Nov 07 '16 at 21:15
  • This works(thanks!), but how do I mod this into my original script? Should I add it between "For Each thing in Fls" and "Next"(where it displays the message box) – Kyle TV Nov 07 '16 at 21:58
  • I got it working! I had to use a whole different script to mod the text files, but the you posted helped my edit the subfolder's text files! Thanks! – Kyle TV Nov 07 '16 at 22:12
  • It's [already been answered](http://stackoverflow.com/a/14965606/692942) before. – user692942 Nov 07 '16 at 22:58
  • It a technique you need to learn for programming. Mainly because we have tree structures such as disks and the registry, but some files such as XML are also tree based. I was going to say is stick your stuff into that program structure (replace `msgbox` with what you want to do). Here's a link on recursion https://en.wikipedia.org/wiki/Recursion_(computer_science). Another way of solving common programming problems, which you use in your code, is Iteration. You use it here `For Each objFile in colFiles`. One does a tree and one does a list. –  Nov 07 '16 at 23:19
  • PS in sample code the `msgbox` is the line you replace with your code. –  Nov 07 '16 at 23:58