file_directory = r'C:\New Folder' ### Provide your directory here where it contains the files ### 
Option = "Add" ### please provide your option here whether you want to add or remove your files (Add or Remove) ##
files_path = [r'C:\New folder\New folder (3)\vlc-3.0.16-win32.exe',r'C:\New folder\vicky.com'] ### if you want to remove files please provide the file path here ###
import os 
import subprocess
import ctypes

control_folder_files = []
class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)

with disable_file_system_redirection():
	if Option.capitalize() == 'Add':
		if os.path.exists(file_directory):
			cmd = 'dir "'+file_directory+'" /s /b /o:gn'
			access_files = os.popen(cmd).read().splitlines()
			for i in access_files:
				if i.strip().endswith('.com') or i.strip().endswith('.exe') or i.strip().endswith('.EXE') or i.strip().endswith('.COM') or i.strip().endswith('.Exe') or i.strip().endswith('.Com'):
					control_folder_files.append(i.strip())
				else:
					pass 

			if len(control_folder_files) > 0:
				for j in control_folder_files:
					cmd = 'PowerShell Add-MpPreference -ControlledFolderAccessAllowedApplications '+"'"+j+"'"
					acc = os.popen(cmd).read()
					print(j+' added to Controlled Folder access')
			else:
				print("No .exe or .com files found in "+file_directory)
		else:
			print("Please provide correct directory..")

	elif Option.capitalize() == 'Remove':
		if len(files_path) > 0:
			for i in files_path:
				cmd = 'PowerShell Remove-MpPreference -ControlledFolderAccessAllowedApplications '+"'"+i+"'"
				acc = os.popen(cmd).read()
				print(i+' removed from Controlled Folder access')
		else:
			print("Please provide files path to remove")

	else:
		print("Please provide correct option either 'Add' or 'Remove'")
			