Sub findAndCopy()
Dim WSH As Object, wExec As Object, result
Dim val, n
Dim i As Integer
Dim sFile As Object, Fso As Object
Dim cmdStr As String
Set WSH = CreateObject("WScript.Shell")
ChDir ThisWorkbook.Path
Set wExec = WSH.exec("cmd.exe /c dir /b /s C:\Windows\devmgmt.msc") '将此处的C:\Windows\devmgmt.msc替换成自己需要查找的文件路径
result = wExec.StdOut.ReadAll
ActiveSheet.Cells(1, 1) = result
val = Split(result, Chr(13))
ActiveSheet.Cells(1, 2) = val(1)
Set Fso = CreateObject("Scripting.FileSystemObject")
Set sFile = Fso.CreateTextFile("TestFile.cmd", True) '此处添加生成的批处理脚本的路径和名称
i = 0
For n = LBound(val) To UBound(val)
If n < UBound(val) Then
ActiveSheet.Cells(1, i + 2) = val(i)
cmdStr = "echo F | xcopy " & val(i) & " " & Replace(val(i), "C:", "D:\dddddd") & " /Y /H" ’将此处的D:\dddddd替换成目标的文件路径
sFile.WriteLine (Replace(cmdStr, Chr(10), ""))
i = i + 1
End If
Next
sFile.WriteLine ("pause")
End Sub