开发者

field width while using delayed expansion

I am using a for loop and constructing names of env variables ( like abc%%i , where i is the loop variable ) that are to be read , and use delayed expansion to print out their values (assuming these env variables have already been set ) ..

echo is dos provides the facility to mention the field width of 开发者_如何学运维the variable to be printed out like echo %x:~-8% will right justify the value of x and the field width will be 8 ..

But since i am using delayed expansion , !abc%%i:~-8! does not seem to be working .

Any ideas as to how to set field width while using delayed expansion ,??

Happiness Deepak


I don't know who told you that %x:~-8% would pad out your variable but they're wrong, at least in the version of cmd that I'm using (XP).

That construct will simply give tou the last 8 characters if the variable is 8 or more. If it's less than 8, you'll get the variable itself, sans padding.

If you want it padded, you can use something like:

set y=        %x%
echo %y:~-8%

And, as can be seen from this script, !abc%%i:~-8! works just fine once you realise that you're responsible for the padding:

@setlocal enableextensions enabledelayedexpansion
@echo off
for /f %%i in ('echo 1 ^&^& echo 92') do (
    set var%%i=        %%i
    echo !var%%i:~-8!
)
echo.=====
set var
endlocal

This outputs:

       1
      92
=====
var1=       1
var92=       92

as you would expect.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜