0

I have a batch file that receives a string of "this:EE notthis:that may:probably", which is being split, but I need to manipulate the split string and can't. Any help in pointing me in the right direction would be very appreciated.

The script is:

:: Set the overrides string from the arguments, stripping quotes
SET overrridesString=%1
SET overrridesString=%overrridesString:"=%

FOR %%i in (%overrridesString%) do (
    SET tmpLine=%%i
    echo [DEBUG] Line is '%%i' and tmpLine is '%tmpLine%'
)

I am getting the following output though, which is really confusing me:

[DEBUG] Line is 'this~EE' and tmpLine is 'may:probably'
[DEBUG] Line is 'notthis:that' and tmpLine is 'may:probably'
[DEBUG] Line is 'may:probably' and tmpLine is 'may:probably'

Thank you in advance

Swatcat
  • 73
  • 6
  • 21
  • 57
  • 1
    You'll need [delayed expansion](http://stackoverflow.com/a/10558905/5047996) for `tmpLine`; if you expand it like `!tmpLine!` then, the shown value will equal `%%i`... – aschipfl Oct 09 '15 at 09:57
  • aschimpfl is [right](http://stackoverflow.com/a/30284028/2152082). And there is a "built-in-solution" to remove surrounding quotes: `SET overrridesString=%~1` (the `~` removes surrounding quotes, if present) Or you remove them here : `FOR %%i in (%~1) do` (no need for the variable `%overridesString%`) – Stephan Oct 09 '15 at 10:06
  • Do you intend to remove *surrounding* quotes by the line `SET overrridesString=%overrridesString:"=%`? if yes, you could simply change your first `SET` line to `SET overrridesString=%~1` (note the `~`; type `CALL /?` for details)... – aschipfl Oct 09 '15 at 10:06
  • The change to using !tmpLine! gave me the value I was looking for, if I want to then manipulate tmpLine using SET tmpLine=%tmpLine::=$%, what would I use to expand it please. Thank you @aschipfl and Stephan – Swatcat Oct 09 '15 at 10:21
  • it's exactly the same, just replace `%` by `!`: `!tmpLine::=$!`... – aschipfl Oct 09 '15 at 10:25
  • Thank you @aschipfl I didn't realise the trailing % was part of the expansion... – Swatcat Oct 09 '15 at 10:37

1 Answers1

0

You will need delayed variable expansion and expand tmpLine like !tmpLine!, so its value will equal %%i.

The following code should do what you expect:

SETLOCAL EnableDelayedExpansion

REM Set the overrides string from the arguments, stripping quotes
REM The '~' modifier removes surrounding '""' (see 'CALL /?')
SET overrridesString=%~1

FOR %%i in (%overrridesString%) do (
    SET tmpLine=%%i
    ECHO [DEBUG] Line is '%%i' and tmpLine is '!tmpLine!'
    REM The following line illustrates how to replace ':' by '$'
    ECHO [DEBUG] Manipulated tmpLine is '!tmpLine::=$!'
)
ENDLOCAL
Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99