请问这个维护 hosts 文件的 bat 哪里写的不对? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
Foxkeh
0.01D
V2EX    程序员

请问这个维护 hosts 文件的 bat 哪里写的不对?

  •  1
     
  •   Foxkeh 2024-11-27 11:13:57 +08:00 2177 次点击
    这是一个创建于 367 天前的主题,其中的信息可能已经有所发展或是发生改变。

    自己喜欢折腾 hosts 文件,比如下面这是 Postman7 系列最终版屏蔽更新用的. 目前已经可以通过 PowerShell 可以正常维护 hosts 文件 虽然 PowerShell 更方便语法也更简洁,但是需要额外执行一次 set-ExecutionPolicy RemoteSigned 才可以运行脚本感觉还是有点麻烦,所以还是折腾下 bat 脚本文件, 但是输出有问题但不知道出在哪

    @echo off setlocal enabledelayedexpansion chcp 65001 > nul :: 提权到管理员权限 net session >nul 2>&1 if %errorlevel% neq 0 ( echo Requesting administrative privileges... powershell -Command "Start-Process '%~0' -Verb runAs" exit /B ) :: 定义要添加的规则列表 set "rules[0]=127.0.0.1 dl.pstmn.io" set "rules[1]=127.0.0.1 onboarding.postman.com" set "rules[2]=127.0.0.1 analytics.getpostman.com" set "rules[3]=127.0.0.1 assets.getpostman.com" set "rules[4]=127.0.0.1 static.getpostman.com" set "rules[5]=127.0.0.1 skills.gw.postman.com" set "rules[6]=127.0.0.1 skills-assets.pstmn.io" set "rules[7]=127.0.0.1 skills.getpostman.com" set "rules[8]=127.0.0.1 config.getpostman.com" set "rules[9]=127.0.0.1 cdn.postman.com" set "rules[10]=127.0.0.1 getpostman.com" set "rules[11]=127.0.0.1 www.postman.com" set "rules[12]=#127.0.0.1 identity.getpostman.com" set "rules[13]=#127.0.0.1 sync-v3.getpostman.com" :: 设置 hosts 文件路径 set "hostsFile=%windir%\System32\drivers\etc\hosts" :: 备份原始 hosts 文件 copy "%hostsFile%" "%hostsFile%.bak" >nul :: 移除 hosts 文件的只读属性 attrib -R "%hostsFile%" :: 读取每条规则并进行处理 for %%a in (0,1,13) do ( set "line=!rules[%%a]!" echo Current: !line! :: 检查该规则是否已存在于 hosts 文件中 findstr /C:"!line!" "%hostsFile%" >nul if errorlevel 1 ( :: 如果未找到,则添加到 hosts 文件 echo !line! >> "%hostsFile%" echo Added: !line! ) else ( echo Skipped: !line! already exists ) ) :: 重新添加 hosts 文件的只读属性 attrib +R "%hostsFile%" echo Done. endlocal pause 

    不知道为啥一共 13 条记录输出界面只打印下面 3 条,相应的 hosts 文件里也只出现了这三条, 百思不得其解, 求指教

    Current: 127.0.0.1 dl.pstmn.io Skipped: 127.0.0.1 dl.pstmn.io already exists Current: 127.0.0.1 onboarding.postman.com Skipped: 127.0.0.1 onboarding.postman.com already exists Current: #127.0.0.1 sync-v3.getpostman.com Skipped: #127.0.0.1 sync-v3.getpostman.com already exists Done. Press any key to continue . . . 
    11 条回复    2024-11-27 21:49:55 +08:00
    FrankFang128
        1
    FrankFang128  
       2024-11-27 11:18:46 +08:00
    问 AI 吧
    javalaw2010
        2
    javalaw2010      2024-11-27 11:18:53 +08:00
    虽然我不懂 bat 语法,但是根据你的代码和输出,我猜应该是 for %%a in (0,1,13)这句的问题
    javalaw2010
        3
    javalaw2010  
       2024-11-27 11:21:01 +08:00   1
    刚查了下,也许你应该使用 for /l 语法。
    Foxkeh
        4
    Foxkeh  
    OP
       2024-11-27 11:21:25 +08:00
    @FrankFang128 问过 AI 了,这个脚本都是 AI(chatgpt 和讯飞星火)帮助生成的,我完全不会 bat 语法
    cc666
        5
    cc666  
       2024-11-27 11:24:31 +08:00   1
    for /L %%a in (0,1,13) do (
    wwwz
        6
    wwwz  
       2024-11-27 11:35:50 +08:00   1
    问题点 1:for 循环范围错误
    for %%a in (0,1,13) 的写法实际上会将 (0,1,13) 作为一个单独的列表处理,而不是逐步迭代 0 到 13 。

    解决办法:应使用一个范围循环或明确列出需要的索引:

    for /L %%a in (0,1,13) do (
    或:

    for %%a in (0 1 2 3 4 5 6 7 8 9 10 11 12 13) do (
    tangtang666
        7
    tangtang666  
       2024-11-27 12:22:47 +08:00   1
    直接用 switchhosts 这个软件就行
    niuin969
        8
    niuin969  
       2024-11-27 13:08:13 +08:00   1
    PowerToys 也可以
    bluforduwjldz1
        9
    bluforduwjldz1  
       2024-11-27 13:14:32 +08:00   1
    问题出在 for 循环的范围设置上。在您的代码中:

    batch

    复制
    for %%a in (0,1,13) do
    这行代码只会循环 3 次,分别处理索引 0 、1 和 13 的规则。在批处理中,这种写法不会遍历 0 到 13 之间的所有数字。

    要遍历所有 13 条规则,您可以使用以下修改后的代码:
    ```bash
    @echo off
    setlocal enabledelayedexpansion
    chcp 65001 > nul

    :: 提权到管理员权限
    net session >nul 2>&1
    if %errorlevel% neq 0 (
    echo Requesting administrative privileges...
    powershell -Command "Start-Process '%~0' -Verb runAs"
    exit /B
    )

    :: 定义要添加的规则列表
    set "rules[0]=127.0.0.1 dl.pstmn.io"
    set "rules[1]=127.0.0.1 onboarding.postman.com"
    set "rules[2]=127.0.0.1 analytics.getpostman.com"
    set "rules[3]=127.0.0.1 assets.getpostman.com"
    set "rules[4]=127.0.0.1 static.getpostman.com"
    set "rules[5]=127.0.0.1 skills.gw.postman.com"
    set "rules[6]=127.0.0.1 skills-assets.pstmn.io"
    set "rules[7]=127.0.0.1 skills.getpostman.com"
    set "rules[8]=127.0.0.1 config.getpostman.com"
    set "rules[9]=127.0.0.1 cdn.postman.com"
    set "rules[10]=127.0.0.1 getpostman.com"
    set "rules[11]=127.0.0.1 www.postman.com"
    set "rules[12]=#127.0.0.1 identity.getpostman.com"
    set "rules[13]=#127.0.0.1 sync-v3.getpostman.com"

    :: 设置 hosts 文件路径
    set "hostsFile=%windir%\System32\drivers\etc\hosts"

    :: 备份原始 hosts 文件
    copy "%hostsFile%" "%hostsFile%.bak" >nul

    :: 移除 hosts 文件的只读属性
    attrib -R "%hostsFile%"

    :: 读取每条规则并进行处理
    for /L %%a in (0,1,13) do (
    set "line=!rules[%%a]!"
    echo Current: !line!
    :: 检查该规则是否已存在于 hosts 文件中
    findstr /C:"!line!" "%hostsFile%" >nul
    if errorlevel 1 (
    :: 如果未找到,则添加到 hosts 文件
    echo !line! >> "%hostsFile%"
    echo Added: !line!
    ) else (
    echo Skipped: !line! already exists
    )
    )

    :: 重新添加 hosts 文件的只读属性
    attrib +R "%hostsFile%"

    echo Done.
    endlocal
    pause
    ```

    主要修改是将:

    ```bash
    for %%a in (0,1,13) do
    ```
    修改为
    ```bash
    for /L %%a in (0,1,13) do
    ```

    使用 /L 参数后,格式为 (start,step,end),这样会从 0 开始,每次加 1 ,一直到 13 ,从而处理所有的规则。这样修改后,脚本将会处理所有 14 条规则(索引 0 到 13 )。
    Foxkeh
        10
    Foxkeh  
    OP
       2024-11-27 14:49:56 +08:00
    @javalaw2010
    @cc666
    @wwwz
    @bluforduwjldz1
    多谢多谢各位大侠指点[手动抱拳]
    xqzr
        11
    xqzr  
       2024-11-27 21:49:55 +08:00
    @Livid #6 #9 AI
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2631 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 14:53 PVG 22:53 LAX 06:53 JFK 09:53
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86