Sandeep Prasad
Sandeep Prasad

Reputation: 35

How can I use special characters for making folder in batch file?

@echo off
setlocal EnableDelayedExpansion

set "folder=C:\Test\Test01\Test02\Test03\Test04\!!Test05\Test06"

md %folder%\Final

echo %folder%
pause

This will create in C:\Test\Test01\Test02\Test03\Test04\Test05\Test06\Final.

Result should be like C:\Test\Test01\Test02\Test03\Test04\!!Test05\Test06\Final.

How can I use special characters (!!Test05) for making folder in batch file?

Upvotes: 1

Views: 1792

Answers (3)

Compo
Compo

Reputation: 38614

Your command works fine if you remove the unused and unwanted second line. ! is not a special character in Windows but it is interpreted in a special way when you have enabled delayed expansion.

@Echo Off
Set "folder=C:\Test\Test01\Test02\Test03\Test04\!!Test05\Test06"
MD "%folder%\Final"
Echo=%folder%
Timeout -1

Delayed Expansion is off by default although this can be changed. If you need to ensure that your ! are seen correctly then the safest option would be to add the line SetLocal DisableDelayedExpansion prior to your specific code section.

Upvotes: 0

aschipfl
aschipfl

Reputation: 34909

When delayed expansion is enabled, exclamation marks are lost when stating them literally or when expanding normal %-variables. Toggle delayed expansion and read affected variables with surrounding ! only to overcome this:

@echo off
setlocal DisableDelayedExpansion

rem // Delayed Expansion is disabled, so literal exclamation marks are maintained:
set "folder=C:\Test\Test01\Test02\Test03\Test04\!!Test05\Test06"

setlocal EnableDelayedExpansion
rem /* Delayed Expansion is enabled, so do not expand variables with `%`,
rem    or you will lose exclamation marks: */
md "!folder!\Final"
endlocal

rem /* Delayed Expansion is disabled, variables can be expanded with `%`,
rem    without losing exclamation marks: */
echo "%folder%"
endlocal

Upvotes: 1

axwr
axwr

Reputation: 2236

for this, since windows treats ! as a special character you are going to need to escape it. the ^ symbol is an escape character so something like:

@echo off
setlocal EnableDelayedExpansion

set "folder=C:\Test\Test01\Test02\Test03\Test04\^!^!Test05\Test06"

md %folder%\Final

echo %folder%
pause

Should work fine. Take a look here: https://ss64.com/nt/syntax-esc.html

Upvotes: 1

Related Questions