CMD EnableDelayedExpansion和变量内的感叹号

时间:2014-08-11 10:55:04

标签: loops variables cmd

我尝试从ttt.txt文件中读取命令,然后在循环中运行它们

“my program.exe”是一个winrar SFX存档,-d是目标路径参数。问题是,如果DestinationDir varibale包含特殊字符,如“!”感叹号

我试图替换!跟^! ,目的地路径然后包含!还有^ char。

ttt.txt

md "Some folder"
"c:\tmp\my program.exe" -d!DestinationDir!
md "myNewDir"
...
another commands

例如: Execute.bat“C:\ new Folder [!!! $#,;] path \”“Test”

Execute.bat

@echo off

set "DestinationDir=%1"
set "MenuGroupDir=%2"

@echo par1=%DestinationDir%
SET DestinationDir="%DestinationDir:!=^!%"
SETLOCAL EnableDelayedExpansion

@echo DestinationDir= %DestinationDir% !DestinationDir!



IF exist %DestinationDir% IF [%MenuGroupDir%] NEQ [] (
    @echo destination-dir=%DestinationDir%
    @echo menugroup-dir=%MenuGroupDir%
    FOR /F "usebackq tokens=* delims=," %%G IN ("ttt.txt") DO  (
    %%G
    )

    )
) ELSE (
    @echo DestinationDir=%DestinationDir%
    @echo MenuGroupDir=%MenuGroupDir%
    @echo.
    @echo Usage:
    @echo Parameter1 must be Destination (Dir must Exist)
    @echo Parameter2 must be MenuGroup Dir
)

pause

0 个答案:

没有答案