DeleteFile with Permission Denied

守給你的承諾、 提交于 2021-02-05 07:56:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!