1

Trying to isolate just a portion of a string contained in a .BAT variable.

The String Variable !Original! contains the following:

v2.30|Action=Allow|Active=FALSE|Dir=In|Profile=Domain|Profile=Private|Name=@{Microsoft.Windows.Cortana_1.13.0.18362_neutral_neutral_cw5n1h2txyewy?ms-resource://Microsoft.Windows.Cortana/resources/PackageDisplayName}|Desc=@{Microsoft.Windows.Cortana_1.13.0.18362_neutral_neutral_cw5n1h2txyewy?ms-resource://Microsoft.Windows.Cortana/resources/ProductDescription}|LUOwn=S-1-5-21-1502285707-838241421-2811185785-1001|AppPkgId=S-1-15-2-1861897761-1695161497-2927542615-642690995-327840285-2659745135-2630312742|EmbedCtxt=@{Microsoft.Windows.Cortana_1.13.0.18362_neutral_neutral_cw5n1h2txyewy?ms-resource://Microsoft.Windows.Cortana/resources/PackageDisplayName}|Platform=2:6:2|Platform2=GTEQ|

I'm trying to parse it for the sequence of characters just after the word "Name=".

The following For Loop returns: The system cannot find the file v2.30|Action.

FOR /F "tokens=* delims=" %%h in (!Original! ^| Findstr "Name=") do (ECHO %%h)

What am I doing wrong? Why does it only read the first little part of the contents of !Original! ?

Appreciated...

Arianax
  • 9
  • 4

1 Answers1

0

Try this (save it as a .bat):

@if (@X)==(@Y) @end /* JScript comment
    @echo off

    set "string=v2.30|Action=Allow|Active=FALSE|Dir=In|Profile=Domain|Profile=Private|Name=@{Microsoft.Windows.Cortana_1.13.0.18362_neutral_neutral_cw5n1h2txyewy?ms-resource://Microsoft.Windows.Cortana/resources/PackageDisplayName}|Desc=@{Microsoft.Windows.Cortana_1.13.0.18362_neutral_neutral_cw5n1h2txyewy?ms-resource://Microsoft.Windows.Cortana/resources/ProductDescription}|LUOwn=S-1-5-21-1502285707-838241421-2811185785-1001|AppPkgId=S-1-15-2-1861897761-1695161497-2927542615-642690995-327840285-2659745135-2630312742|EmbedCtxt=@{Microsoft.Windows.Cortana_1.13.0.18362_neutral_neutral_cw5n1h2txyewy?ms-resource://Microsoft.Windows.Cortana/resources/PackageDisplayName}|Platform=2:6:2|Platform2=GTEQ|"
    set "splitBy=|"

    for /f "tokens=* delims=" %%a in ('cscript //E:JScript //nologo "%~f0"  "%string%" "%splitBy%"') do (
        set "%%~a" 2>nul
    )
    echo %Name%
    exit /b %errorlevel%

@if (@X)==(@Y) @end JScript comment */

var string=WScript.Arguments.Item(0);
var splitBy=WScript.Arguments.Item(1);

var i;
for (i = 0; i < string.split(splitBy).length; i++) {
  WScript.Echo(string.split(splitBy)[i]);
}

batch syntax only solution:

@echo off
setlocal EnableDelayedExpansion

@echo off
REM Creating a Newline variable (the two blank lines are required!)
set NLM=^


set NL=^^^%NLM%%NLM%^%NLM%%NLM%


set "string=v2.30|Action=Allow|Active=FALSE|Dir=In|Profile=Domain|Profile=Private|Name=@{Microsoft.Windows.Cortana_1.13.0.18362_neutral_neutral_cw5n1h2txyewy?ms-resource://Microsoft.Windows.Cortana/resources/PackageDisplayName}|Desc=@{Microsoft.Windows.Cortana_1.13.0.18362_neutral_neutral_cw5n1h2txyewy?ms-resource://Microsoft.Windows.Cortana/resources/ProductDescription}|LUOwn=S-1-5-21-1502285707-838241421-2811185785-1001|AppPkgId=S-1-15-2-1861897761-1695161497-2927542615-642690995-327840285-2659745135-2630312742|EmbedCtxt=@{Microsoft.Windows.Cortana_1.13.0.18362_neutral_neutral_cw5n1h2txyewy?ms-resource://Microsoft.Windows.Cortana/resources/PackageDisplayName}|Platform=2:6:2|Platform2=GTEQ|"

setlocal enableDelayedExpansion
echo !string:^|=%NL%! >#
for /f "tokens=* delims=" %%a in (#) do (
    set "%%~a" >nul 2>&1
)

echo %NAME%
del /q #
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • That might work. Trying to keep this to .bat code only, however. – Arianax Mar 27 '20 at 16:20
  • @Arianax - check my update. The first approach works without creating temp files. With a batch only solution you'll need a one. – npocmaka Mar 27 '20 at 17:07
  • 1
    without a temporary file: change to `set NL=^%NLM%%NLM%^%NLM%%NLM%` and `for /f "tokens=* delims=" %%a in ("%string:|=!NL!%") do set "%%~a" 2>nul` (at least with that string; may be different when the string contains `%&<>"!` - didn't test) – Stephan Mar 27 '20 at 18:43
  • @Stephan - I've tried this ,but it is not working when the string has `?`. The `NAME` has it. Plain `FOR` is intended to iterate files and `?` always taken for a wildcard - no way to turn this feature off. – npocmaka Mar 27 '20 at 18:46
  • Seems to work for me (despite of what you correctly say about the `?` probably because of the quotes, which forces `for /f` to "String mode"). I'll doublecheck tomorrow - too late for brain acrobatic now. – Stephan Mar 27 '20 at 19:23
  • I'm confused as to why you're using the line set NL=^^^%NLM%%NLM%^%NLM%%NLM% . Can't you just use ^ followed by two line-breaks in the statement !string:^|=%NL%! ? – Arianax Mar 27 '20 at 20:58