|Because delayed expansion causes exclamation marks to flag variables they, as you mention, disappear.|
Unfortunately their are only two ways (that I am aware of) to keep the exclamation marks to stay untouched, either escape them with 2 ^^carats or not have delayed expansion enabled.
With the for loop over a file you have no chance to apply carats but there is a trick that can be used, if you set a variable with text that contains an exclamation mark before you enable delayed expansion the exclamation mark inside the variable will not expand/disappear.
rem delayed expansion must not be enabled at this point
for /f %%a in (test.txt) do (
Just remember the endlocal at the end, with out it you will probably get setlocal recursions.
If you have a variable that you must keep afterwards(because endlocal should kill everything set since setlocal), this may help.
for /f "delims=" %%z in ("!var!") do endlocal&set var=%%z