V2EX bluforduwjldz1
 bluforduwjldz1 最近的时间轴更新
bluforduwjldz1

bluforduwjldz1

V2EX 第 553071 号会员,加入于 2021-08-10 21:10:22 +08:00
今日活跃度排名 8525
bluforduwjldz1 最近回复了
2024-11-27 13:14:32 +08:00
回复了 Foxkeh 创建的主题 程序员 请问这个维护 hosts 文件的 bat 哪里写的不对?
问题出在 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 )。
关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     791 人在线   最高记录 6679       Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 11ms UTC 20:28 PVG 04:28 LAX 12:28 JFK 15:28
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