Visual Basic program to recursively take ownership of folders and subfolders

Discussion in 'Visual Basic ( VB )' started by umphorton, Sep 15, 2015.

  1. umphorton

    umphorton New Member

    Joined:
    Sep 15, 2015
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Our network has issues with File Synchronization. Users are randomly losing data. At some point before I got here it was enable for a lengthy period of time then disabled by GPO. Whoever created the user shares didn't turn it off when they were created. Once a user logged on before the GPO was applied and they were syncing files it never stopped after the GPO disabled them. After I turned off synchronization on the user shares they started randomly losing data. The data was found in the c:\windows\csc\v.206\namespace folder. The only way we have found to fix the issue once and for all is to delete their profile after we recover their data and that is just not feasible with 1000 users. In order to access the csc folder you must take ownership and for some reason it fails randomly on some of the subdirectories. So I am writing a tool that allow you to browse to the csc folder and then take ownership of it. It works great for the csc folder but I can't figure out how to make it happen recursively. I am new to visual basic so I am struggling to make it work.
    Here is the code:

    'CSC TAKE OWNERSHIP AND DATA RECOVERY TOOL
    '9/15/2015
    'This tools will take recursively take ownership of the c:\windows\csc folder and copy the affected users
    'folder to a network share then remove all the folders under c:\windows\csc\v2.06\namespace.
    Code:
    Imports System.IO
    Imports System
    Imports System.Drawing
    Imports System.Windows.Forms
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
    #Region " Windows Form Designer generated code "
        Public Sub New()
            MyBase.New()
            InitializeComponent()
        End Sub
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
        Private components As System.ComponentModel.IContainer
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents FolderBrowserDialog1 As System.Windows.Forms.FolderBrowserDialog
        Friend WithEvents folderName As System.Windows.Forms.TextBox
        Friend WithEvents tbUserName As System.Windows.Forms.TextBox
        Friend WithEvents bnBrowseFolder As System.Windows.Forms.Button
        Friend WithEvents bnDoit As System.Windows.Forms.Button
        Friend WithEvents bnQuit As System.Windows.Forms.Button
        Friend WithEvents Label2 As Label
        Friend WithEvents PictureBox1 As PictureBox
        Friend WithEvents Label3 As Label
        Friend WithEvents LinkLabel1 As LinkLabel
        Friend WithEvents Label4 As System.Windows.Forms.Label
    
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
            Me.folderName = New System.Windows.Forms.TextBox()
            Me.tbUserName = New System.Windows.Forms.TextBox()
            Me.bnBrowseFolder = New System.Windows.Forms.Button()
            Me.Label1 = New System.Windows.Forms.Label()
            Me.bnDoit = New System.Windows.Forms.Button()
            Me.bnQuit = New System.Windows.Forms.Button()
            Me.FolderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog()
            Me.Label4 = New System.Windows.Forms.Label()
            Me.Label2 = New System.Windows.Forms.Label()
            Me.PictureBox1 = New System.Windows.Forms.PictureBox()
            Me.Label3 = New System.Windows.Forms.Label()
            Me.LinkLabel1 = New System.Windows.Forms.LinkLabel()
            CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'folderName
            '
            Me.folderName.Location = New System.Drawing.Point(203, 366)
            Me.folderName.Name = "folderName"
            Me.folderName.Size = New System.Drawing.Size(304, 20)
            Me.folderName.TabIndex = 0
            '
            'tbUserName
            '
            Me.tbUserName.Location = New System.Drawing.Point(203, 410)
            Me.tbUserName.Name = "tbUserName"
            Me.tbUserName.Size = New System.Drawing.Size(256, 20)
            Me.tbUserName.TabIndex = 11
            '
            'bnBrowseFolder
            '
            Me.bnBrowseFolder.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            Me.bnBrowseFolder.Location = New System.Drawing.Point(519, 368)
            Me.bnBrowseFolder.Name = "bnBrowseFolder"
            Me.bnBrowseFolder.Size = New System.Drawing.Size(71, 19)
            Me.bnBrowseFolder.TabIndex = 1
            Me.bnBrowseFolder.Text = "Browse..."
            '
            'Label1
            '
            Me.Label1.Location = New System.Drawing.Point(115, 363)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(88, 24)
            Me.Label1.TabIndex = 2
            Me.Label1.Text = "Folder Name:"
            Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
            '
            'bnDoit
            '
            Me.bnDoit.Location = New System.Drawing.Point(131, 479)
            Me.bnDoit.Name = "bnDoit"
            Me.bnDoit.Size = New System.Drawing.Size(72, 48)
            Me.bnDoit.TabIndex = 6
            Me.bnDoit.Text = "Take Ownership"
            '
            'bnQuit
            '
            Me.bnQuit.Location = New System.Drawing.Point(255, 479)
            Me.bnQuit.Name = "bnQuit"
            Me.bnQuit.Size = New System.Drawing.Size(56, 48)
            Me.bnQuit.TabIndex = 7
            Me.bnQuit.Text = "Quit"
            '
            'FolderBrowserDialog1
            '
            Me.FolderBrowserDialog1.Description = "Select the directory that you want to use As the default."
            Me.FolderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.Windows
            Me.FolderBrowserDialog1.ShowNewFolderButton = False
            '
            'Label4
            '
            Me.Label4.Location = New System.Drawing.Point(109, 406)
            Me.Label4.Name = "Label4"
            Me.Label4.Size = New System.Drawing.Size(88, 24)
            Me.Label4.TabIndex = 12
            Me.Label4.Text = "User Name: "
            Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight
            '
            'Label2
            '
            Me.Label2.AutoSize = True
            Me.Label2.Font = New System.Drawing.Font("Verdana", 21.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            Me.Label2.Location = New System.Drawing.Point(125, 147)
            Me.Label2.Name = "Label2"
            Me.Label2.Size = New System.Drawing.Size(465, 35)
            Me.Label2.TabIndex = 13
            Me.Label2.Text = "SDE CSC FOLDER OWNERSHIP"
            '
            'PictureBox1
            '
            Me.PictureBox1.Image = Global.ChangeOwner.My.Resources.Resources.SDELogo_1_9_14
            Me.PictureBox1.Location = New System.Drawing.Point(11, 12)
            Me.PictureBox1.Name = "PictureBox1"
            Me.PictureBox1.Size = New System.Drawing.Size(255, 105)
            Me.PictureBox1.TabIndex = 14
            Me.PictureBox1.TabStop = False
            '
            'Label3
            '
            Me.Label3.AutoSize = True
            Me.Label3.Font = New System.Drawing.Font("Verdana", 21.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            Me.Label3.Location = New System.Drawing.Point(146, 182)
            Me.Label3.Name = "Label3"
            Me.Label3.Size = New System.Drawing.Size(426, 35)
            Me.Label3.TabIndex = 15
            Me.Label3.Text = "AND DATA RECOVERY TOOL"
            '
            'LinkLabel1
            '
            Me.LinkLabel1.AutoSize = True
            Me.LinkLabel1.LinkBehavior = System.Windows.Forms.LinkBehavior.NeverUnderline
            Me.LinkLabel1.Location = New System.Drawing.Point(56, 235)
            Me.LinkLabel1.Name = "LinkLabel1"
            Me.LinkLabel1.Size = New System.Drawing.Size(616, 78)
            Me.LinkLabel1.TabIndex = 16
            Me.LinkLabel1.TabStop = True
            Me.LinkLabel1.Text = resources.GetString("LinkLabel1.Text")
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(734, 862)
            Me.Controls.Add(Me.LinkLabel1)
            Me.Controls.Add(Me.Label3)
            Me.Controls.Add(Me.PictureBox1)
            Me.Controls.Add(Me.Label2)
            Me.Controls.Add(Me.Label4)
            Me.Controls.Add(Me.bnQuit)
            Me.Controls.Add(Me.bnDoit)
            Me.Controls.Add(Me.Label1)
            Me.Controls.Add(Me.bnBrowseFolder)
            Me.Controls.Add(Me.folderName)
            Me.Controls.Add(Me.tbUserName)
            Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
            Me.Name = "SDE CSC FOLDER OWNERSHIP AND DATA RECOVERY TOOL"
            CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)
            Me.PerformLayout()
    
        End Sub
    
    #End Region
        Private Sub bnBrowseFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnBrowseFolder.Click
            If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
                Me.folderName.Text = FolderBrowserDialog1.SelectedPath
            End If
        End Sub
        Private Sub bnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnQuit.Click
            Me.Close()
        End Sub
    
        Private Sub bnDoit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnDoit.Click
            Dim strPath As String
            Dim strUser As String
    
            If tbUserName.Text = "" Then
                MsgBox("Please type your NMEA account name (eg. v06.med.va.gov\vhabecxxxxxx0)", MsgBoxStyle.Exclamation)
                Exit Sub
            End If
    
            strPath = folderName.Text
            strUser = tbUserName.Text
    
            Try
                ChangeOwner(strPath, strUser)
            Catch ex As Exception
                MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical)
            End Try
            MsgBox("Done!")
            End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Text = "SDE CSC FOLDER OWNERSHIP AND DATA RECOVERY TOOL"
            Me.BackColor = Color.LightSteelBlue
            Me.Size = New Size(750, 900)
            Me.Location = New Point(100, 40)
            Me.MaximizeBox = False
        End Sub
    End Class
    
    Any help would be awesome! I have struggled for a week to get this far.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice