# -*- coding: utf-8 -*- """把 .bat 里查程序的那三行写干净。 上次替换留下了混搭: set "EXE=%~dp0ykxmz.exe" if not exist "%EXE%" set "EXE=%~dp0..\ykxmz.exe" if not exist "%EXE%" set "EXE=%~dp0LanRemote-Agent\ykxmz.exe" ^^^^^^^^^^^^^^^ 目录名还是旧的 改成:先找新的三个位置,再兜底老目录。 """ from __future__ import annotations import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") BATS = [ ROOT / "tools" / "wlzj.bat", ROOT / "tools" / "xffhq.bat", ] # 上次留下的混搭 BROKEN = ( 'set "EXE=%~dp0ykxmz.exe"\r\n' 'if not exist "%EXE%" set "EXE=%~dp0..\\ykxmz.exe"\r\n' 'if not exist "%EXE%" set "EXE=%~dp0LanRemote-Agent\\ykxmz.exe"' ) FIXED = ( 'set "EXE=%~dp0ykxmz.exe"\r\n' 'if not exist "%EXE%" set "EXE=%~dp0..\\ykxmz.exe"\r\n' 'if not exist "%EXE%" set "EXE=%~dp0ykxmz\\ykxmz.exe"\r\n' 'rem 兼容改名前的老目录:从旧版本升级上来的机器上可能还留着\r\n' 'if not exist "%EXE%" set "EXE=%~dp0LanRemote-Agent\\LanRemote-Agent.exe"\r\n' 'if not exist "%EXE%" set "EXE=%~dp0..\\LanRemote-Agent\\LanRemote-Agent.exe"' ) def main() -> int: ok = True for path in BATS: if not path.is_file(): print(f" [跳过] {path.name} 不存在") continue raw = path.read_bytes() try: text = raw.decode("gbk") except UnicodeDecodeError: print(f" [跳过] {path.name} 不是 GBK") ok = False continue # 统一成 \n 处理,最后再换回 \r\n normalized = text.replace("\r\n", "\n") broken = BROKEN.replace("\r\n", "\n") fixed = FIXED.replace("\r\n", "\n") if broken in normalized: normalized = normalized.replace(broken, fixed, 1) body = normalized.replace("\n", "\r\n") path.write_bytes(body.encode("gbk", errors="replace")) print(f" {path.name}: 查找逻辑已写干净") elif fixed in normalized: print(f" {path.name}: 已经是干净的") else: print(f" [手动看] {path.name}: 没匹配上,实际内容是:") for line in normalized.splitlines(): if "EXE=" in line: print(f" {line}") print() print(" 复核:") for path in BATS: if not path.is_file(): continue text = path.read_bytes().decode("gbk") print(f" --- {path.name} ---") for line in text.splitlines(): if 'set "EXE=' in line or "找不到" in line or "兼容" in line: print(f" {line}") return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())