Search code examples
windowsbatch-filecmd

How to fetch the substring from the input string in command prompt


I have the following string

set var="/host=the_host/core-service=vault:add(vault-options=[("KEYSTORE_URL" => "C:\wildfly-15.0.1.Final\bin\vault\vault.keystore"),("KEYSTORE_PASSWORD" => "MASK-18BzezESSkb72WrhEf6Rsu"),("KEYSTORE_ALIAS" => "vault"),("SALT" => "1234abcd"),("ITERATION_COUNT" => "120"),("ENC_FILE_DIR" => "C:\wildfly-15.0.1.Final\bin\vault/")])"

I want to extract only the MASK-18BzezESSkb72WrhEf6Rsu value alone using windows batch. I have tried using the for but couldn't find anything to fetch the password alone.


Solution

  • @ECHO OFF
    SETLOCAL
    
    SET "string=/core-service=vault:add(vault-options=[("KEYSTORE_URL" => "C:\wildfly-8.2.1.Final\bin\vault\vault.keystore"),("KEYSTORE_PASSWORD" => "MASK-18BzezESSkb72WrhEf6Rsu"),("KEYSTORE_ALIAS" => "vault"),("SALT" => "1234abcd"),("ITERATION_COUNT" => "120"),("ENC_FILE_DIR" => "C:\wildfly-8.2.1.Final\bin\vault/")])"
    
    SET "string=%string:"=%"
    SET "string=%string:>=%"
    SET "string=%string:<=%"
    SET "string=%string:)=%"
    SET "string=%string:(=%"
    
    SET "password="
    
    FOR %%e IN (%string%) DO IF DEFINED password (
      SET "password=%%e"
      GOTO found
     ) ELSE  IF /i "%%e"=="KEYSTORE_PASSWORD" SET "password=next"
    
    ECHO password NOT found
    GOTO :eof
    
    :found
    ECHO password is "%password%"
    
    GOTO :EOF
    

    Remove the awkward characters by uing string-substitution then run through the remaining list of words, if the string in the word before is found, set password as a flag, and grab the next one.