开发者

VBScript cannot delete folder

I have a VBScript program that creates a folder in the user's temp folder:

set fso = CreateObject("scripting.FileSystemObject")
temp = fso.GetSpecialFolder(2)
dropzone = temp & "\{d450c76c-2ad8-4f73-af8a-ccc5ba28036a}\"

If Not fso.FolderExists(dropzone) Then 
    set NewFolder = fso.CreateFolder(dropzone)
End If
set NewFolder = Nothing

At the end of the program, I would like to delete that folder. I tried this but it gives me a permission denied error:

set deletefolder = fso.GetFolder(dropzone)
deletefo开发者_开发知识库lder.Delete(True) 
set fso = Nothing


  • No, not a problem with timing
  • No, not a problem with dispose
  • No, not a problem solved by MsgBox
  • No, not a problem with attributes
  • No, not a problem with current directory path
  • No, can't use Kill
  • No, it's not access denied
  • No, you don't have to shell out
  • No, MSDN documentation won't tell you

YOU JUST HAVE TO DELETE THE TRAILING BACKSLASH IN THE PATH BECAUSE DELETEFOLDER DOESN'T LIKE IT.

Now, feel free to shoot the messenger ...


@giodamelio Weirdly enough, I put the line msgBox dropzone before your code and it worked. When I comment out the msgbox it wont work?

  • Your code or pc is too fast! :D
  • You probably don't dispose/close files you use or store in that folder (or not closed correctly)


Check the attributes of the file and set to 0 if it's not already set.

set deletefolder = fso.GetFolder(dropzone)
if deletefolder.Attributes=0 then
 deletefolder.Delete(True)
else
 deletefolder.Attributes=0
 deletefolder.Delete(True)
end if
set fso = Nothing


It's very, very probably not an actual "access denied". More likely it's an "can't delete while files are open". Close any open references to that folder (text streams, processes you might have started, look carefully) and do

fso.DeleteFolder(dropzone)

See MSDN documentation on DeleteFolder().


try to use Kill("folder path")


for DeleteFolder to work, the current directory for the script should not be a part of the path of the folder to be deleted. Use Shell.CurrentDirectory to change the current directory to some system path and then call DeleteFolder. I have verified that this works. Check this.


If your folder path has a backslash, the method will throw an error! Most of my scripts have a backslash at the end of paths purposely using another function, so I removed the last character of my path (backslash) and viola!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜