-2

Just noticed that VAR=%VAR:*STRING% does eliminate the previous string but VAR=%VAR:STRING*% doesn't elimiate next string

so how to eliminate the next string ? my current code is :

:CheckEnvironmentVariable Location Variable Value
IF [%1] EQU [System] (
    ECHO Querying system
) ELSE (
    IF [%1] EQU [User] (
        ECHO Querying User Environments
        FOR /F "usebackq tokens=*" %%x IN (`REG QUERY "HKCU\Environment"`) DO (
            SET CURRVARS=%%x&&SET CURRVARS=!CURRVARS:REG_*=!
            ECHO !CURRVARS!
        )
    ) ELSE (
        ECHO ERROR ^^! Invalid Environment Variable Location "%1"
    )
)
EXIT /B

which is doen't work as expected

Andre B
  • 9
  • 4
  • 2
    You need to explain what you want your result to be. It appears from your question code and body text that you're hoping for a result like this: ```EXPAND_SZ %USERPROFILE%\AppData\Local\Temp``` or this: ```TEMP REG_```. Your code can only be properly understood if you show us the content of `%%x` and the expected return value of `!CURRVARS!`. – Compo Jun 18 '22 at 10:21
  • Some hints: __1.__ Replace `IF [%1] EQU [System] (` by `IF /I "%~1" == "System" (` and `IF [%1] EQU [User] (` by `IF /I "%~1" == "User" (`. For the reason read my answer on [Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files](https://stackoverflow.com/a/47386323/3074564). __2.__ Replace ``FOR /F "usebackq tokens=*" %%x IN (`REG QUERY "HKCU\Environment"`) DO (`` by `FOR /F "tokens=1,2*" %%G IN ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Environment" 2^>nul') DO (`. For the reason run in a command prompt window `for /?` and read the entire output usage help carefully. – Mofi Jun 18 '22 at 12:01
  • It is not clear what you really want. You want to perhaps define the environment variables in current command process with the environment variables as defined for the current user or for the system. __3.__ There can be used instead of `SET CURRVARS=%%x&&SET CURRVARS=!CURRVARS:REG_*=!` and `ECHO !CURRVARS!` the two command lines `if /I "%%H" == "REG_SZ" (set "%%G=%%I") else if /I "%%H" == "REG_EXPAND_SZ" call set "%%G=%%I"` to define the environment variable and as second command line `set %%G` to get output all environment variables starting with the string of current environment variable. – Mofi Jun 18 '22 at 12:06
  • Please take also a look on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) [Delayed expansion](https://ss64.com/nt/delayedexpansion.html) should neither be enabled nor used in your batch script to process correct also environment variable values containing one or more exclamation marks. – Mofi Jun 18 '22 at 12:10
  • Well, `VAR=%VAR:*STRING%` is nonsense. You probably mean `%VAR:*STRING=%`, which, when being expanded, means to replace everything up to and including the first case-insensitive occurrence of `STRING` in the string value of variable `VAR` by an empty string, so effectively to remove said portion. Within such an expression, the character `*` does not have a special meaning when it is placed at another position than behind the `:`… – aschipfl Jun 18 '22 at 12:16

2 Answers2

0

I am afraid I don't understand what you want to do. However, I guess that is related to this "possible" solution:

@echo off
setlocal EnableDelayedExpansion

set "VAR=This is a STRING long value"
echo VAR:  %VAR%

rem Eliminate string "previous" to "STRING" (including it)
set "tail=%VAR:*STRING=%"
echo Tail: "%tail%"

rem Eliminate string "next" to "STRING" (not including it)
set "head=!VAR:%tail%=!"
echo Head1: "%head%"

rem Eliminate string "next" to "STRING" (including it)
set "head=!VAR:STRING%tail%=!"
echo Head2: "%head%"

Output:

VAR:  This is a STRING long value
Tail: " long value"
Head1: "This is a STRING"
Head2: "This is a "
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

Based upon the code you have submitted, I'm not even sure why you would want to try to split the string at that particular place. There is a consistent string in every single line that would be returned by your reg.exe command, and that is REG_. The beauty of that paricular string is that it will always be non space separated, non localized, and never contain special characters. If you split at that point, you know that the substring you're looking for, will always be every token following its remainder, e.g. EXPAND_SZ your string(s); SZ your string(s).

So here's some example code which uses that method, but please be aware that it wil not work as intended should you have any variable defined within the System or User Environments with names including the case insensitive string REG_:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

:Ask4Var
ClS
Set "sName="
Set /P "sName=Please enter the name of the variable you wish to verify>"
If Not Defined sName GoTo Ask4Var
(
    Set %sName%
) 2>NUL | %SystemRoot%\System32\findstr.exe /BIL "%sName%=" 1>NUL || (
    Echo There is no variable named %sName% in the current environment
    %SystemRoot%\System32\timeout.exe /T 3 1>NUL
    GoTo Ask4Var
)

Set "Env=System"
Set "RootKey=HKLM"
Set "SubKey=System\CurrentControlSet\Control\Session Manager\Environment"
%SystemRoot%\System32\choice.exe /C SU /N /M "[S]ystem OR [U]ser?"
If ErrorLevel 2 (
    Set "Env=User"
    Set "RootKey=HKCU"
    Set "SubKey=Environment"
)

Set "Reg=%SystemRoot%\System32\reg.exe"
Set "ValueString="
For /F Delims^=^ EOL^= %%G In (
    '%Reg% Query "%RootKey%\%SubKey%" /V /F "%sName%" /E ^|
     %SystemRoot%\System32\find.exe "REG_"'
) Do (
    Set "Result=%%G"
    SetLocal EnableDelayedExpansion
    For /F "Tokens=1,*" %%H In ("!Result:*REG_=!") Do (
        EndLocal
        Set "ValueString=%%I"
    )
)

If Not Defined ValueString (
    Echo There is no variable named %sName% in the %Env% environment
    %SystemRoot%\System32\timeout.exe /T 3 1>NUL
    GoTo Ask4Var
)

Echo The expanded string value of %sName% is %ValueString%.
%SystemRoot%\System32\timeout.exe /T 7 1>NUL

GoTo :EOF
Compo
  • 36,585
  • 5
  • 27
  • 39