Loading... 最近C盘慢慢爆红,当时玩电脑的时候学着网上的教程,说硬盘分区是一个好的习惯。因此我就把1T的固态,只分了200G给C盘,但是随着各种莫名奇妙的软件都喜欢偷偷的占用一点C盘的位置,即使我尽量把能装到D盘和能放到D盘的软件全都放到D盘,但是还是避免不了的C盘越用越多,所以到底是谁的问题? 所以我决定,合并分区,但是懒得重装系统,故折腾一个上午,把所有的东西拷贝出到移动固态硬盘,然后删除D卷,最后C卷拓展分区,这下C盘就舒服了,这个完美丝滑的享受这一个T,不用再每次想着分DC的事情了。我觉得着这现在对于我这小巧的笔记本来说,利大于弊。(合并分区网上教程很多,暂时没放,去搜搜吧)。 这里讲我考完后遇到的一些问题。并借助AI和代码批量化处理问题。 事先声名一下,一下代码都是根据我的路径来的,小白来了,记得参照一下修改路径成你的才能使用。 old_path = "D:\\\\" 是我的D盘的原始路径 new_path = "C:\\\D\\\\" 是我迁移后的保存路径 > 1、桌面快捷方式怎么变白了,如何处理?D盘文件和软件都移到了C盘下的文件夹D内,我现在怎么把所有图标文件的属性目标路劲的头从原始的"D:/"全部换成"C:\D\",所有的图标文件都在TOOLS文件夹内。写代码实现。 # 修改lnk文件的目标路径 ```python # -*- coding: utf-8 -*- import os import win32com.client def update_shortcuts(folder_path, old_prefix, new_prefix): shell = win32com.client.Dispatch("WScript.Shell") for filename in os.listdir(folder_path): if filename.endswith(".lnk"): # 只处理 .lnk 文件 shortcut_path = os.path.join(folder_path, filename) shortcut = shell.CreateShortcut(shortcut_path) if shortcut.TargetPath.startswith(old_prefix): new_target = shortcut.TargetPath.replace(old_prefix, new_prefix, 1) shortcut.TargetPath = new_target shortcut.Save() print(f"Updated: {filename} -> {new_target}") else: print(f"Skipped: {filename} (Path does not match)") if __name__ == "__main__": tools_folder = r"C:\D\asus\桌面\TOOLS" # 替换成你的 TOOLS 目录 update_shortcuts(tools_folder, "D:\\", "C:\\D\\") ``` 执行完这段代码后的效果是再次点击桌面快捷方式又能运行软件喽。  > 图标变白,显然是图标路径找不到了,还是原来没有修改,所以还需要批量修改! # 修改lnk文件图标 ```python # -*- coding: utf-8 -*- import os import win32com.client def update_shortcut_icon_paths(folder_path, old_prefix, new_prefix): if not os.path.exists(folder_path): print(f"错误:目录 {folder_path} 不存在,请检查路径!") return shell = win32com.client.Dispatch("WScript.Shell") for filename in os.listdir(folder_path): if filename.endswith(".lnk"): # 只处理 .lnk 文件 shortcut_path = os.path.join(folder_path, filename) shortcut = shell.CreateShortcut(shortcut_path) # 获取当前的图标路径 old_icon = shortcut.IconLocation # 仅修改以 D:\ 开头的图标路径 if old_icon.startswith(old_prefix): new_icon = old_icon.replace(old_prefix, new_prefix, 1) shortcut.IconLocation = new_icon shortcut.Save() print(f"已修改: {filename} -> {new_icon}") else: print(f"跳过: {filename} (图标路径不匹配)") if __name__ == "__main__": tools_folder = r"C:\D\asus\桌面\TOOLS" # 你的快捷方式所在目录 update_shortcut_icon_paths(tools_folder, "D:\\", "C:\\D\\") ``` 执行完这段代码后,软件图标又回来了咯。  > 我把软件都移动了,在windows的控制面板卸载或更改程序中,所有软件它都还是去D盘索引,但是D盘已近不在了,这怎么解决? # 修改注册表 ### 方法一: - **打开注册表编辑器**: - 按 `Win + R`,输入 `regedit`,回车。 - **找到已安装程序的路径**: - **32 位软件路径**: mathematica 复制编辑 `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall` - **64 位软件路径**: mathematica 复制编辑 `HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall` - **在 `Uninstall` 目录下查找你的软件**: - 可能是软件名称,比如 `Google Chrome` - 也可能是 GUID(形如 `{1234-ABCD-5678-XYZ}`) - **修改软件的 `InstallLocation` 或 `UninstallString`**: - 找到 `InstallLocation` 或 `UninstallString`,把 `D:\` 改成 `C:\D\`。 - 可能还需要修改 `DisplayIcon`(显示的软件图标路径)。 - **关闭注册表,重启电脑**,检查控制面板是否更新了路径。 ## 方法二: ```python import winreg def update_registry_paths(reg_base, old_prefix, new_prefix): try: with winreg.OpenKey(reg_base, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", 0, winreg.KEY_READ | winreg.KEY_WRITE) as uninstall_key: count = 0 for i in range(winreg.QueryInfoKey(uninstall_key)[0]): try: subkey_name = winreg.EnumKey(uninstall_key, i) with winreg.OpenKey(uninstall_key, subkey_name, 0, winreg.KEY_READ | winreg.KEY_WRITE) as subkey: for value_name in ["InstallLocation", "Inno Setup: App Path", "QuietUninstallString", "UninstallString", "DisplayIcon"]: try: value, value_type = winreg.QueryValueEx(subkey, value_name) if isinstance(value, str): # 1. 记录是否有引号 has_quotes = value.startswith('"') and value.endswith('"') # 2. 处理 DisplayIcon,可能带 `,0` 形式 if value_name == "DisplayIcon": if "," in value: path_part, extra_part = value.split(",", 1) # 分离路径和索引值 path_part = path_part.strip('"') # 去掉路径部分的引号 extra_part = extra_part.strip('"') # 去掉索引部分的引号 if path_part.startswith(old_prefix): new_path_part = path_part.replace(old_prefix, new_prefix, 1) new_value = f'{new_path_part},{extra_part}' # 不加引号 else: path_part = value.strip('"') if path_part.startswith(old_prefix): new_value = path_part.replace(old_prefix, new_prefix, 1) if " " in new_value: # 如果有空格,加引号 new_value = f'"{new_value}"' print(f"已修改 {value_name}: {value} -> {new_value}") else: # 3. 处理普通路径(InstallLocation, UninstallString等) raw_value = value.strip('"') if raw_value.startswith(old_prefix): new_value = raw_value.replace(old_prefix, new_prefix, 1) if has_quotes or " " in new_value or value_name in ["QuietUninstallString", "UninstallString"]: new_value = f'"{new_value}"' # 这些路径需要带引号 # 4. 处理 QuietUninstallString / UninstallString 结尾多余的引号 if value_name in ["QuietUninstallString", "UninstallString"] and new_value.endswith('"') and " " in new_value.strip('"'): first_space_index = new_value.find(' ') if first_space_index != -1 and first_space_index < len(new_value) - 1: new_value = new_value[:-1] # 移除最后的 `"` print(f"已修改 {value_name}: {value} -> {new_value}") # 5. 写回注册表 winreg.SetValueEx(subkey, value_name, 0, value_type, new_value) count += 1 except FileNotFoundError: pass # 某些软件可能没有这个属性,跳过 except OSError: break # 避免访问越界 print(f"完成!共修改 {count} 处路径") except Exception as e: print(f"错误: {e}") if __name__ == "__main__": old_path = "D:\\" # 旧路径 new_path = "C:\\D\\" # 新路径 print("正在修改 64 位软件路径...") update_registry_paths(winreg.HKEY_LOCAL_MACHINE, old_path, new_path) print("正在修改 32 位软件路径...") update_registry_paths(winreg.HKEY_LOCAL_MACHINE, old_path, new_path) ``` 以上代码好像只修改了64位的,下面这是全新升级版。而且再次运行第二次会报错,而不是正常提示修改完成,没有什么可以修改的,以下代码解决了这个bug。 ```python import winreg def update_registry_paths(reg_base, subkey_path, old_prefix, new_prefix, access_flag): try: with winreg.OpenKey(reg_base, subkey_path, 0, winreg.KEY_READ | winreg.KEY_WRITE | access_flag) as uninstall_key: count = 0 # 记录修改次数 for i in range(winreg.QueryInfoKey(uninstall_key)[0]): try: subkey_name = winreg.EnumKey(uninstall_key, i) with winreg.OpenKey(uninstall_key, subkey_name, 0, winreg.KEY_READ | winreg.KEY_WRITE | access_flag) as subkey: for value_name in ["InstallLocation", "Inno Setup: App Path", "QuietUninstallString", "UninstallString", "DisplayIcon", "UninstallDataFile"]: try: value, value_type = winreg.QueryValueEx(subkey, value_name) if isinstance(value, str): has_quotes = value.startswith('"') and value.endswith('"') new_value = value # 先赋默认值,避免变量未定义 if value_name == "DisplayIcon": if "," in value: path_part, extra_part = value.split(",", 1) path_part = path_part.strip('"') extra_part = extra_part.strip('"') if path_part.startswith(old_prefix): new_path_part = path_part.replace(old_prefix, new_prefix, 1) new_value = f'{new_path_part},{extra_part}' else: path_part = value.strip('"') if path_part.startswith(old_prefix): new_value = path_part.replace(old_prefix, new_prefix, 1) if " " in new_value: new_value = f'"{new_value}"' else: raw_value = value.strip('"') if raw_value.startswith(old_prefix): new_value = raw_value.replace(old_prefix, new_prefix, 1) if has_quotes or " " in new_value or value_name in ["QuietUninstallString", "UninstallString"]: new_value = f'"{new_value}"' if value_name in ["QuietUninstallString", "UninstallString"] and new_value.endswith('"') and " " in new_value.strip('"'): first_space_index = new_value.find(' ') if first_space_index != -1 and first_space_index < len(new_value) - 1: new_value = new_value[:-1] if new_value != value: print(f"已修改 {value_name}: {value} -> {new_value}") winreg.SetValueEx(subkey, value_name, 0, value_type, new_value) count += 1 except FileNotFoundError: pass # 某些键可能不存在,跳过 # **处理 UninstallDataFile** uninstall_data_file = None new_value = None try: uninstall_data_file, value_type = winreg.QueryValueEx(subkey, "UninstallDataFile") except FileNotFoundError: uninstall_data_file = None # 确保即使键不存在,后续不会出错 if uninstall_data_file: raw_value = uninstall_data_file.strip('"') if raw_value.startswith(old_prefix): new_value = raw_value.replace(old_prefix, new_prefix, 1) if " " in new_value: new_value = f'"{new_value}"' if new_value and new_value != uninstall_data_file: print(f"已修改 UninstallDataFile: {uninstall_data_file} -> {new_value}") winreg.SetValueEx(subkey, "UninstallDataFile", 0, winreg.REG_SZ, new_value) count += 1 except OSError: break # 避免访问超出索引 if count == 0: print("未找到需要修改的路径,注册表保持不变。") else: print(f"完成!共修改 {count} 处路径") except Exception as e: print(f"错误: {e}") if __name__ == "__main__": old_path = "D:\\" new_path = "C:\\D\\" print("正在修改 64 位软件路径...") update_registry_paths(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", old_path, new_path, winreg.KEY_WOW64_64KEY) print("正在修改 32 位软件路径...") update_registry_paths(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall", old_path, new_path, winreg.KEY_WOW64_32KEY) ```  ## 方法三: 如果你不想写代码,可以使用 PowerShell **一键替换所有 `D:\` 路径**: 1. **打开 PowerShell(管理员权限)** 2. **运行以下命令** ```PowerShell $keys = @( "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*", "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" ) foreach ($key in $keys) { Get-ItemProperty -Path $key -ErrorAction SilentlyContinue | ForEach-Object { if ($_.InstallLocation -match "^D:\\") { $newPath = $_.InstallLocation -replace "^D:\\", "C:\D\" Set-ItemProperty -Path $key -Name InstallLocation -Value $newPath Write-Host "已修改: $($_.InstallLocation) -> $newPath" } } } ``` 1. **重启电脑**,然后检查 **控制面板 → 卸载或更改程序** 是否更新。 最后,还是很愉快的用geek,cclean,UninstallTool工具删除重装了一些软件,这就是今天学到的哈哈! 最后修改:2025 年 03 月 04 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 7 如果觉得我的文章对你有用,请随意赞赏