Thursday, March 10, 2011

VB.NET program to recursively write a dummy line into all files in a directoy.

Code is in VB.NET (Works with .NET framework 2.0 / 3.0 /3.5)



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fol As DirectoryInfo
        Dim fileList() As FileInfo

        fol = New DirectoryInfo(txtPath.Text)

        RecursiveTouch(txtPath.Text)

    End Sub

    Private Sub RecursiveTouch(ByVal dirName As String)
        Dim filelist() As FileInfo
        Dim folderList() As DirectoryInfo
        Dim rootFolder As DirectoryInfo
        Dim touchStream As StreamWriter
        Dim i, j As Integer

        rootFolder = New DirectoryInfo(dirName)

        filelist = rootFolder.GetFiles()

        For i = 0 To filelist.Length - 1
            touchStream = New StreamWriter(filelist(i).FullName, True)
            touchStream.WriteLine(vbCrLf + "#comment" + vbCrLf)
            touchStream.Flush()
            touchStream.Close()
        Next

        folderList = rootFolder.GetDirectories()

        For j = 0 To folderList.Length - 1
            RecursiveTouch(folderList(j).FullName)
        Next




    End Sub

No comments: