windos.bat 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. :: Copyright (c)2022 https://maruifu.cn
  2. :: This is a batch script for fixing Google Translate and making it available
  3. :: in the Chinese mainland. If you experience any problem, visit the page below:
  4. :: http://maruifu.cn/article/251
  5. @echo off
  6. setlocal enabledelayedexpansion
  7. chcp 437 >NULL
  8. set "source_domain=google.cn"
  9. set "target_domain=translate.googleapis.com"
  10. set "hosts_file=C:\Windows\System32\drivers\etc\hosts"
  11. for /f "skip=4 tokens=2" %%a in ('"nslookup %source_domain% 2>NUL"') do set ip=%%a
  12. set "old_rule=null"
  13. set "new_rule=%ip% %target_domain%"
  14. set "comment=# Fix Google Translate CN"
  15. for /f "tokens=*" %%i in ('type %hosts_file%') do (
  16. set "line=%%i"
  17. :: Retrieve the rule If the target domain exists.
  18. if not "!line:%target_domain%=!"=="%%i" set "old_rule=%%i"
  19. )
  20. if not "%old_rule%"=="null" (
  21. echo A rule has been added to the hosts file.
  22. echo [1] Update [2] Delete
  23. set /p action="Enter a number to choose an action: "
  24. if "!action!"=="1" (
  25. if not "%old_rule%"=="%new_rule%" (
  26. echo Deleting the rule "%old_rule%"
  27. echo Adding the rule "%new_rule%"
  28. set "new_line=false"
  29. for /f "tokens=*" %%i in ('type %hosts_file% ^| find /v /n "" ^& break ^> %hosts_file%') do (
  30. set "rule=%%i"
  31. set "rule=!rule:*]=!"
  32. if "%old_rule%"=="!rule!" set "rule=%new_rule%"
  33. if "!new_line!"=="true" >>%hosts_file% echo.
  34. >>%hosts_file% <NUL set /p="!rule!"
  35. set "new_line=true"
  36. )
  37. ) else (
  38. echo The rule already exists, nothing to do.
  39. )
  40. )
  41. if "!action!"=="2" (
  42. echo Deleting the rule "%old_rule%"
  43. set "new_line=false"
  44. for /f "tokens=*" %%i in ('
  45. type "%hosts_file%" ^| findstr /v /c:"%comment%" ^| findstr /v "%target_domain%" ^| find /v /n "" ^& break ^> "%hosts_file%"
  46. ') do (
  47. set "line=%%i"
  48. set "line=!line:*]=!"
  49. if "!new_line!"=="true" >>%hosts_file% echo.
  50. >>%hosts_file% <NUL set /p="!line!"
  51. set "new_line=true"
  52. )
  53. )
  54. ) else (
  55. echo Adding the rule "%new_rule%"
  56. echo.>>%hosts_file%
  57. echo %comment%>>%hosts_file%
  58. <NUL set /p="%new_rule%">>%hosts_file%
  59. )
  60. echo Done.
  61. pause