Reputation:
I'm trying to hide all my files excluding .exe.
Below hides: files, exe
Does not hide: folders
I want: Hide folders, files
Does not hide: .exe
import os, shutil
import ctypes
folder = 'C:\\Users\\TestingAZ1'
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
ctypes.windll.kernel32.SetFileAttributesW(file_path, 2)
except Exception as e:
print(e)
I cannot use -onefile due to large size for each exe.
Upvotes: 3
Views: 299
Reputation: 3729
You almost got it ;)
import os
import ctypes
folder = 'C:\\Users\\TestingAZ1'
for item_name in os.listdir(folder):
item_path = os.path.join(folder, item_name)
try:
if os.path.isfile(item_path) and not item_name.lower().endswith('.exe'):
ctypes.windll.kernel32.SetFileAttributesW(item_path, 2)
elif os.path.isdir(item_path) and item_name not in ['.', '..']:
ctypes.windll.kernel32.SetFileAttributesW(item_path, 2)
except Exception as e:
print(e)
Looking at the documentation of SetFileAttributesW
, it can be used for folders as well. Which leave some "filtering". If your item is a file, you do not want to hide it if it ends on ".exe" or ".EXE". If it's a folder, you do not want to hide it if it is the folder you're in or its parent.
Upvotes: 4