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

时间:2015-02-26 17:47:18

标签: vbscript

任何帮助将不胜感激。我想删除文件夹的内容并根据条件(设置名称和变量日期 - 今天)放置文件。我试着拼凑一些东西......但是即使我在追逐自己的尾巴时,我也看到了逻辑变得笨拙。任何帮助将不胜感激。

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

1 个答案:

答案 0 :(得分:2)

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

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