import os
import shutil
folder_path = "指定文件夹路径"
# 删除所有快捷方式
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
if file_path.endswith(".lnk"):
os.remove(file_path)
# 创建新的快捷方式
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".exe"):
file_path = os.path.join(root, file)
shortcut_path = os.path.join(root, file.replace(".exe", ".lnk"))
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(shortcut_path)
shortcut.Targetpath = file_path
shortcut.WorkingDirectory = root
shortcut.IconLocation = file_path
shortcut.Save()
注意:该示例中使用了win32com库,需事先安装。
上一篇:遍历文件夹,但不涉及子文件夹