如何使用特殊字符在批处理文件中创建文件夹?
问题描述:
@echo off
setlocal EnableDelayedExpansion
set "folder=C:\Test\Test01\Test02\Test03\Test04\!!Test05\Test06"
md %folder%\Final
echo %folder%
pause
这将在C:\Test\Test01\Test02\Test03\Test04\Test05\Test06\Final
中创建。如何使用特殊字符在批处理文件中创建文件夹?
结果应该像C:\Test\Test01\Test02\Test03\Test04\!!Test05\Test06\Final
。
如何在批处理文件中使用特殊字符(!!Test05
)制作文件夹?
答
启用delayed expansion,说明他们在字面上或扩大正常%
- 变量时,感叹号都将丢失。切换延迟扩展及与周围!
只有克服这种阅读受影响的变量:如果删除未使用的和不必要的二线
@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
答
为此,因为Windows对待!作为一个特殊的角色,你将需要逃避它。 ^符号是一个转义字符,如下所示:
@echo off
setlocal EnableDelayedExpansion
set "folder=C:\Test\Test01\Test02\Test03\Test04\^!^!Test05\Test06"
md %folder%\Final
echo %folder%
pause
应该正常工作。看看这里:https://ss64.com/nt/syntax-esc.html
答
你的命令工作正常。 !在Windows中不是特殊字符,但当您启用延迟扩展时,会以特殊方式解释它。
@Echo Off
Set "folder=C:\Test\Test01\Test02\Test03\Test04\!!Test05\Test06"
MD "%folder%\Final"
Echo=%folder%
Timeout -1
默认情况下延迟扩展是关闭的,虽然这可以改变。如果您需要确保您的!
被正确看到,那么最安全的选择是在您的特定代码部分之前添加行SetLocal DisableDelayedExpansion
。
+0
非常感谢。 –
感谢您的帮助 抱歉,它仍然无法正常工作。我的意思是文件夹名称应该是“!! Test05” –
@SandeepPrasad如果您运行此行:md“C:\ Test \ Test01 \ Test02 \ Test03 \ Test04 \ ^!^!Test05 \ Test06 \ Final” 它是否适用于您? – Scheme
谢谢,但我需要存储在可靠 –