批处理脚本从 FOR 循环内的变量中删除字符
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (*.*) DO (
SET var=%%A
ECHO !var:~0,-4!
)
由于我们在 FOR 循环中迭代变量,因此您必须在变量周围使用 !
,但是,结合方法 :~#,-#
来删除从变量末尾开始的字符,它不需要。
在我的示例中,我们只是从文件名中删除扩展名。我知道您可以使用 %%~nA
来获取文件名,但这只是一个用法示例。
有没有办法在 FOR 循环内部执行此操作?也许与我正在使用的方法不同?
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (*.*) DO (
SET var=%%A
ECHO !var:~0,-4!
)
Since we're iterating the variable within the FOR loop, you have to use !
around the variable, however, in conjunction with the method :~#,-#
to remove characters from the end of the variable, it doesn't take.
In my example, we are just removing the extension from the filename. I understand that you can use %%~nA
to just get the filename, but this is just an example of usage.
Is there a way to do this inside of the FOR loop? Maybe a different method than what I am using?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
set var=%%A
后面有隐藏空格,则可能会失败。那么你只删除空格,因此最好使用
set "var=%%A"
它与隐藏空格无关,甚至最后一个引号后面的字符也会被忽略。
但也许您的问题属于完全不同的类型?
It could fail if there are hidden spaces behind
set var=%%A
.Then you only remove the spaces, therefore it's better to use
set "var=%%A"
It's independent of hidden spaces, even characers are ignored behind the last quote.
But perhaps your problem is of a completly different type?