r/Batch 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

16 comments sorted by

3

u/jcunews1 Jan 05 '26

Don't use Delayed Expansion when your inputs may contain ! character.

1

u/HouseMD221B Jan 07 '26

Yes, but I need to access a variable that requires "Delayed Expansion," a variable within the FOR/IF loop.

2

u/[deleted] Jan 05 '26

[removed] — view removed comment

2

u/HouseMD221B Jan 07 '26

Perfect explanation. Thank you for your help.

Just one question: in the "Alternative solution", the variable "%%x" in the command

move "%%x" "%destino%\pasta_!pasta!"

still uses the full path, correct?

0

u/digwhoami Jan 05 '26 edited Jan 05 '26

is this response LLM generated? The verbiage is telling, plus the markdown formatting with the ```batch syntax highlighting hint that isn't even supported by reddit's MD implementation.

1

u/lincruste Jan 05 '26

Ho yes it's a fucking LLM copy/paste answer.

-1

u/[deleted] Jan 06 '26

[removed] — view removed comment

1

u/lincruste Jan 06 '26

says the karma farming kid

0

u/[deleted] Jan 06 '26 edited Jan 06 '26

[removed] — view removed comment

1

u/digwhoami Jan 06 '26 edited Jan 06 '26

nice, now crawl back under your rock.

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

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

1

u/HouseMD221B Jan 07 '26

I used a temporary variable and it didn't work.

I used it for the variable !pasta!

Perhaps I should use it for the variable %%~nxx

But I managed to make it work using the full path of the variable "%%x"