根据csv删除文件夹

时间:2017-11-06 13:10:57

标签: vb.net

我目前正在尝试使用VB解决问题,但已经遇到了问题。我不是一个程序员,而且像往常一样了解基础知识,但在这个阶段我的任务太先进了。

基本上我想根据从AD获取的csv中存在的登录名(每天生成)删除存在于两个特定位置的文件夹(包含的所有文件夹和文件)。

当前脚本在90天后禁用用户帐户,删除一些组并将用户帐户移至已禁用的OU。我还需要删除他们的用户个人资料和重定向的文件夹,这些文件夹与CSV中包含的登录名相匹配。

到目前为止,我已经汇总了以下内容:

'Open up the text file
Dim oFSO, oTS
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = oFSO.OpenTextFile("c:\scripts\GmailDisabled.csv

'Assumes the CSV files first row will be the column names and they will be User Name, Email Address, Date Account Expired, Original OU (currently missing login name)

'Creates the variables
Dim sLoginName

'Skip the first line in the code
Dim sLine, sData
sLine - oTS.ReadLine

'Go through file one line at a time
Do Until oTS.AtEndOfStream

'Get the user information from this row
sLoginName - sData(0)

CSV包含以下信息:

用户名,登录名,电子邮件地址,日期帐户已过期,原始OU

路径是:

\\netapp01fs\profiles\%username%

\\rukredir\%username%

我认为我在Powershell中有更多的运气,但它确实需要是VB。每次删除某些内容时更新CSV也会很有帮助。

感谢您查看并提供任何帮助 丹

1 个答案:

答案 0 :(得分:0)

    'Assumes the CSV files first row will be the column names and they will be User Name, Email Address, Date Account Expired, Original OU (currently missing login name)

“test.csv” 登录名,用户名,电子邮件地址,日期帐户已过期,原始OU Daniel.Gange,Daniel Gange,Daniel.Gange @ test.co.uk,09/08/2017,“CN = Daniel Gange,OU = Test,OU = Test,DC = Test,DC = co,DC = uk”< / p>

    Using sr As StreamReader = File.OpenText("c:\scripts\GmailDisabled.csv")
        sr.ReadLine() 'read the first line you said to skip
        Do
            Dim xData As String = sr.ReadLine()  'read one line at the time
            Dim csvValue() As String = xData.Split(",") ' split at every comma (csv file)
            ' the first value always has index 0 which is username
            Dim sUserName As String = csvValue(0)
            Dim PathToDelete1 As String = String.Format("\\netapp01fs\profiles\{0}", sUserName)
            Dim PathToDelete2 As String = String.Format("\\rukredir\\{0}", sUserName)
            MsgBox(PathToDelete1)
            MsgBox(PathToDelete2)
        Loop Until sr.EndOfStream = True 'loop until the end of file
        sr.Close() 'close the csv
    End Using

如果代码以您希望的方式工作,那么使用此代码实际删除您的文件夹。在测试删除数据的内容时,切勿删除文件/文件夹。您可能最终删除了一些您不想删除的内容。

'Assumes the CSV files first row will be the column names and they will be User Name, Email Address, Date Account Expired, Original OU (currently missing login name)
Using sr As StreamReader = File.OpenText("c:\scripts\GmailDisabled.csv")
    sr.ReadLine() 'read the first line you said to skip
    Do
        Dim xData As String = sr.ReadLine()  'read one line at the time
        Dim csvValue() As String = xData.Split(",") ' split at every comma (csv file)
        ' the first value always has index 0 which is username
        Dim sUserName As String = csvValue(0)
        Dim PathToDelete1 As String = String.Format("\\netapp01fs\profiles\{0}", sUserName)
        Dim PathToDelete2 As String = String.Format("\\rukredir\\{0}", sUserName)
        Directory.Delete(PathToDelete1, True) ' set recursive to True to delete files and sub folders
        Directory.Delete(PathToDelete2, True) ' set recursive to True to delete files and sub folders
        'MsgBox(PathToDelete1)
        'MsgBox(PathToDelete2)
    Loop Until sr.EndOfStream = True 'loop until the end of file
    sr.Close() 'close the csv
End Using

更短的版本

    'Assumes the CSV files first row will be the column names and they will be User Name, Email Address, Date Account Expired, Original OU (currently missing login name)
    Using sr As StreamReader = File.OpenText("c:\scripts\GmailDisabled.csv")
        sr.ReadLine() 'read the first line you said to skip
        Do
            Dim xData As String = sr.ReadLine()  'read one line at the time
            ' the first value always has index 0 which is username
            Directory.Delete(String.Format("\\netapp01fs\profiles\{0}", xData.Split(",")(0)), True) ' set recursive to True to delete files and sub folders
            Directory.Delete(String.Format("\\rukredir\\{0}", xData.Split(",")(0)), True) ' set recursive to True to delete files and sub folders
        Loop Until sr.EndOfStream = True 'loop until the end of file
        sr.Close() 'close the csv
    End Using