Reputation: 2145
I've got a simple batch file that places the parameter it's called with into a text file:
setlocal ENABLEDELAYEDEXPANSION
set filename=%~n1
set pathname=%~p1
set letter=%~d1
>>path.txt echo %letter%%pathname%%filename%
(it does more, but this is sufficient to show the problem)
The parameter is a full path: C:\te st\file & name.xml
This batch file works as long as there's no & in the path name. But the above path results in filename=file
and the & is interpreted as an argument.
I tried using set "filename=%~n1"
but that results in
>>path.txt echo C:\te st\"file & name.xml"
which is incorrect. I can't get rid of the quotes. I tried:
>>path.txt echo %letter%%pathname%!filename!
but that results in
>>path.txt echo C:\te st\!filename!
How do I get the correct path in my text file?
Upvotes: 1
Views: 126
Reputation: 82418
setlocal DisableDelayedExpansion
set "filename=%~n1"
set "pathname=%~p1"
set "letter=%~d1"
setlocal EnableDelayedExpansion
>>path.txt echo !letter!!pathname!!filename!
After assigning the content into variables, only delayed expansion should be used, because delayed expansion never changes or try to parse the content.
The setlocal DisableDelayedExpansion
at the beginning ensures, that exclamation marks are preserved while assigning the arguments to variables.
Upvotes: 1