VB.net如何复制一个文件夹下所有同名文件到另一文件夹

2024-12-13 20:07:58
推荐回答(1个)
回答1:

'这是VB的要先引用Microsoft Scripting RunTime,下面的例子是找叫cmd.xxx的文件的
Private Sub Command1_Click()
Dim m_objFSO As New FileSystemObject
Dim objFolder As Scripting.Folder '文件夹对象
Dim objFile As Scripting.File '文件对象

Set objFolder = m_objFSO.GetFolder("c:/test")

For Each objFile In objFolder.Files
Dim filenames() As String
filenames = Split(objFile.Name, ".")
If filenames(0) = "cmd" Then
FileCopy objFile.Path, "c:/TestCMD/" & "newName." & filenames(1)
End If
Next objFile
End Sub
-----------------------------------这是VB.net的--------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim files() As String
files = System.IO.Directory.GetFiles("C:/test")
For i = 0 To files.Length - 1
Dim filenames() As String
filenames = files(i).Substring(files(i).LastIndexOf("\") + 1).Split(".")
If filenames(0) = "cmd" Then
FileCopy(files(i), "c:/TestCMD/" & "newName." & filenames(1))
End If
Next
End Sub