如何在XP cmd脚本中将substring命令应用于双百分数变量? [英] how to apply substring command to double percent variable in XP cmd scripts?

查看:71
本文介绍了如何在XP cmd脚本中将substring命令应用于双百分数变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是使用普通变量的示例:

here is the example how you do it with normal variables:

SET _test=123456789abcdef0
SET _result=%_test:~-7%
ECHO %_result%
:: that shows: abcdef0

但是在开始时要处理具有双百分数的变量(例如 %% A ),在for循环中需要这样的变量:

But what to do with variables with double percent at the begin (like %%A), variables like this are needed in for loops:

FOR /D %%d IN (c:\windows\*) DO (
  echo %%d
)

这有效,但是:

FOR /D %%d IN (c:\windows\*) DO (
  echo %%d:~-7%
)

只需将:〜-7 复制到echo命令中

simply copies :~-7 into the echo command

推荐答案

replace和substring语法仅适用于变量,不适用于参数。

The replace and substring syntax only works for variables not for parameters.

但是您可以简单地将参数复制到变量中,然后使用子字符串语法。

But you can simply copy the parameter into a variable and then use the substring syntax.

setlocal EnableDelayedExpansion
FOR /D %%d IN (c:\windows\*) DO (
  set "var=%%d"
  echo !var:~-7!
)

此处您需要延迟的扩展,通常是% var%将在解析完整块时扩展,而不是在执行时扩展。

You need here the delayed expansion, as a normal %var% would be expanded while parsing the complete block, not at execution time.

或者您可以使用 call 技术,但这非常慢并且有很多副作用。

Or you could use the call technic, but this is very slow and have many side effects.

FOR /D %%d IN (c:\windows\*) DO (
  set "var=%%d"
  call echo %%var:~-7%%
)

这篇关于如何在XP cmd脚本中将substring命令应用于双百分数变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆