Home > Back-end >  Eliminate strings with wildcard in batch file variable
Eliminate strings with wildcard in batch file variable

Time:06-21

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

CodePudding user response:

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 "

CodePudding user response:

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
  • Related