- Jun
- 1,092
- 48
I run into problems often because of ampersand characters in material that I want to process in a script. If nothing is being piped, the command
Today one of my scripts that displays a file directory with descriptions failed because one of the descriptions had an ampersand it in. It took me a frustratingly long time to figure out that that was the cause of the failure. The solution I came up with was to use
Perhaps another solution would have been to change the command separator to a character that was highly unlikely to appear in the command output and then to change it back to '&' after the piping was finished.
setdos /x-5 generally allows one to manage the problem. However, if the output of a command is going to be piped into another command, that does not work, because the /x-5 option turns off piping as well as multiple commands.Today one of my scripts that displays a file directory with descriptions failed because one of the descriptions had an ampersand it in. It took me a frustratingly long time to figure out that that was the cause of the failure. The solution I came up with was to use
setdos /x-5, but instead of trying to pipe the output, I redirected it to a tmp#: pseudo-device. Then I restored normal processing using setdos /x0 and typed the contents of the tmp#: device into a pipe. So the code looked like this:
Code:
REM clear the tmp device
tmp /c tmp9:
REM turn off the special meaning of & (and, as a bonus, |)
setdos /x-5
REM run the commands in a loop with the output sent to the tmp device
{command loop} >> tmp9:
REM restore normal processing
setdos /x0
REM use TYPE (which doesn't care about special characters) to send the text to the pipe
type tmp9: |! {pipe process}
Perhaps another solution would have been to change the command separator to a character that was highly unlikely to appear in the command output and then to change it back to '&' after the piping was finished.