问题
I'm running into and issue that has many entries on Stack Overflow, but none of them solved my problem.
I have this piece of code, which tries to delete every .txt in a folder, but I keep getting a permission denied error on the "DeleteFile" command:
directory = "C:\TEST\FOLDER"
Set fso = CreateObject("Scripting.FileSystemObject")
Function DeleteTXTs
For Each f In fso.GetFolder(directory).Files
If LCase(fso.GetExtensionName(f)) = "txt" Then
fso.DeleteFile("C:\TEST\FOLDER\*.txt"),DeleteReadOnly
End If
Next
End Function
My permissions on the "C:\TEST" and subfolders are of full control to every account that exists in windows... What could I do?
回答1:
I have tested the code and it works correctly, is it worth adding in On Error Resume Next
error handling in the For
loop, as it may be a specific file that is causing the problem. A second test would be have you declared the DeleteReadOnly
correctly as when i removed this from the code i also received a "permission denied" when the code reached a read only .txt file. Finally if you have changed permissions on a folder have you done the old faithful reboot.
来源:https://stackoverflow.com/questions/21381367/deletefile-with-permission-denied