2

I am making a batch script that creates a .json script if it is not found in a specific location. If it needs to be made, I also have my batch script insert all of the default code for the JSON. I can make a 1:1 copy of the original I'm making the new one from, but I want to know if there's a faster way to do so without having to copy the code line for line with the current syntax that I'm using. I am inserting the code via the ECHO command in this manner:

ECHO %CODELINE%>> "%JSONSCRIPT%

I want to know if there is something I can do so that each line doesn't have to start with ECHO and end with >> "%JSONSCRIPT%. Is this possible? Here is a snippet from my script for one of the JSON files to be created:

:MAKEJSON
BREAK>"Scripts\JSON1.json"
ECHO {>> "Scripts\JSON1.json"
ECHO   "list": [>> "Scripts\JSON1.json"
ECHO     {>> "Scripts\JSON1.json"
ECHO       "var": {>> "Scripts\JSON1.json"
ECHO         "showName": "Example 01",>> "Scripts\JSON1.json"
ECHO         "targetName": "TBD">> "Scripts\JSON1.json"
ECHO       },>> "Scripts\JSON1.json"
ECHO       "type": {>> "Scripts\JSON1.json"
ECHO         "showName": "Example 02",>> "Scripts\JSON1.json"
ECHO         "typeName": "[Test]",>> "Scripts\JSON1.json"
ECHO       }>> "Scripts\JSON1.json"
ECHO     },>> "Scripts\JSON1.json"
ECHO     {>> "Scripts\JSON1.json"
ECHO       "var": {>> "Scripts\JSON1.json"
ECHO         "showName": "Test 2",>> "Scripts\JSON1.json"
ECHO         "varName": "[Test2]">> "Scripts\JSON1.json"
ECHO       },>> "Scripts\JSON1.json"
ECHO       "type": {>> "Scripts\JSON1.json"
ECHO         "showName": "Sampletext",>> "Scripts\JSON1.json"
ECHO         "typeName": "TBD",>> "Scripts\JSON1.json"
ECHO       }>> "Scripts\JSON1.json"
ECHO     }>> "Scripts\JSON1.json"
ECHO   ]>> "Scripts\JSON1.json"
ECHO }>> "Scripts\JSON1.json"

3 Answers 3

2

Why don't you just create a "template" text file that has all of the actual text you want, and then reference that in your script instead?

For example, your template text file would contain:

{
  "list": [
    {
      "var": {
        "showName": "Example 01",
        "targetName": "TBD"
      },
      "type": {
        "showName": "Example 02",
        "typeName": "[Test]",
      }
    },
    {
      "var": {
        "showName": "Test 2",
        "varName": "[Test2]"
      },
      "type": {
        "showName": "Sampletext",
        "typeName": "TBD",
      }
    }
  ]
}

And your script would simply have:

type "Scripts\template.txt" >> "Scripts\JSON1.json"

0
2

A template is usually used to create varying content. So a batch has the additional use to insert variables into the template.

This batch simplifies output by using a (code block) with redirection:

@Echo off

For /L %%n in (1,2,10) Do Call :MAKEJSON %%n

Goto :Eof
:MAKEJSON
Set /A n=100 + %1,m=101 +%1,T = %1 + 1
Set  "JSONSCRIPT=Script\JSON%1.json"
( Echo={
  Echo=  "list": [
  Echo=    {
  Echo=      "var": {
  Echo=        "showName": "Example %n:~-2%",
  Echo=        "targetName": "TBD"
  Echo=      },
  Echo=      "type": {
  Echo=        "showName": "Example %m:~-2%",
  Echo=        "typeName": "[Test]",
  Echo=      }
  Echo=    },
  Echo=    {
  Echo=      "var": {
  Echo=        "showName": "Test %T%",
  Echo=        "varName": "[Test2]"
  Echo=      },
  Echo=      "type": {
  Echo=        "showName": "Sampletext",
  Echo=        "typeName": "TBD",
  Echo=      }
  Echo=    }
  Echo=  ]
  Echo=}
) >"%JSONSCRIPT%"

And creates several files in one run:

│   MakeJson.cmd
│
└───script
        JSON1.json
        JSON3.json
        JSON5.json
        JSON7.json
        JSON9.json

With content like this:

> type script\JSON5.json
{
  "list": [
    {
      "var": {
        "showName": "Example 05",
        "targetName": "TBD"
      },
      "type": {
        "showName": "Example 06",
        "typeName": "[Test]",
      }
    },
    {
      "var": {
        "showName": "Test 6",
        "varName": "[Test2]"
      },
      "type": {
        "showName": "Sampletext",
        "typeName": "TBD",
      }
    }
  ]
}
0

After some digging, I came across this question on Stack Overflow that has an answer (by grawity) that I modified to work in the manner I was looking for. ECHO must still be declared at the beginning of each line you want to insert into your text-based document/script for this to work. Begin the code block with a single left parenthesis on it's own line, and end with a right parenthesis on a new line followed by > "%JSONFILE%". Here is the code block I used that achieves this:

:MAKEJSON
BREAK>"Scripts\JSON1.json"
(
ECHO {
ECHO   "list": [
ECHO     {
ECHO       "var": {
ECHO         "showName": "Example 01",
ECHO         "targetName": "TBD"
ECHO       },
ECHO       "type": {
ECHO         "showName": "Example 02",
ECHO         "typeName": "[Test]",
ECHO       }
ECHO     },
ECHO     {
ECHO       "var": {
ECHO         "showName": "Test 2",
ECHO         "varName": "[Test2]"
ECHO       },
ECHO       "type": {
ECHO         "showName": "Sampletext",
ECHO         "typeName": "TBD",
ECHO       }
ECHO     }
ECHO   ]
ECHO }
) > "%JSONFILE%"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .