VBA 复制源文件夹下所有文件到目标文件夹

VBA 复制源文件夹下所有文件到目标文件夹

功能

  • 将源文件夹下含有关键字且后缀含有“.x"的所有文件复制到指定文件夹下。
  • 如果在源文件夹下没有找到含该关键字的文件,则在log文件中写入该关键字和0
  • 如果在源文件夹下找到多个含该关键字的文件,则在log文件中写入该关键字和找到的次数

bug

  • 需要判定input的文件是否存在

改进

  • input参数化,而不是修改功能函数

步骤

Option Explicit

'//clear folder
Function clearFolder(ByVal folder As String) As Boolean
    Shell "cmd.exe /c rd/s/q " & folder & "\"
    Shell "cmd.exe /c md " & folder
End Function


'//clear txt
Function clearTxt(ByVal txtPath As String) As Boolean
    clearTxt = True
On Error GoTo ErrorHandler
    Open txtPath For Output As #1
        Print #1, ""
    Close #1
    
ExitFuction:
    Exit Function
    
ErrorHandler:
    Debug.Print "clearTxt() ERR(No." & Err.Number & "): " & Err.Description
    clearTxt = False
End Function

'//write txt line by line
Sub writeTxt(ByVal txtPath As String, ByVal lineStr As String)
    Dim fs As Object
    Dim f
    Set fs = CreateObject("Scripting.FileSystemObject")
    '//txtPath: open txtPath
    '//8: write something from the end of file
    '//True: if txtPath is not exist,new one
    Set f = fs.opentextfile(txtPath, 8, True)
    f.writeline lineStr
    f.Close
End Sub

'//copy file from Path to afterPath
Sub copyFiles(ByVal Path As String, ByVal afterPath As String)
    Dim Spath As String
    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    afterPath = afterPath & "\" & Dir(Path)
    fs.CopyFile Path, afterPath
End Sub

'//read all files in the folder
Function getFilePathList(ByVal strFolder As String, ByR

你可能感兴趣的:(VBA,前端)