1.

Solve : Fixed width?

Answer»

The place I'm starting from is the first stepping stone to a more complex batch file.  What I want to have the batch file do at the moment is count from one input number to the second input number by 1.  I have it working to get the two numbers and can get it to count up.  the issue I run into is trying to zero pad the front of my numbers.  This is what I have right now.
Code: [Select]echo off
Title TG test
echo %DATE% %TIME%
for /l %%a in (%1,1,%2) do (
if /i %%a LSS 10 ( echo 00%%a)
if /i %%a LSS 100 ( echo 0%%a)
if /i %%a LSS 1000 ( echo %%a)
)

What I get when I run this as test.bat 1 10 is
001
01
1
002
02
2
so on and so forth.  What I would like is
001
002
003
....
010

Any help would be great.Not too difficult, you just want to use ELSE with your if testing. So:
Code: [Select]echo off
Title TG test
echo %DATE% %TIME%
for /l %%a in (%1,1,%2) do (
  if /i %%a LSS 10 (echo 00%%a) else (
    if /i %%a LSS 100 (echo 0%%a) else (
      if /i %%a LSS 1000 (echo %%a)
    )
  )
)I've tried that.  This is the output I get using that.
001 else (
01 else (
1
002 else (
02 else (
2
003 else (
03 else (
3
004 else (
04 else (
4
005 else (
05 else (
5
006 else (
06 else (
6
007 else (
07 else (
7
008 else (
08 else (
8
009 else (
09 else (
9
010 else (
10

I don't know how to get rid of the echo on the else.Check the revision. I removed the parenthesis when I shouldn't have. My bad.Got it with
Code: [Select]for /l %%a in (%1,1,%2) do (
  if /i %%a LSS 10 (echo 00%%a) else (
    if /i %%a LSS 100 (echo 0%%a) else (
      if /i %%a LSS 1000 (echo %%a)
    )
  )
)
Thank you so much.  Pad the number with a larger number of pad characters than you will need (can be any character, not just zero). Then select the rightmost N-1 characters using the syntax

%string:~-N%

The number is 1 more than the desired number width. Here we show 3 digits. Since we are in a loop we use delayed expansion and !var! instead of %var%

Code: [Select]echo off
setlocal enabledelayedexpansion
for /l %%a in (%1,1,%2) do (
  set num=00000000000000000000%%a
  echo !num:~-4!
)I'm gonna stay with the way Raven suggested since I'm using it on a large scale batch file.  The echo was just to get the initial number counter working.  I'm using a few xcopy, cd, and another program CALLS with the number in the loop.
Thanks anyways. Quote from: Salmon Trout on December 29, 2011, 02:16:00 PM

Pad the number with a larger number of pad characters than you will need (can be any character, not just zero). Then select the rightmost N-1 characters using the syntax

%string:~-N%

The number is 1 more than the desired number width. Here we show 3 digits. Since we are in a loop we use delayed expansion and !var! instead of %var%

Code: [Select]echo off
setlocal enabledelayedexpansion
for /l %%a in (%1,1,%2) do (
  set num=00000000000000000000%%a
  echo !num:~-4!
)
Your code should run much QUICKER.  And as I always say time is money.

But I wasn't sure if the O/P wanted numbers greater than 99 to be padded with a zero.
Too me it looks like they wanted
001
010
100
1000Lemming, I understood about why you used echo. I use the method I showed above in a variety of DIFFERENT batch scripts, including bulk renaming of files and it is just as effective as any other method.

It is more efficient, unless you are specifically doing the tests for other reasons and there is other processing involved with each test.

If not, and if you were wanting to set a variable for instance, you would just change it to

Code: [Select]echo off
setlocal enabledelayedexpansion
for /l %%a in (%1,1,%2) do (
  set num=00000000000000000000%%a
  set var=!num:~-4!
)
But this is more about efficient programming than anything, and showing you another way to accomplish the task you initially wanted done. If your personal preference is to use my originally posted method, then by all means feel free to use it. This is what I'm doing and it makes it a little easier to do the brute force way vs variable.  It is easier since I am very new to batch file writing.  I have also expanded the batch file to take in two numbers when being called

Code: [Select]TG_batch.bat 1 2

Code: [Select]echo off
Title TG batch
for /l %%A in (%1, 1, %2) do (
    if /i %%A LLS 10 (
        xcopy t:\!template t:\run\00%%A
        copy t:\!def\00%%A.atmo t:\00%%A\part1\00%%A.atmo
        run other program 1
        copy output.testcase ..\part2\input.dat
        run other program 2
    ) else (
    if......
    if......
)
This is for the most part what I am doing with the help initially from Raven.  The file structure is only set to run no more than 100 iterations so getting to 1000 was never a concern.
Since I am new I don't know how to correctly IMPLEMENT the smaller set of batch code with what I need it to do.
I am extactic to see the number of responses to this.  It is just that at the time I don't know how to correctly use the new supplied way.
Thank you all for your help.another way if you want to see the powers of 10

Code: [Select]echo off
setlocal enabledelayedexpansion
REM pad with spaces
set "padchar= "
REM pad with zeroes
set padchar=0
for /l %%a in (%1,1,%2) do (
set pad=& for %%b in (10000 1000 100 10) do if %%a lss %%b set pad=%padchar%!pad!
echo !pad!%%a
)
Quote from: lemming622 on December 29, 2011, 05:28:14 PM
This is what I'm doing and it makes it a little easier to do the brute force way vs variable.  It is easier since I am very new to batch file writing.  I have also expanded the batch file to take in two numbers when being called

Code: [Select]TG_batch.bat 1 2

Code: [Select]echo off
Title TG batch
for /l %%A in (%1, 1, %2) do (
    if /i %%A LLS 10 (
        xcopy t:\!template t:\run\00%%A
        copy t:\!def\00%%A.atmo t:\00%%A\part1\00%%A.atmo
        run other program 1
        copy output.testcase ..\part2\input.dat
        run other program 2
    ) else (
    if......
    if......
)
This is for the most part what I am doing with the help initially from Raven.  The file structure is only set to run no more than 100 iterations so getting to 1000 was never a concern.
Since I am new I don't know how to correctly implement the smaller set of batch code with what I need it to do.
I am extactic to see the number of responses to this.  It is just that at the time I don't know how to correctly use the new supplied way.
Thank you all for your help.
If you do it that way you have to replicate all your commands inside each IF ELSE.  Use the IF ELSE to SET a variable with the padded number then do all your commands.


Discussion

No Comment Found