1

In a batchscript I am trying to capture a string containing multiple special characters

< , > , "Text" , = , / , \ , etc.

in a variable but I was unable to get it working using different escape characters

\ , "" , ^ , etc.

in different configurations, either single enclosing, escaping each special character separately, escaping string parts without success.

EDIT: As per Squashmans comment I tried using the enableddelayedexpansion but that did not resolve the issue

Example:

setlocal enabledelayedexpansion

set "var=<ExampleString Id="63fc47e6-fafb-48f92-bd92-38ed9aa4a765" Name="The Name of this item." AnotherItem="Name of this item." AnotherID="AAA-123" Action="GO"><ItemOne><Item Id="*"/></ItemOne><ItemTwo><Location1 Path="Mydrive\Folder1\*"/><Locaton2 Path="C:\Program Files (x86)\*"/></ItemTwo></ExampleString>"

ECHO %var%

It still returns the error:

< was unexpected at this time.

Woudi
  • 31
  • 6
  • 3
    Best practice when setting a value to a variable is to use quotes. `set "var=^&*@!"` You will probably need to use the variable with delayed expansion after that. – Squashman Jan 15 '21 at 18:06
  • Thanks Squashman, I tried but it did not work, see my edit. – Woudi Jan 15 '21 at 18:54
  • 2
    To expand on the above, your question is about capturing the required content within a variable, and the advice above should achieve that as needed, i.e. `set "var=your required content here"`. To verify that your content is correct, to simply verify that your content is correct, use `set var` instead of `ECHO %var%`. However, if you intend to expand the content of the variable named `var`, using the `echo` command, you will probably need to use delayed expansion when doing so. – Compo Jan 15 '21 at 19:01
  • 4
    it's not enough to *enable* [delayed expansion](https://stackoverflow.com/a/30284028/2152082), you also have to actually *use* it. – Stephan Jan 15 '21 at 19:05
  • The reason is that you need to use `!var!` when delayed expanding your variable. You have choices now, if you want me to close your question as a delayed expansion duplicate, I can; If you want me to close it because your issue is that you are not using the defined syntax for expanding a delayed variable, I can, if you want me to close it for not using the recommended syntax for defining a variable with `set`, I can. Either way, your question should be deleted as it is not of future use to readers who have researched their issue before posting. I advise that you delete the question yourself. – Compo Jan 15 '21 at 19:06
  • Thanks all, the problem was indeed not actually delaying the expansion using `!var!`. It is evident that batch scripting is not my primary use so I appreciate the positive feedback Compo ;) – Woudi Jan 15 '21 at 20:00

1 Answers1

0

As per Squashmans comment using the enableddelayedexpansion, correct use of the variable syntax and next comments pointing out to actually delay the expansion using !var! instead of %var% it works perfectly.

Solution Example:

setlocal enabledelayedexpansion

set "var=<ExampleString Id="63fc47e6-fafb-48f92-bd92-38ed9aa4a765" Name="The Name of this item." AnotherItem="Name of this item." AnotherID="AAA-123" Action="GO"><ItemOne><Item Id="*"/></ItemOne><ItemTwo><Location1 Path="Mydrive\Folder1\*"/><Locaton2 Path="C:\Program Files (x86)\*"/></ItemTwo></ExampleString>"

ECHO !var!

Use setlocal enabledelayedexpansion, the correct variable syntax set "var=content" and actual delaying of the expansion with !var! instead of %var%

Compo
  • 36,585
  • 5
  • 27
  • 39
Woudi
  • 31
  • 6