本文介绍了批处理-如何在作为参数传递给子例程的字符串中回显感叹号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

SET /A countArgs=1

FOR %%p in (%pathListToCheck%) DO (
    IF NOT EXIST %%p (
        CALL :error "!countArgs!. Argument -> bla!"
        EXIT /B 1
    )
    SET /A countArgs+=1
)

:error
    ECHO ERROR
    set x=%~1
    ECHO !x!
EXIT /B 0

不幸的是,感叹号没有得到 echo d.我还试图像 ^! ^^!那样转义它,但是它不起作用.

Unfortunately the exclamation mark does not get echod. I also tried to escape it like ^! and ^^! but it doesn't work.

我在这里使用延迟支出来使大号(> )起作用.如果我尝试直接ECHO参数( ECHO%〜1 ),它将失败.有关详细信息,请参见我的上一个问题

I use delayed expension here to make the greater-then sign (>) work. If i would try to ECHO the parameter directly (ECHO %~1) it would fail. For details see my previous question

如何解决此问题?

感谢您的帮助...

推荐答案

您没有在他的 answer .
setlocal enabledelayedexpansion 是导致感叹号消失的原因.没有理由在您当前的代码中使用它.

You didn't read/understand Stephans summary in his answer.
setlocal enabledelayedexpansion is the cause of the vanished exclamation marks.
There is no reason to use it in your present code.

如果您想在没有qoutes的情况下回显< |>& ,则必须将其转义.这可以通过代码来完成.

If you want to echo <|>& without qoutes you have to escape those. That can be done by code.

:: Q:\Test\2018\05\19\SO_50419709.cmd
@Echo off
SetLocal EnableExtensions DisableDelayedExpansion

SET /A countArgs=1

set "pathlisttocheck=%userprofile%,x:\x\x\"
FOR %%p in (%pathListToCheck%) DO (
    IF NOT EXIST %%p (
        CALL :error "%%countArgs%%. Argument -> bla!  %%~p"
        EXIT /B 1
    )
    SET /A countArgs+=1
)
EXIT /B 1

:error
ECHO ERROR
set "x=%~1"
set "x=%x:>=^>%"
ECHO %x%
EXIT /B 0

示例输出:

> Q:\Test\2018\05\19\SO_50419709.cmd
ERROR
2. Argument -> bla! x:\x\x\

这篇关于批处理-如何在作为参数传递给子例程的字符串中回显感叹号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:29