Question:
Is it possible to have a function receive a parameter which has both a percent sign and an exclamation mark?
Problem:
I am in a FOR /D
processing directories in this fashion:
FOR /D %%d IN ("%~1\*") DO (
CALL :process "%%~fd"
)
The problem comes when the subdirectory name (%%~fd
) contains both a %
and a !
(which is completely legal in Windows), such as C:\&!x#%
. When I read %1
in the subprocedure :process
, the percent sign disappears. If I EnableDelayedExpansion, then the exclamation mark does.
I read this post and, apparently, this cannot be solved. If delayed expansion is disabled, the %
will be erased. If delayed expansion is enabled, the !
will be.
Example:
ex.bat:
@ECHO OFF
SET arg="%~1"
CALL :clean_echo %arg%
GOTO :EOF
:clean_echo
SET arg="%~1"
SET arg=%arg:&=^&%
SET arg=%arg:|=^|%
SET arg=%arg:<=^<%
SET arg=%arg:>=^>%
ECHO %arg:~1,-1%
GOTO :EOF
If I execute ex.bat "%!"
the output is just !
. The %
is lost when it is passed on to the :clean_echo
subprocedure.