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.
For loop (default) of Batch language is used to iterate over a list of files. Example: copy some files into a directory (Note: the files to be copied into the target directory need to be in the same disk drive).
FOR /D - Loop through several folders. FOR /L - Loop through a range of numbers. FOR /F - Loop through items in a text file. FOR /F - Loop through the output of a command.
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
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With