|I didn't intend it to be, but it's a long one....|
for /f "skip=2 tokens=1* delims=]" %%a in (' find /v /n "" "yourfile.txt" ') do (
Create a line iterating loop (for /f) that ignores the first two lines of input(skip=2), the reason for the skip is "find" adds a header line we don't want. The lines of text are then split on the character "]" (delims=]) and broken into two chunks, the first chunk and the rest of the line(tokens=1*), The first chunk will populate %%a and the rest of the line %%b.
The actual input for the loop is a command(marked either side with single quotes), all 'find /v /n "" "yourfile.txt" ' does is output all lines from the file prefixed with line numbers in the format of "line of text". This is important because for /f always ignores blank lines, this way blank line will show up as "[x]" so the for loop won't ignore it.
if "%%b"=="" (
Remember %%b is our "rest of line variable"? If %%b is blank it signals that a new record starts from the next line.
>> "newfile.txt" echo.
This is one of the commands executed if %%b is empty, all this does is append a newline to the output text file.
This is the other command executed if %%b is empty, all it does is undefine the variable "comma" that is used as a flag to determine if I need to prefix a comma to the output or not.
) else (
This marks the end of the commands executed if %%b is empty and start the section executed if %%b does contain something.
if defined comma (
This nested if statement checks if the variable "comma" is defined, this if statement must close before the else of the initial if statement. Take note that I used the defined keyword rather than expanding the variable, to expand the variable properly would require "SetLocal EnableDelayedExpansion" to be used, "defined" side steps this issue because all I needed was true/false.
>> "newfile.txt" set /p =,%%b<nul
If comma is defined I want to prefix the text(%%b) with a comma and add it to the file. The trick here is using "set /p", although it's intended purpose is user input it is useful to output text without a newline, meaning it can append to the same line in the new file multiple times. The <nul is to make set /p exit immediately rather than waiting for the user.
) else (
This is the else on the "if defined comma" command, the condition for the following codeblock is that "comma" isn't defined(first record).
>> "newfile.txt" set /p =%%b<nul
The condition is that "comma" isn't defined, so %%b is output without a comma.
Again inside of the "defined" else clause, because I want to prefix a comma for the rest of the record I set comma to 1, it could be anything it just needs to be something.
End of the "if defined" command.
End of the 'if "%%b"=="" ' command.
End of the for loop.
>> "newfile.txt" echo.
Add a new line to the new file to make it ready to be appended to in future.
Set /p - The first record can not begin with "=" because set /p doesn't allow the first character of "prompt string" to be "=". I don't know why, it just wont. The records prefixed with a comma are unaffected.
delims=] - For loops split on delimiters until the first non delimiter character, so if any lines start with "]" They will be stripped until the first character that isn't "]". They can be anywhere else on the line without any issues because * means the rest of the line including any delimiter characters.