Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8877451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:28:19+00:00 2026-06-14T19:28:19+00:00

I am working on a batch file that get’s data as a variable, and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:28:20+00:00Added an answer on June 14, 2026 at 7:28 pm

    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 SET from 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”

    @echo off
    setlocal disableDelayedExpansion
    setlocal enableDelayedExpansion
    
    ::Read the json string from a file
    <test.txt set /p "json="
    
    ::Define LF variable to contain a linefeed
    set LF=^
    
    
    ::The above 2 blank lines are critical - DO NOT REMOVE
    
    ::Strip the enclosing braces
    set "json=!json:~1,-1!"
    
    ::Substitute a linefeed for ","
    for %%A in ("!LF!") do set "json=!json:","=%%~A!"
    
    ::Substitute = for ":"
    set "json=!json:":"==!"
    
    ::Remove remaining "
    set "json=!json:"=!"
    
    ::Loop through the data, creating variables of the form var_name=value
    for /f "delims=" %%A in ("!json!") do (
    
      REM If delayed expansion is enabled then endlocal to get back to disabled state
      if "!!" equ "" endlocal
    
      REM Create the variable
      set "var_%%A"
    )
    
    ::Display the results - list all variables that begin with var_
    set var_
    

    Here is the output

    var_Actors=Timothy Hutton, Robert Urich, Kim Cattrall, Robert Culp
    var_Director=Bob Clark
    var_Genre=Action, Comedy, Drama
    var_imdbID=tt0090217
    var_imdbRating=5.7
    var_imdbVotes=2,360
    var_Plot=Jimmy Lynch is angry because his older brother, who was injured as a result of an off duty fire rescue...
    var_Poster=http://ia.media-imdb.com/images/M/MV5BMTQ2OTk1ODA1MV5BMl5BanBnXkFtZTcwNjYwNjgyMQ@@._V1_SX300.jpg
    var_Rated=PG-13
    var_Released=15 Feb 1985
    var_Response=True
    var_Runtime=1 h 42 min
    var_Title=Turk 182!
    var_Writer=Denis Hamill, James Gregory Kingston
    var_Year=1985
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand a batch file that was sent to me in
I'm trying to create a batch file that will check the existence of a
I am working on a batch file to gather the mac addresses of two
I had a problem with set not working in a batch file; it took
I want my classpath to be set via a batch file. I'm working on
Given a postscript file that has the following header %!PS-Adobe-3.0 I would like to
Trying to get Thin working with Bundle on Windows, I know, major PITA but
Background I have a Spring batch program that reads a file (example file I
I'm trying to knock together a little batch script for downloading files, that takes
I'm working on a project that requires batch processing of a large number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.