VBScript --删除文件夹内容并放置一个文件

VBScript --Delete folder contents and place a file

如有任何帮助,我们将不胜感激。我想删除文件夹的内容并根据条件(设置名称和可变日期——今天)放置一个文件。我试着把一些东西拼凑在一起……但即使我看到我追逐自己的尾巴时逻辑也变得歪斜了。任何帮助将不胜感激。

Set FSO1 = CreateObject("Scripting.FileSystemObject")
Set folder = FSO1.GetFolder("C:Docs\Template")

for each f in folder.files
On Error Resume Next
name = f.name
f.Delete True
On error GoTo 0
Next

MyDate = Replace(Date,"/","-")
Const src = "C:\Docs\Blah"
Const dst = "C:\Docs\Template"
Set FSO = CreateObject("Scripting.FileSystemObject")

For each f In FSO.GetFolder(src).Files
If FSO.GetFileName(f.name) = "Bluhdeblah_" & Mydate & ".xlsm"
Then FSO.CopyFile src, "C:\Docs\Template", True

End If

Set FSO = Nothing
Set FSO1 = Nothing

你有一些问题。尝试以下(未经测试)并了解它的流程:

Const src = "C:\Docs\Blah\" ' Note "\" is added here
Const dst = "C:\Docs\Template\"
UpdateFolder

Sub UpdateFolder()
    Dim oFSO, oFile, sFile
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    ' Delete Existing Files
    On Error Resume Next
    For Each oFile In oFSO.GetFolder(dst).Files
        oFile.Delete
    Next
    On Error GoTo 0
    ' Copy updated file if found in src to dst
    sFile = src & "Bluhdeblah_" & Format(Date, "dd-mm-yy") & ".xlsm"
    If oFSO.FileExists(sFile) Then oFSO.copyfile sFile, dst, True
    Set oFSO = Nothing
End Sub