0

I'm looking to find path with client_1 string, replace client_1 part with client_1\bin, and set PATH=%PATH%;%newpath%.

I've tried

@echo off
setlocal EnableDelayedExpansion

set newpath=
for %%a in ("%path:;=";"%") do (
    rem @echo %%~a
    if [%newpath%]==[] (
        echo %%~a | findstr /I /C:"client_1" >nul
        if not errorlevel 1 (
            set newpath=%%~a
            set newpath=!newpath:client_1\bin=client_1!
        )
    )
)

set PATH=%PATH%;%newpath%
Compo
  • 36,585
  • 5
  • 27
  • 39
mpapec
  • 50,217
  • 8
  • 67
  • 127
  • 1
    Does this answer your question? [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) Specifically, since you're setting and using a variable inside of the same set of parentheses, you need to add `setlocal enabledelayedexpansion` to the top of your script and use `!newpath:client_1\bin=client_1!` instead of `%newpath:client_1\bin=client_1%`. – SomethingDark Jun 30 '23 at 13:26
  • @SomethingDark no, it's still failing – mpapec Jun 30 '23 at 13:46
  • Could you elaborate on how it's failing? Is the variable not getting created at all? – SomethingDark Jun 30 '23 at 13:55
  • @SomethingDark substitution wasn't working as expected. – mpapec Jul 01 '23 at 07:18
  • Could you please explain what you're seeing and what you're expecting to see? I'd expect someone with 50k rep to know better than to respond with "it isn't working" when asked for elaboration. – SomethingDark Jul 01 '23 at 16:10
  • Check the answer below. – mpapec Jul 01 '23 at 16:20

1 Answers1

0

Substitution part was also wrong beside missing EnableDelayedExpansion, and condition around newpath var also needed to use ! instead of %.

@echo off
setlocal EnableDelayedExpansion

set newpath=
for %%a in ("%path:;=";"%") do (
    rem @echo %%~a
    if [!newpath!]==[] (
        echo %%~a | findstr /I /C:"client_1" >nul
        if not errorlevel 1 (
            set newpath=%%~a
            set newpath=!newpath:client_1=client_1\bin!
        )
    )
)

set PATH=%PATH%;%newpath%
mpapec
  • 50,217
  • 8
  • 67
  • 127