0

%vData% equals the HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path variable which includes ;%main%\Programs\Go\Bin.
%main% is a variable in the same Environment which holds the value C:\Main.

I want to check if this value exists before adding, so I have the code:

echo %vData% | FindStr /L /I /C:"%%main%%\\Programs\\Go\\Bin"

I have tried %%main%% and %main% and %^main% and %^^main% but it consistently tells me the string does not exist.

How do I get it to show me it does exist?

aschipfl
  • 33,626
  • 12
  • 54
  • 99

2 Answers2

1

Given that the variable really contains the literal string ;%main%\Programs\Go\Bin (you can prove it by simply doing echo/%vData%), I believe that you are looking for this:

echo/%%vData%%| findstr /I /C:"%%main%%\\Programs\\Go\\Bin"

or, even better, since special characters in vData become protected:

cmd /V /C echo(!vData!| findstr /I /C:"%%main%%\\Programs\\Go\\Bin"

The problem with your code:

echo %vData% | FindStr /L /I /C:"%%main%%\\Programs\\Go\\Bin"

is the fact that either side of the pipe | is executed in a new cmd instance the left side of the pipe | is executed in a new cmd instance since it is an internal command [updated due to the finding handled in this question], so variable vData is expanded twice:

  1. when the whole command line is executed, the left side becomes:

    echo ...;%main%\Programs\Go\Bin
    
  2. when the left side of the pipe is executed, it becomes:

    echo ...;C:\Main\Programs\Go\Bin
    
aschipfl
  • 33,626
  • 12
  • 54
  • 99
0

This routine should assist.

@ECHO OFF
SETLOCAL
SET "main=VALUE OF MAIN"
SET "vdata=something;%%main%%\Programs\Go\Binsomethingelse"
ECHO -----
ECHO %vdata%
SET vdata
ECHO -----

SET vData| FindStr /L /I /C:"%%main%%\\Programs\\Go\\Bin"
ECHO %ERRORLEVEL%
ECHO -----

SET vData|FINDSTR /b /L /i "vdata=" | FindStr /L /I /C:"%%main%%\\Programs\\Go\\Bin"
ECHO %ERRORLEVEL%
ECHO -----

echo %vData% | FindStr /L /I /C:"%%main%%\\Programs\\Go\\Bin"
ECHO %errorlevel%

GOTO :EOF

Note the filter /b /L /i "vdata=" which protects the routine from the presence of other variables starting vdata like vdata2 or whatever. If you have but 1, this extra filter is not required.

Magoo
  • 77,302
  • 8
  • 62
  • 84