2
votes

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)
deletefolder.Delete(True) 
set fso = Nothing
8

8 Answers

5
votes
  • 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 ...

2
votes

@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)
1
votes

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().

1
votes

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
0
votes

try to use Kill("folder path")

0
votes

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.

0
votes

i am still getting permission denied even after setting its attribute to 0 Its still coming as 16.

I know its not an answer. But i dont want to open a new thread for the same query. i tried doing task kill for a particular path also.enter image description here

Dim FSO, Folder
set FSO=CreateObject("Scripting.FileSystemObject")
Folder=WScript.Arguments(0)
Folder1=WScript.Arguments(1)
Folder2=WScript.Arguments(2)
File=WScript.Arguments(3)

If FSO.FolderExists(Folder) Then

set deletefolder = fso.GetFolder(Folder)

if deletefolder.Attributes=0 then
 deletefolder.Delete(True)
else


deletefolder.Attributes=0
msgbox deletefolder.Attributes 'Still coming as 16
 deletefolder.Delete(True)
end if

msgbox "Deleted"
FSO.CreateFolder(Folder)

End If

If FSO.FolderExists(Folder1) Then



FSO.CreateFolder(Folder1)

End If

If FSO.FolderExists(Folder2) Then



FSO.CreateFolder(Folder2)

End If

If Not FSO.FileExists(File) Then



FSO.CreateTextFile(File)


End If

WScript.StdOut.Write(File)



'FSO.CopyFolder WScript.Arguments(1), WScript.Arguments(0)&"\"
'msgbox Day(Date)
'FSO.CreateTextFile Folder&"\"&"LogPR_To_PO_"&Day(Date)&"_"&Month(Date)&"_"&Year(Date)&".txt"


Set FSO=nothing
-1
votes

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!