George Hernando
George Hernando

Reputation: 2650

Escaping strings in a Windows Batch For Loop

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

Upvotes: 0

Views: 773

Answers (1)

Anthony Miller
Anthony Miller

Reputation: 15930

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"
)

Upvotes: 1

Related Questions