Search code examples
windowsbatch-filecmdmaxlengthdigit

How to create a number with N digits in Batch Script Windows?


I would like to create a number with N digits.

Example;

myVar=543 and I would like it with 6 digits so myVar will be 000543

myVar=44345 will be 044345
myVar=1 will be 0000001
...

I do that with Batch, so just with Windows Batch commands.


Solution

  • add 6 zeros at the front of the number and then cut the last 6 characters:

    set myVar=51
    echo 1: %myVar%
    set myVar=000000%myvar%
    echo 2: %myVar%
    set myVar=%myVar:~-6%
    echo 3: %myVar%