0

I'm having problems with the following code:

FOR /f "tokens=*" %%A in (%MYFILE_PATH%) do (
    [other stuff]
    echo %%A>> "%MYFILE_PATH%.scratch"
)

The file being read has XML in it and when < and > signs are read, the script throws errors. How can I escape the contents of %%A to safely echo to the output file? I don't want to put double quotes around it since it will then echo the quotes too.

Thanks

1
  • This code doesn't fail with <> characters, as echo %%A is safe against special characters (only the exlamation mark can cause problems). I assume your real code call functions or copy %%A into other variables Commented Sep 8, 2011 at 6:33

1 Answer 1

1
FOR /f "tokens=*" %%A in (%MYFILE_PATH%) do (
    [other stuff]
    (echo %%A)>>"%MYFILE_PATH%.scratch"
)

When appending the contents of %%A into another file, all that you are essentially doing is copying the file.

EDIT

 FOR /f "tokens=* delims=" %%A in (%MYFILE_PATH%) do (
    [other stuff]
    (echo %%A)>>"%MYFILE_PATH%.scratch"
)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! With this there is no error any more. But the whitespace in the line being copied is getting stripped. Any way to prevent that?
It also seems to strip some other characters. Like
It also seems to strip some other characters. Like the line: <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'xxx.org/dtd/my-beans.dtd'> becomes <//www.xxx.org/dtd/my-beans.dtd'>
edited my answer. The reason it was not working correctly is because in FOR /F loops, white space is the delimeter so it is automatically removed unless you specify nothing as the delims... so is a semicolon.
Never got it fully working. Came up with a different approach to not do it within a batch file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.