将具有特定扩展名的文件移动到vb.net中的相关文件夹

时间:2019-02-20 22:16:54

标签: vb.net

我正在使用下面的子过程将特定文件分类为特定文件夹,但是我认为应该有更多的逻辑方法来执行此操作,而不要使用太多ifelif结构。我不知道这样做是真的方法。

Dim DirInfo As New DirectoryInfo(strPath & "\" & My.Settings.txt_main_db)
                    For Each SubFile As FileInfo In DirInfo.GetFiles
                        If Path.GetExtension(SubFile.Name) = ".spck" Or Path.GetExtension(SubFile.Name) = ".buspck" Then
                            Dim subPathSpck = strPath & "\" & My.Settings.txt_main_db & "\" & My.Settings.txt_mbs_db_substructure
                            Directory.CreateDirectory(subPathSpck)
                            SubFile.MoveTo(subPathSpck & "\" & SubFile.Name)
                        ElseIf Path.GetExtension(SubFile.Name) = ".fbi" Then
                            Dim subPathFBI = strPath & "\" & My.Settings.txt_main_db & "\" & My.Settings.txt_elastic_body
                            Directory.CreateDirectory(subPathFBI)
                            SubFile.MoveTo(subPathFBI & "\" & SubFile.Name)
                        ElseIf Path.GetExtension(SubFile.Name) = ".stl" _
                            Or Path.GetExtension(SubFile.Name) = ".obj" _
                            Or Path.GetExtension(SubFile.Name) = ".igs" _
                            Or Path.GetExtension(SubFile.Name) = ".slp" _
                            Or Path.GetExtension(SubFile.Name) = ".obj" Then
                            Dim subPathCAD = strPath & "\" & My.Settings.txt_main_db & "\" & My.Settings.txt_cad_geometry
                            Directory.CreateDirectory(subPathCAD)
                            SubFile.MoveTo(subPathCAD & "\" & SubFile.Name)
                        ElseIf Path.GetExtension(SubFile.Name) = ".if2" _
                            Or Path.GetExtension(SubFile.Name) = ".afs" _
                            Or Path.GetExtension(SubFile.Name) = ".tre" Then
                            Dim subPathIF2 = strPath & "\" & My.Settings.txt_main_db & "\" & My.Settings.txt_input_functions
                            Directory.CreateDirectory(subPathIF2)
                            SubFile.MoveTo(subPathIF2 & "\" & SubFile.Name)
                        ElseIf Path.GetExtension(SubFile.Name) = ".subvar" Then
                            Dim subPathSubVar = strPath & "\" & My.Settings.txt_main_db & "\" & My.Settings.txt_mbs_db_ip
                            Directory.CreateDirectory(subPathSubVar)
                            SubFile.MoveTo(subPathSubVar & "\" & SubFile.Name)
                        ElseIf Path.GetExtension(SubFile.Name) = ".tpf" _
                            Or Path.GetExtension(SubFile.Name) = ".tir" Then
                            Dim subPathDelft = strPath & "\" & My.Settings.txt_main_db & "\" & My.Settings.txt_tyre_delft_swift
                            Directory.CreateDirectory(subPathDelft)
                            SubFile.MoveTo(subPathDelft & "\" & SubFile.Name)
                        ElseIf Path.GetExtension(SubFile.Name) = ".rdf" Then
                            Dim subPathRoad = strPath & "\" & My.Settings.txt_main_db & "\" & My.Settings.txt_mbs_db_road
                            Directory.CreateDirectory(subPathRoad)
                            SubFile.MoveTo(subPathRoad & "\" & SubFile.Name)
                        Else
                            Dim subPathExt = strPath & "\" & My.Settings.txt_main_db & "\" & My.Settings.txt_mbs_db_extfile
                            Directory.CreateDirectory(subPathExt)
                            SubFile.MoveTo(subPathExt & "\" & SubFile.Name)
                        End If
                    Next

1 个答案:

答案 0 :(得分:2)

如果... elseif ..(甚至是Select Case)也没有错,但是如果您要删除冗长的选择,则可以使用Dictionary,其中的关键是文件扩展名和值是处理该特定键扩展名的功能。
该键用于查找可配置变量并执行文件移动的功能。

这可以通过这种方式完成,我让你来判断这是否更清楚还是不符合您当前的代码:

' some test values
Dim strPath As String = "e:\temp"
Dim txt_main_db As String = "root"
Dim txt_mbs_db_substructure = "spkBackup"
Dim txt_elastic_body = "fbiBackup"

' the dictionary
Dim moveHandler As Dictionary(Of String, Action(Of FileInfo)) 

' initialization somewhere in your code...
Sub Main
    ' Set the handler for each extensions but you could have the same handler for many extensions
    moveHandler.Add(".spck", Function(f As FileInfo) HandleSpck(f))
    moveHandler.Add(".buspck", Function(f As FileInfo) HandleSpck(f))
    moveHandler.Add(".fbi", Function(f As FileInfo) HandleFbi(f))
    ......
    moveHandler.Add("others", Function(f As FileInfo) HandleOthers(f))
End Sub

' handler to move a file with .spck or .buspck  extension 
Function HandleSpck(SubFile As FileInfo)
    Dim subPathSpck = Path.Combine(strPath,txt_main_db, txt_mbs_db_substructure)
    Directory.CreateDirectory(subPathSpck)
    SubFile.MoveTo(Path.Combine(subPathSpck,SubFile.Name))
End Function

' handler to move a file with .fbi  extension 
Function HandleFbi(SubFile As FileInfo)
    Dim subPathSpck = Path.Combine(strPath, txt_main_db, txt_elastic_body )
    Directory.CreateDirectory(subPathSpck)
    SubFile.MoveTo(Path.Combine(subPathSpck, SubFile.Name))
End Function

' Other handlers for other extensions
.....

最后,您可以以非常短的循环调用处理程序

Dim source = Path.Combine(strPath, txt_main_db)

Dim DirInfo As New DirectoryInfo(source)
For Each SubFile As FileInfo In DirInfo.GetFiles
    If moveHandler.ContainsKey(SubFile.Extension) Then
        moveHandler(SubFile.Extension).Invoke(SubFile)
    Else
        moveHandler("others").Invoke(SubFile)
    End If
Next

但是,等等...。现在可以放置此代码了,可以轻松注释模式。您始终执行相同的代码。唯一改变的是目的地。现在,如果我们有一个传递目标文件夹和FileInfo变量的处理程序,该怎么办?我们只能有一个处理程序

Function HandleFileMove(destFolder as String, SubFile As FileInfo)
    Dim subFolder = Path.Combine(strPath,txt_main_db, destFolder)
    Directory.CreateDirectory(subFolder)
    SubFile.MoveTo(Path.Combine(subFolder,SubFile.Name))
End Function

我们必须调整字典以始终调用相同的处理程序,但要提供创建子文件夹所需的新参数。

moveHandler.Add(".spck", Function(f As FileInfo) HandleFileMove(txt_mbs_db_substructure, f))
moveHandler.Add(".fbi", Function(f As FileInfo) HandleFileMove(txt_elastic_body, f))
.....

当然,仅当您在公共处理程序中唯一的任务是在预定义的子文件夹中复制文件时,这才是好方法。如果您需要为特定扩展程序执行其他任何任务,那么最好为每个扩展程序使用单独的处理程序。