Solved left padding in DOS command

October 10, 2013 at 21:29:06
Specs: Windows 7
I have this simple script. It will create a new file contain 999-999.
If user enter 99 it must show 099. How to use the left padding here?


@echo off
Set /P p_rr_fr= Please enter Running No From : || Set p_rr_fr=NothingChosen
If "%p_rr_fr%"=="NothingChosen" goto :sub_error

Set /P p_rr_to= Please enter Running No To : || Set p_rr_to=NothingChosen
If "%p_rr_to%"=="NothingChosen" goto :sub_error

echo %p_rr_fr%-%p_rr_to%>running_no.txt


echo Please key in Running No

See More: left padding in DOS command

Report •

October 10, 2013 at 23:07:21
✔ Best Answer
::====== script starts here ===============
:: pad.bat 2013-10-11 13:03:12.57
@echo off & setLocal enableDELAYedeXpansioN

set /p Z= num?
if !Z! lss 1000 set N=0!Z!
if !Z! lss 100 set N=00!Z!
if !Z! lss 10 set N=000!Z!

::====== script ends here =================

M2 Golden-Triangle

Report •

October 11, 2013 at 11:43:33
Hey M2.
Yeh, but... but...but... what if the person enters, for example, "09" or "009"? (which would technically qualify as "correct" data I guess). I've always leaned toward the "pack and hack" method I learned on this forum:
set z=000%z%
set z=%z:~-3%

also, you padded for 4 places

Report •

October 11, 2013 at 19:01:02
Pack and hack is the best name for padding I've heard so far, and I'm totally going to use that name from now on.

That said, technically both examples should probably run the number though SET /A, just to ensure it's a number in a standard decimal format. After all, 0xA is technically a valid number.

How To Ask Questions The Smart Way

Report •

Related Solutions

October 11, 2013 at 19:27:03
@Razor, Ha ha! But "beware the fruminous bandersnatch" of octal notation as wielded by batch! (Poetry was not intended, at first!) I quickly learned that adding zero to "08" or "09" gave me a result of 0 since 08 and 09 are interp. as zero by batch. Octal's a batch-trap waiting to devour its next newbie victim.

Report •

October 12, 2013 at 02:56:34

Yours is better.

I didn't allow for BS entries.

M2 Golden-Triangle

Report •

Ask Question