2

I would like split a string in two part with = as delimiter.

I saw this post but I do not manage ta adapt.

I try this:

set "str=4567=abcde"
echo %str%
set "var1=%str:^=="^&REM #%
echo var1=%var1%

Why it does not work?

1
  • 3
    because its an equal sign which is special character when it comes to string replacement. Commented Dec 3, 2014 at 12:00

2 Answers 2

4

While not a bulletproof solution (use the for, artoon), without more info, this can do the work

@echo off
    setlocal enableextensions enabledelayedexpansion

    set "str=4567=abcde"

    rem Step 1 - remove the left part
    set "str1=!str:%str%!"

    rem Step 2 - Get the right part
    set "right=!str:*%str1%!"

    rem Step 3 - Get the left part
    set "left=!str:%right%=!"
    set "left=%left:~0,-1%"

    echo [%left%] [%right%]

edited to adapt to comments (OP code in comments adapted to my code, or the reverse)

for /f "delims=" %%i in ('set') do ( 
    setlocal enabledelayedexpansion

    rem Step 1 - remove the left part
    set "str=%%i"
    for %%x in ("!str!") do set "str1=!str:%%~x!"

    rem Step 2 - Get the right part 
    for %%x in ("!str1!") do set "right=!str:*%%~x!" 

    rem Step 3 - Get the left part 
    for %%x in ("!right!") do set "left=!str:%%~x=!" 
    set "left=!left:~0,-1!" 

    echo [!left!] [!right!] 
    endlocal
)

And no, as previously indicated this is not bulletproof and some of the variables show problems (had I said it is not bulletproof?).

What i don't understand is the requirement to not use a for loop and then use a for loop. It is a lot easier this way

for /f "tokens=1,* delims==" %%a in ('set') do (
    echo [%%a] [%%b]
)

Another alternative (not as easy as the for, more stable than the previous one, non bulletproof) is

    for /f %%a in ('set') do (
        call :split left right %%a
        echo [!left!] [!right!]
    )
    goto :eof


:split leftVar rightVar data
    set "%~1=%~3"
    setlocal enabledelayedexpansion
    set "data=%*"
    set "data=!data:*%1 %2 %3=!"
    set "data=%data:~1%"
    endlocal & set "%~2=%data%"
    goto :eof
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, it works but how use it in loop for ? I tried this : for /f "delims=" %%i in ('set') do ( rem Step 1 - remove the left part set "str1=!%%i:%%i!" rem Step 2 - Get the right part set "right=!%%i:*%str1%!" rem Step 3 - Get the left part set "left=!%%i:%right%=!" set "left=%left:~0,-1%" echo [!left!] [!right!] )
@artoon, answer updated. And, as you are using a for loop, i have also included a code with a for loop to do the same work with less problems
@artoon, one more added.
2

As npocmaka commented above, = has special meaning and cannot be replaced with traditional variable string manipulation. If you know the length of either side of the equal sign, you could strip off a number of characters. For example, if "4567" will always be 4 characters, you could set "var1=%str:~0,4%". Or if "abcde" will always be 5 characters, you could set "var1=%str:~0,-6%" (5 chars + 1 for the equal sign).

Otherwise, a for loop is your only other option without using 3rd party utilities.

for /f "delims==" %%I in ("%str%") do set "var1=%%I"

If you've got grep installed, you can do something like:

echo %str% | grep -P -o "^[^=]*"

... but you'd still need to capture its output with another for /f loop.

If you are allergic to for loops, and as an exercise in providing a solution to your question without any regard for efficiency, here's how you get the first half of your string without using a single for loop. Put grep and its dependencies in your %PATH%. Then:

echo %str% | grep -P -o "^[^=]*" >temp.txt
set /P "var1="<temp.txt
del temp.txt
echo %var1%

There, I fixed it!

1 Comment

That's a different question entirely. You should post it as a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.