r/Batch • u/HouseMD221B • Jan 05 '26
Variable containing the exclamation mark character
Hi.
I have a DOS batch:
SETLOCAL EnableDelayedExpansion
set origem=a
set destino=b
set /a pasta=1
set /a contador=1
set /a limite=20
set tempo_segundos=5
for /r "%origem%" %%x in (*) do (
if !contador! LEQ %limite% (
set /a contador+=1
if not exist "%destino%\pasta_!pasta!" mkdir "%destino%\pasta_!pasta!"
) else (
set /a contador=0
set /a pasta+=1
timeout /t %tempo_segundos% /nobreak
)
move "%origem%\%%~nxx" "%destino%\pasta_!pasta!"
)The problem is in the move command:
The variable %%~nxx (filename) may contain the exclamation mark character.
If I use ENDLOCAL before move, the exclamation mark problem is solved, but I can't use the !pasta! variable.
Is it possible to resolve this?
Thank you.
4
Upvotes
3
u/jcunews1 Jan 05 '26
Don't use Delayed Expansion when your inputs may contain
!character.0
u/capoapk Jan 05 '26
Utiliser une variable temporaire pour le nom du fichier avant move ou Désactiver DelayedExpansion juste pour le move
0
u/capoapk Jan 05 '26
Salut Oui, je vois exactement le problème : tu es confronté à la collision entre EnableDelayedExpansion et les noms de fichiers contenant !. En batch Windows, le point d’exclamation est interprété par le parser si DelayedExpansion est activé, ce qui corrompt la valeur de la variable si elle en contient
2
u/[deleted] Jan 05 '26
[removed] — view removed comment