0

I wrote a batch process via bingchat, About Adding Proxy Automation for WSA,and one of the functions performs well when carried out on its own, but an error occurs when adding it to an if statement, and the error associated with it is FINDSTR: Cannot open。I'm totally new to this.

this is code

@echo off

:menu
cls
echo 1. Set Android proxy to local IP address
echo 2. Cancel Android proxy
set /p choice=Enter your choice (1 or 2):
if "%choice%"=="1" (
    echo Your local IP addresses are:
    ipconfig | findstr /c:"IPv4 Address"
    set /p ip=Please enter the number of the IP address you want to use as the proxy:
    for /f "tokens=2 delims=: " %%f in ('ipconfig ^| findstr /c:"IPv4 Address"') do (
        set /a count+=1
        if !count! == %ip% set ip=%%f
    )
    setlocal enableextensions
    set "configFile=%userprofile%\.config\clash\config.yaml"
    for /f "tokens=2 delims=: " %%a in (' findstr /c:"mixed-port" "%configFile%"') do set "port=%%a"
    echo The port number of Clash is %port%.
    adb shell settings put global http_proxy %ip%:%port%
    adb shell settings put global https_proxy %ip%:%port%
    echo Proxy has been set to %ip%:%port%.
    pause
    endlocal
) else if "%choice%"=="2" (
    adb shell settings put global http_proxy :0
    adb shell settings put global https_proxy :0
    echo Proxy has been cancelled.
) else (
    echo Invalid choice. Please enter 1 or 2.
    pause
    goto menu
)
pause

The port port code segment in search yaml can work normally when it is taken out alone, but it will display the error of FINDSTR: Cannot open when it is put into the if statement. I try to add something like the if statement to the code segment that can run normally. Brackets, an error occurred in the result, I don't know how the brackets work, and I didn't find the corresponding article on the Internet

Compo
  • 36,585
  • 5
  • 27
  • 39
  • You have delayed expansion problems. Change all `%ip%`, `%port%`, and `%configFile%` variables to `!ip!`, `!port!`, and `!configFile!` respectively. Please also stop using `set /p` for known single key entry, use `choice.exe` instead. Please open a Command Prompt window, type `%SystemRoot%\System32\choice.exe /?`, and press the `[ENTER]` key, to find out how to use it. – Compo Aug 17 '23 at 09:46
  • thanks!i replace setlocal enableextensions to setlocal enabledelayedexpansion,and change % to !;I will try – micicipi Aug 17 '23 at 10:08
  • Simply doing that is not enough, you need to replace it as you've mentioned, and move it to a location before you use `!count!`. – Compo Aug 17 '23 at 12:34
  • I strongly recommend to read [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) There is explained in full details how to code in a batch file a choice menu being fail-safe and secure in usage, very handy for the users and which makes coding a batch file easier by avoiding command blocks beginning with `(` and ending with matching `)`. The batch file development becomes much easier as delayed variable expansion is not needed at all. – Mofi Aug 17 '23 at 16:43
  • For example, your current code prompts the user for entering __1__ or __2__. On a German keyboard where the CapsLock key is currently active and there is not used the German (IBM) keyboard layout, a user might press quickly the key __2__ and next __RETURN__ and wonders now why the console window closed suddenly. The reason is that the user entered `"` instead of __2__ and that causes a syntax error on the __IF__ condition which becomes `if """ == "1" (`. The *Windows Command Processor* outputs an error message which the user can't see, exits the batch file processing and closes itself. – Mofi Aug 17 '23 at 16:50

0 Answers0