【问题标题】:Move files with certain extensions only仅移动具有特定扩展名的文件
【发布时间】:2013-12-07 22:26:07
【问题描述】:

我目前正在使用Directory.Move 方法将文件从一个位置复制到另一个位置。我想做的只是移动具有某些扩展名(.dbf、.ini 和 .txt)的文件。如果原始文件夹不包含任何这些文件,那么我只想创建一个空目录

我正在使用的当前代码是...

Dim n As Integer

If lb1.SelectedItems.Count = 0 Then Exit Sub

For n = 0 To UBound(AllDetails)

    If AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps = lb1.SelectedItem Then

        If Not My.Computer.FileSystem.DirectoryExists(aMailbox & "\" & AllDetails(n).uFile) Then
            Directory.Move(zMailbox & AllDetails(n).uFile, aMailbox & "\" & AllDetails(n).uFile)
            lb3.Items.Add(AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps)
        Else

            lb3.Items.Add(AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps)
            Exit Sub
        End If
    End If
Next

所有变量都已声明,这有效,但会移动整个文件夹内容

【问题讨论】:

  • 如果你想复制文件,那你为什么要使用Directory.Move
  • 数组AllDetails包含什么类型,它的属性是什么意思
  • 使用Directory.Move,因为文件包含在唯一命名的文件夹中,并且正在复制整个文件夹。有没有更好的办法?基本上这些是作业文件,“所有详细信息”数组是来自文本文件中几行的信息,用于识别作业

标签: vb.net visual-studio-2012


【解决方案1】:

解决此问题的一种方法是将每个扩展名用作File.GetFiles 的搜索模式,然后在返回的每个文件上使用Directory.Move。这样的事情可能会有所帮助:

    For Each OldFile As String In (From s In {".dbf", ".ini", ".txt"}
                                From f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, s)
                                Select f)
        Directory.Move(OldFile, aMailbox & "\" & AllDetails(n).uFile & "\" & Path.GetFileName(OldFile))
    Next

【讨论】:

    【解决方案2】:

    如果您想要的只是某些文件扩展名?好吧Open File Dialog 可以帮你过滤

    假设您通过单击按钮调用此操作:

        Dim fd As OpenFileDialog = New OpenFileDialog()
    
    
        fd.Title = "Open File Dialog"
        fd.InitialDirectory = "the initial directory you want to look at first"
    
        'this filters the available files to be opened!
        fd.Filter = "dbf files|*.dbf*|ini files|*.ini*|Text files|*.txt*"
        fd.FilterIndex = 2 'set's the default files to open first as .ini
        fd.RestoreDirectory = True
    
        If fd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            'Copy the file to the location using the copyer subroutin
            resulta.Text = fd.FileName.ToString
            Copyer(fd.FileName.Tostring, "the location where you want to copy")
        End If
    End Sub
    

    现在对于文件的复制,看看这个子程序。该子例程首先检查文件是否存在。如果是,则将其删除,然后复制新文件。

    Public Sub Copyer(ByVal theFile As String, ByVal Lokasyon As String)
        Try
    
            Dim resultpath As String = Lokasyon
            If System.IO.File.Exists(resultpath) = True Then
    
                System.IO.File.Delete(resultpath) 
                'this deletes the file so you can overwrite it.
            End If
            My.Computer.FileSystem.CopyFile(theFile, resultpath)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    

    如果这对您有帮助,请告诉我。

    【讨论】:

    • 其实不是的。
    • 但我通常还是这样做。我刚刚被打断了。这不是问题的解决方案。 OP 希望移动所有具有这些扩展名的文件,而不仅仅是一个文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    相关资源
    最近更新 更多