2015-10-19 56 views
1

我需要在2個不同的文件夾中創建2個txt文件。然後我需要刪除這兩個文件,但該腳本只會刪除第一個文件夾中的第一個文件。Vbscript刪除多個文件不起作用

有我的VBScript代碼

strFile = "\timestamp.txt" 

Set objFSO = CreateObject("Scripting.FileSystemObject") 


       Set objFile = objFSO.CreateTextFile(source & strFile) 
       Set objFile = objFSO.CreateTextFile(destination & strFile) 

       Set src = objFSO.GetFolder(source) 
       Set dst = objFSO.GetFolder(destination) 

       for each f in src.Files 
        On Error Resume Next 
         name = f.name 
         If name = "timestamp.txt" Then 
          f.Delete True 
         End If 
        On Error GoTo 0 
       Next 
       for each f in dst.Files 
        On Error Resume Next 
         name = f.name 
         If name = "timestamp.txt" Then 
          f.Delete True 
         End If 
        On Error GoTo 0 
       Next 
+2

刪除「On Error Resume Next」以查看您正在收到什麼錯誤以及何時收到錯誤。我懷疑一個「正在使用的文件」異常,看到你沒有關閉你創建的文件對象。 – DanL

+0

「Microsoft VBScript運行時錯誤:權限被拒絕」 - 這是錯誤 –

回答

3

我解決這個問題。我需要在創建文件後關閉objFile。 Thx you DanL

+0

好聽,我不應該爲您發佈答案嗎? ;-) – DanL