for loop working in CMD prompt but not in batch file - for loop was copy pasted

泄露秘密 提交于 2019-12-18 05:56:23

问题


Note: I have almost no idea at all how batch file 'for' loops work.

The batch file I have currently:

"C:\Program Files (x86)\HMA! Pro VPN\bin\HMA! Pro VPN.exe" -connect
"C:\Program Files (x86)\HMA! Pro VPN\bin\HMA! Pro VPN.exe" -changeip  
ping -w 2000 -n 1 1.1.1.1
:wait
for /f "usebackq tokens=1,2,3,*" %A in (`netsh interface show interface`) do @if "%D"=="Local Area Connection 2" set state=%B
if %state%==Connected goto :end
goto :wait
:end

I'm trying to write a robust IP changer that utilizes HMA! Pro VPN - it should work whether the VPN client has been opened or not, and whether the VPN is currently opened or not, and should pause until the VPN has connected.

If you Google the for loop, you'll see it appear in a stackoverflow answer - this for loop works perfectly fine in a CMD prompt, and sets %state% to Connected/Disconnected well, but in a batch file, throws the following error:

D"=="Local was unexpected at this time.

I would learn more about for loops in batch had I not been under a very tight schedule - I have learnt about these before, but this loop looks pretty weird to me.


回答1:


In a batch file you have to use two percents for FOR parameters, that's all

for /f "usebackq tokens=1,2,3,*" %%A in (`netsh interface show interface`) do (
    if "%%D"=="Local Area Connection 2" set state=%%B
)


来源:https://stackoverflow.com/questions/11249339/for-loop-working-in-cmd-prompt-but-not-in-batch-file-for-loop-was-copy-pasted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!