|
自己制作PE时,通常会缺少文件,这就少不了查找和复制,有的时候文件分布在多个目录下,手动查找和复制很麻烦,我就写了一个自动生成批处理copy的VBA程序。
程序代码如下:
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
代码的原理如下:
1. 利用dos命令dir /b /s filename 生成文件路径;
2. 利用字符串处理,创建批处理语句,并且写到文件当中;
不足:
1.当需要查找的文件很多时,需要手动添加路径;
2.代码没有函数话,使用稍显不便 |
|