如何递归地为文件名添加前缀并输出到文件夹

时间:2016-09-30 20:43:33

标签: vb.net

对,这个让我精神振奋。我确定我做的事情令人难以置信的愚蠢,但我无法解决这个问题。我正在尝试复制目录结构中的每个文件,并使用它的源目录重命名,输出到单个文件夹中。我的代码似乎什么都不做。帮助!

Imports System.IO
Public Class Form1
Dim projDirectory As String = "C:\Users\phil\Desktop\satdoc software trial folders\test project folder"

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim ProjArray = IO.Directory.GetDirectories(projDirectory)
    ComboBox1.MaxDropDownItems = ProjArray.Length

    For Each proj As String In ProjArray
        proj = proj.Substring(proj.LastIndexOf("\") + 1)
        ComboBox1.Items.Add(proj) 'populate combobox with list of projects in folder
    Next

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim projfolder As New DirectoryInfo(projDirectory & "\" & ComboBox1.SelectedItem)
    finalise(projfolder)

End Sub
Private Sub finalise(ByVal folder As DirectoryInfo)
    Try
        Dim files = folder.GetFiles
        For Each file In files
            Dim pos1 = InStr(file.Name, ComboBox1.SelectedItem) - 1 'position in string marks beginning of project name
            Dim pos2 = file.Name.LastIndexOf("\") ' position in string marks last subfolder
            Dim NoofChars As Integer = pos2 - pos1 'number of chars to use from full string
            Dim filenameappend = file.Name.Substring(pos1, NoofChars) 'select the appropriate chars
            Dim append As String = filenameappend.Replace("\", " ") 'replace all teh \ with spaces
            file.CopyTo(projDirectory & "\output\" & append & file.Name) 'copy to another folder and prefix the filename with the filestructure of its parent project
            Label1.Text = append
        Next
        For Each subfolder In folder.GetDirectories
            finalise(subfolder) 'recursive through all folders
        Next
    Catch e As Exception
    End Try

End Sub
End Class

0 个答案:

没有答案