Vbscript根据开头的字母复制文件

时间:2017-05-02 13:40:33

标签: vbscript copy letters

我想让这个脚本复制所有以" XX"开头的文件。目前它只复制一个文件。

Dim objFSO, colFiles, objFile, strDestFolder, objNewestFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = objFSO.GetFolder("C:\source")
strDestFolder = "C:\destination\"

For Each objFile In colFiles.Files
  If Left(objFile.Name, 2) = "XX" Then
  If objNewestFile = "" Then   
    Set objNewestFile = objFile  
  Else   
      If objNewestFile.DateLastModified < objFile.DateLastModified Then    
        Set objNewestFile = objFile   
      End If  
  End If
End If
Next

If Not objNewestFile Is Nothing Then 
objFSO.CopyFile objNewestFile.Path,strDestFolder,True
End If

WScript.Echo "Copied."    

1 个答案:

答案 0 :(得分:2)

您可以在FSO *方法的[source]参数中使用通配符?.CopyFile

所以代码可能如下:

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\source\XX*.*", "C:\destination\", True
WScript.Echo "Copied."