I am working on a batch file that get’s data as a variable, and in that variable it has an exclamation mark.
What i am trying to do is add the necessary escape characters to the variable.
"Title":"Turk 182!"
Above is an example of the data I am working with.
setlocal EnableDelayedExpansion
rem replace ! with ^^!
set var=%var:!=^^!!%
But I am not sure that is the correct syntax, since how can i escape an esclamation mark if it’s also used as part of the search and replace?
I am trying to replace all esclamation mark with an escaped version so it can be displayed and worked with.
Btw, is there any existing functions in batch that will remove and allow escaping of all special characters?
Here is more code that may help explain what I am doing.
{"Title":"Turk 182!","Year":"1985","Rated":"PG-13","Released":"15 Feb 1985","Runtime":"1 h 42 min","Genre":"Action, Comedy, Drama","Director":"Bob Clark","Writer":"Denis Hamill, James Gregory Kingston","Actors":"Timothy Hutton, Robert Urich, Kim Cattrall, Robert Culp","Plot":"Jimmy Lynch is angry because his older brother, who was injured as a result of an off duty fire rescue...","Poster":"http://ia.media-imdb.com/images/M/MV5BMTQ2OTk1ODA1MV5BMl5BanBnXkFtZTcwNjYwNjgyMQ@@._V1_SX300.jpg","imdbRating":"5.7","imdbVotes":"2,360","imdbID":"tt0090217","Response":"True"}
rem removes starting and ending brackets
set json=%json:~1,-1%
setlocal EnableDelayedExpansion
rem replace "," with linebreak
set json=!json:","="#"!
setlocal EnableDelayedExpansion
rem replace ! with ^^!
set json=%json:!=^^!!%
setlocal DisableDelayedExpansion
echo %json%
echo.
exit /b
Then i place the json variable in the loop and it error’s out instead of the true value I just see the actual replace command i used above.
I’m not convinced you really need to escape
!. It is possible to do, but as jeb tried to explain, it can get complicated.Based on the code you provided, it looks to me like you do not understand what delayed expansion does. I suggest you type
HELP SETfrom a command prompt and read the documentation. The description of delayed expansion begins about half way down with the line that reads “Finally, support for delayed environment variable expansion…”There are other major benefits to delayed expansion. One of the biggest advantages is you never have to worry about escaping any special characters when you use delayed expansion. Escaping characters while using normal expansion is a pain, and can be extremely confusing until you gain experience. (It really is logical and predictable, but until you understand it, it looks like gibberish)
The biggest problem with delayed expansion is it does not play nice with FOR loops when the data contains
!. That is because the delayed expansion occurs after the FOR variable is expanded, so values containing!will be corrupted. In my json parser below, I toggle the delayed expansion off within the loop to avoid that problem.I don’t know much about json, so my solution below may be naive (incomplete). But here is a simple json parser that works with the data you provided. I put your json string in a file named “test.txt”
Here is the output