# -*- coding: utf-8 -*- """修 .bat 里的旧程序名。 发现: tools/fxfhq.bat、tools/wlzj.bat、tools/xffhq.bat 都是**改名之前** 生成的,里面还在找 LanRemote-Agent.exe。 程序已经叫 ykxmz.exe 了,这些脚本一跑就"找不到程序" —— 真 bug。 编码约定(这部分是对的,不动): .bat 用 **GBK** + CRLF + `chcp 936`。 这是中文 Windows 下 cmd.exe 的正确做法: 控制台默认代码页 936,UTF-8 的中文在 cmd 里会变乱码。 顺手把生成它们的脚本也改了,免得下次重新生成又变回旧名。 """ from __future__ import annotations import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") BATS = [ ROOT / "tools" / "fxfhq.bat", ROOT / "tools" / "wlzj.bat", ROOT / "tools" / "xffhq.bat", ROOT / "windows" / "放行防火墙_以管理员运行.bat", ] # 生成它们的脚本(下次重新生成时用得上) GENERATORS = [ ROOT / "_build" / "fix_netcheck_output.py", ROOT / "_build" / "make_fix_bat.py", ROOT / "_build" / "make_toolbox.py", ROOT / "_build" / "rename_tools.py", ] # 兼容旧名:查找 EXE 时把两个名字都试一遍, # 这样从老版本升级上来的目录也能找到程序 LOOKUP_OLD = '''set "EXE=%~dp0LanRemote-Agent.exe" if not exist "%EXE%" set "EXE=%~dp0..\\LanRemote-Agent.exe" if not exist "%EXE%" set "EXE=%~dp0LanRemote-Agent\\LanRemote-Agent.exe"''' LOOKUP_NEW = '''set "EXE=%~dp0ykxmz.exe" if not exist "%EXE%" set "EXE=%~dp0..\\ykxmz.exe" if not exist "%EXE%" set "EXE=%~dp0ykxmz\\ykxmz.exe" rem 兼容改名前的老目录(升级上来的机器上可能还留着) if not exist "%EXE%" set "EXE=%~dp0LanRemote-Agent.exe" if not exist "%EXE%" set "EXE=%~dp0..\\LanRemote-Agent.exe"''' def patch_bat(path: Path) -> int: if not path.is_file(): return 0 raw = path.read_bytes() # 按 GBK 解(这些文件本来就是 GBK) try: text = raw.decode("gbk") except UnicodeDecodeError: print(f" [跳过] {path.name}: 不是 GBK") return 0 original = text # 1. 整段查找逻辑(带老目录兜底) if LOOKUP_OLD in text: text = text.replace(LOOKUP_OLD, LOOKUP_NEW) # 2. 其余零散出现 text = text.replace("LanRemote-Agent.exe", "ykxmz.exe") text = text.replace('LanRemote-Agent\\\\LanRemote-Agent', 'ykxmz\\\\ykxmz') if text == original: print(f" {path.name}: 没有旧名") return 0 count = original.count("LanRemote-Agent") # 写回:GBK + CRLF body = text.replace("\r\n", "\n").replace("\n", "\r\n") path.write_bytes(body.encode("gbk", errors="replace")) print(f" {path.name}: 改了 {count} 处(GBK + CRLF 保持)") return count def main() -> int: total = 0 print("一、修 .bat 内容") for path in BATS: total += patch_bat(path) print() print("二、修生成它们的脚本(免得重新生成又变回旧名)") for path in GENERATORS: if not path.is_file(): continue text = path.read_text(encoding="utf-8") if "LanRemote-Agent" not in text: print(f" {path.name}: 干净") continue count = text.count("LanRemote-Agent") text = text.replace("LanRemote-Agent.exe", "ykxmz.exe") text = text.replace("LanRemote-Agent\\\\LanRemote-Agent", "ykxmz\\\\ykxmz") path.write_text(text, encoding="utf-8", newline="\n") print(f" {path.name}: 改了 {count} 处") print() print("三、复核 .bat") ok = True for path in BATS: if not path.is_file(): continue raw = path.read_bytes() try: text = raw.decode("gbk") except UnicodeDecodeError as exc: print(f" [坏] {path.name}: GBK 解不开 {exc}") ok = False continue crlf = raw.count(b"\r\n") lf_only = raw.count(b"\n") - crlf old_name = "LanRemote-Agent" in text print(f" {path.name}: GBK ✓ CRLF {crlf} 纯LF {lf_only} " f"旧名 {'还有!' if old_name else '无'}") if lf_only or old_name: ok = False return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())