注册 登录
编程论坛 VB.NET论坛

VB.net如何做到复制所有同名的文件从一文件夹到另一文件夹

zhangzhen 发布于 2011-07-25 20:46, 631 次点击
比如说复制所有叫zz的不管什么后缀的文件都复制到指定文件夹?
2 回复
#2
haigecnpeng2011-07-28 19:43
理论上是可以的,不过懒得写了,搜下吧,
大概思路是先在制定的范围内搜索文件,这个例子在很多地方有
然后,filecopy就行了
#3
wube2011-07-29 01:09
练习了一下,是这样吗?用VB6的思路去写的~
顺道问一下~这样写好像FileListBox1的反应不是很灵敏~
这是为何?

程序代码:

Option Explicit On

Public Class Form1

    Dim FileList() As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim TempString As String, FilePath As String, SmailarFile As String, Temp() As String, Target As String
        Dim i As Short, j As Object

        ReDim FileList(0)

        If TextBox1.Text <> "" Then
            Target = Trim(InputBox("The Target File Path is ..."))
            If Target <> "" Then

                Target = IIf(Mid(Target, Len(Target), 1) = "\", Target, Target & "\")
                TempString = Trim(TextBox1.Text) : ToolTip1.SetToolTip(TextBox1, TempString)

                If InStr(TempString, ":") <> 0 And InStr(TempString, "\") <> 0 And InStr(TempString, ".") <> 0 Then

                    Temp = Split(TempString, "\")
                    DriveListBox1.Drive = Temp(0)
                    If UBound(Temp) > 2 Then
                        For i = 0 To UBound(Temp) - 1
                            FilePath = FilePath & Temp(i) & "\"
                        Next i
                        DirListBox1.Path = FilePath
                    Else
                        DirListBox1.Path = Temp(0) & "\"
                    End If

                    SmailarFile = Mid(Temp(UBound(Temp)), 1, InStr(Temp(UBound(Temp)), ".") - 1)
                    FileListBox1.Pattern = "*.*"
                    FileListBox1.Path = FilePath
                    i = 0 : ListBox1.Items.Clear()
                    For Each j In FileListBox1.Items
                        If InStr(j, SmailarFile) <> 0 Then
                            ReDim Preserve FileList(i)
                            FileList(i) = FilePath & j
                            ListBox1.Items.Add(j)
                            i += 1
                        End If
                    Next j

                    For i = 0 To UBound(FileList)
                        FileCopy(FileList(i), Target & Mid(FileList(i), InStrRev(FileList(i), "\") + 1))
                    Next i

                    'MessageBox.Show(Join(FileList, vbNewLine))

                Else
                    MessageBox.Show("Input File Path Format Error !", "ERROR")
                    TextBox1.Text = "" : ListBox1.Items.Clear()
                End If
            Else
                MessageBox.Show("No Target Path !", "ERROR")
            End If
        Else
            MessageBox.Show("No Data !", "ERROR")
        End If

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        TextBox1.Text = "" : ListBox1.Items.Clear()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Application.Exit()
    End Sub

    Private Sub DriveListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DriveListBox1.SelectedIndexChanged
        DirListBox1.Path = DriveListBox1.Drive
        FileListBox1.Path = DirListBox1.Path
    End Sub

    Private Sub DirListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DirListBox1.SelectedIndexChanged
        FileListBox1.Items.Clear()
        FileListBox1.Path = DirListBox1.Path
    End Sub

End Class
1