Solve the problem that the folder cannot be deleted "folder access is denied. You need permission to perform this operation". Theoretically, this method can also be used to delete any folder

Posted by scratchwax2003 on Thu, 03 Feb 2022 21:10:47 +0100

Problem description

Sometimes some files in the disk will be set with complex permissions by some programs. Please forgive me for my lack of knowledge and unable to reasonably explain the permission system of Windows. The problem after this setting is that when you want to delete the folder, you will be prompted that the folder access is denied. You need permission to perform this operation. You need to come from?? To change this folder, as shown in the figure

What I encounter here is the problem that the windows apps folder cannot be deleted. I can't find many methods on Baidu and bing. My English expression is not good, and I don't find much on Google. I even tried to boot ubuntu and use sudo rm -rf to delete it, but it still didn't help.

Manual solution

In the process of exploration, I found that the general saying on the network is to set the permissions of the root folder, but I can't do it here. My situation is that if you want to delete every file in the folder, you can delete it by setting it in the following steps. Forgive me for my ignorance. Why do you need to manually set the file separately? This principle still can't be explained to everyone.

  1. Right click the property security advanced owner to change to Administrators
  2. Add the permission of Everyone in the permission entry below
  3. At this point, you can delete the file all the way

However, this method has an obvious disadvantage. In case there are many miscellaneous files, it will be time-consuming and laborious, so I wrote a small script in python to automatically complete the above operations. In theory, it can also be used to delete any folder. If the boss thinks it's unnecessary or has a convenient method, please spray it gently

Automatic deletion using python

No more nonsense. Go directly to the code. The logic run by the administrator has the following link for reference. Because I use the effect is very good, in order to prevent misoperation, I added a simple verification code verification mechanism. However, please bear the risk of using this script. I test to delete all kinds of folders with the above problems perfectly.

https://blog.csdn.net/xianjie0318/article/details/108604171

# coding:utf8
import sys
import os
import random
import subprocess
import ctypes

def getFiles(currentDir):
    files = []
    currentDirFiles = os.listdir(currentDir)
    for i in range(0,len(currentDirFiles)):
        currentPath = os.path.join(currentDir,currentDirFiles[i])
        if os.path.isdir(currentPath):
            files.extend(getFiles(currentPath))
        if os.path.isfile(currentPath):
             files.append(currentPath)
    return files

# https://blog.csdn.net/xianjie0318/article/details/108604171
def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

def main():
    print("Enter the folder to get permission and delete")
    dir = input()
    if not os.path.isdir(dir):
        print("The path is not a folder")
        input("Press enter to exit")
        exit(0)

    files = getFiles(dir)
    
    verifyCode = random.randint(100000, 999999)
    print("")
    print("")
    print(f"===!!!warning!!!===")
    print(f"Will try to get permission and delete it completely {dir} Content of")
    print(f"Please check the path and enter the confirmation code {verifyCode} Enter to confirm")
    inputVerifyCode = input()
    if inputVerifyCode != str(verifyCode):
        print("Confirmation code input error")
        input("Press enter to exit")
        exit(0)

    for each in files:
        subprocess.run(
            f"takeown /f {each} /a",
            shell=True)
        subprocess.run(
            f"icacls {each} /grant:r everyone:f",
            shell=True)
    subprocess.run(
        f"rmdir /s /q {dir}",
        shell=True)

    print("")
    print("")
    input("Press enter to exit after processing")

if is_admin():
    main()
else:
    if sys.version_info[0] == 3:
        ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
    else:#in python2.x
        ctypes.windll.shell32.ShellExecuteW(None, u"runas", unicode(sys.executable), unicode(__file__), None, 1)

Topics: Python Windows