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%
6
  • 1
    Does this answer your question? 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%. Commented Jun 30, 2023 at 13:26
  • @SomethingDark no, it's still failing Commented Jun 30, 2023 at 13:46
  • Could you elaborate on how it's failing? Is the variable not getting created at all? Commented Jun 30, 2023 at 13:55
  • @SomethingDark substitution wasn't working as expected. Commented Jul 1, 2023 at 7: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. Commented Jul 1, 2023 at 16:10

1 Answer 1

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%
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.