A script to manage incoming files generates an audit trail that logs transaction sets made up of a header record, several transaction records and a trailer record. A single line is written with delimiters, a string for the header, one each for the transactions and finally the trailer. Its fairly simple and the code snippet below describes it perfectly.
There is, however, a flaw in the ointment, and one that's been around since XP transitioned out: sometimes the id in the transaction has a leading space, and cmd strips this out. dbenham posted its awfulness here and I'm hoping someone might have chanced on a solution to the bug? feature? issue.
The following example shows this, with the first line having leading spaces, and the second replacing the leading and trailing spaces with an underscore by way of contrast, and to see what happens during expansion.
The leading space is truncated, the trailing space retained.
A delimiter is sent after each string. wrapping the set
command in quotes makes no difference.
TEST OUTPUT:
no 1 line:header:leading space:leading trailing spaces :leadingtrailing pipe :
no 2 line:header:_leading char:_leading trailing chars__:_leadingtrailing pipe_:
SAMPLE CODE:
type nul>file.txt
>>file.txt (<nul set /p x=no 1 line) &&rem first line
>>file.txt (<nul set /p x=:) &&rem delimiter
>>file.txt (<nul set /p x=header)
>>file.txt (<nul set /p x=:)
(<nul set /p x= leading space)1>>file.txt &&rem leading space
>>file.txt (<nul set /p x=:)
(<nul set /p "x= leading trailing spaces ")1>>file.txt &&rem leading space, trailing space
>>file.txt (<nul set /p x=:)
echo|set /p=" leadingtrailing pipe ">>file.txt
>>file.txt (<nul set /p x=:)
>>file.txt echo:&&rem force a newline to begin line no 2.
>>file.txt (<nul set /p x=no 2 line) &&rem writes to index 1
>>file.txt (<nul set /p x=:)
>>file.txt (<nul set /p "x=header") &&rem no leading, no trailing
>>file.txt (<nul set /p x=:)
(<nul set /p "x=_leading char")1>>file.txt &&rem leading '_'
>>file.txt (<nul set /p x=:)
(<nul set /p "x=_leading trailing chars__")1>>file.txt &&rem leading, trailing '_'
>>file.txt (<nul set /p x=:)
echo|set /p="_leadingtrailing pipe_">>file.txt
>>file.txt (<nul set /p x=:)
rem no newline to avoid a trailing carriage return
CODE EXPANSION: (where the leading white space is stripped out)
(set /p x= leading space 0<nul ) 1>>file.txt && rem leading space
(set /p "x= leading trailing spaces " 0<nul ) 1>>file.txt && rem leading and trailing spaces
My workaround is to replace any leading spaces in the header id with underscores, and then restore the spaces after the run completes. This works fine because none of the incoming records have an underscore in their id. It would be nice, though, to be able to remove the work-around.