开发者

Batch Script Removing characters from a variable inside a FOR loop

开发者 https://www.devze.com 2023-04-06 10:01 出处:网络
SETLOCAL ENABLEDELAYEDEXPANSION FOR %%A IN (*.*) DO ( SET var=%%A ECHO !var:~0,-4! ) Since we\'re iterating the variable within the FOR loop,开发者_Python百科 you have to use ! around the variable
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (*.*) DO (
 SET var=%%A
 ECHO !var:~0,-4!
)

Since we're iterating the variable within the FOR loop,开发者_Python百科 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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号