Remove leading zeros in batch file

后端 未结 4 1636
逝去的感伤
逝去的感伤 2020-12-05 23:57

In my application, I get a number having leading zeros. I am trying to trim the leading zeros and get the actual number. I tried using /a switch which considers

4条回答
  •  囚心锁ツ
    2020-12-06 00:25

    You can use FOR /F to remove leading zeros.

    C:\>SET n=00030
    echo off
    for /f "tokens=* delims=0" %a in ("%n%") DO echo %a
    30
    

    As you can see, the delims is set to 0. This will makes 0 as a delimiter. At the same time with tokens of * this will ensure that the leading 0's will be removed while the rest of the line will be processed (including trailing 0's).

    You may refer to this link for more information about removal of leading 0's.

    P.S. Do remember to use %%a instead of %a when you are running on batch file in FOR /F.

提交回复
热议问题