Home All Groups Group Topic Archive Search About

Drop leading zeros in a variable

Author
13 Feb 2009 4:59 PM
jntoner1
I'm using a variable that is a random hex number in my batch file. I'm
using this value to search some registry keys, but the registry keys
don't use the leading zeros. So, I'm looking for a way to drop only
the leading zeros in my variable.

It's always an 8 character value and could contain up to 7 zeros. I
can do:

set VAR=%VAR:0=%

....but this drops all zeros instead of just the leading zeros. So if
my VAR=00110010, I would just want to drop the leading two zeros so my
result would be VAR=110010. If my VAR=0000000A, I'd want to drop all 7
leading zeros with an end result of VAR=A.

It's also valid to have all zeros for this number, so in that case, I
would want to drop all zeros leaving VAR undefined.

Any pointers would be greatly appreciated.

Author
13 Feb 2009 5:17 PM
Pegasus (MVP)
<jnton***@gmail.com> wrote in message
Show quoteHide quote
news:fd5319c6-7c35-4f27-aeac-744700d05c80@t13g2000yqc.googlegroups.com...
> I'm using a variable that is a random hex number in my batch file. I'm
> using this value to search some registry keys, but the registry keys
> don't use the leading zeros. So, I'm looking for a way to drop only
> the leading zeros in my variable.
>
> It's always an 8 character value and could contain up to 7 zeros. I
> can do:
>
> set VAR=%VAR:0=%
>
> ...but this drops all zeros instead of just the leading zeros. So if
> my VAR=00110010, I would just want to drop the leading two zeros so my
> result would be VAR=110010. If my VAR=0000000A, I'd want to drop all 7
> leading zeros with an end result of VAR=A.
>
> It's also valid to have all zeros for this number, so in that case, I
> would want to drop all zeros leaving VAR undefined.
>
> Any pointers would be greatly appreciated.

Not particularly elegant but it works . . .
@echo off
set x=0000012345
:Loop
set x= %x: 0=%
if [%x:~0,2%]==[ 0] goto Loop
set x=%x:~1%
echo x=%x%
Are all your drivers up to date? click for free checkup

Author
13 Feb 2009 6:08 PM
Al Dunbar
Show quote Hide quote
"Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
news:%23oezs7fjJHA.1388@TK2MSFTNGP06.phx.gbl...
>
> <jnton***@gmail.com> wrote in message
> news:fd5319c6-7c35-4f27-aeac-744700d05c80@t13g2000yqc.googlegroups.com...
>> I'm using a variable that is a random hex number in my batch file. I'm
>> using this value to search some registry keys, but the registry keys
>> don't use the leading zeros. So, I'm looking for a way to drop only
>> the leading zeros in my variable.
>>
>> It's always an 8 character value and could contain up to 7 zeros. I
>> can do:
>>
>> set VAR=%VAR:0=%
>>
>> ...but this drops all zeros instead of just the leading zeros. So if
>> my VAR=00110010, I would just want to drop the leading two zeros so my
>> result would be VAR=110010. If my VAR=0000000A, I'd want to drop all 7
>> leading zeros with an end result of VAR=A.
>>
>> It's also valid to have all zeros for this number, so in that case, I
>> would want to drop all zeros leaving VAR undefined.
>>
>> Any pointers would be greatly appreciated.
>
> Not particularly elegant but it works . . .
> @echo off
> set x=0000012345
> :Loop
> set x= %x: 0=%
> if [%x:~0,2%]==[ 0] goto Loop
> set x=%x:~1%
> echo x=%x%

Here's another inelegant solution:

    @echo off

    set _in=00004006

    set _out=\%_in%
    set _out=%_out:\0=\%
    set _out=%_out:\0=\%
    set _out=%_out:\0=\%
    set _out=%_out:\0=\%
    set _out=%_out:\0=\%
    set _out=%_out:\0=\%
    set _out=%_out:\0=\%
    set _out=%_out:\=%
    echo/"%_in%" - "%_out%"

/Al
Author
13 Feb 2009 7:25 PM
RemS
Show quote Hide quote
"Al Dunbar" wrote:

>
> "Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
> news:%23oezs7fjJHA.1388@TK2MSFTNGP06.phx.gbl...
> >
> > <jnton***@gmail.com> wrote in message
> > news:fd5319c6-7c35-4f27-aeac-744700d05c80@t13g2000yqc.googlegroups.com...
> >> I'm using a variable that is a random hex number in my batch file. I'm
> >> using this value to search some registry keys, but the registry keys
> >> don't use the leading zeros. So, I'm looking for a way to drop only
> >> the leading zeros in my variable.
> >>
> >> It's always an 8 character value and could contain up to 7 zeros. I
> >> can do:
> >>
> >> set VAR=%VAR:0=%
> >>
> >> ...but this drops all zeros instead of just the leading zeros. So if
> >> my VAR=00110010, I would just want to drop the leading two zeros so my
> >> result would be VAR=110010. If my VAR=0000000A, I'd want to drop all 7
> >> leading zeros with an end result of VAR=A.
> >>
> >> It's also valid to have all zeros for this number, so in that case, I
> >> would want to drop all zeros leaving VAR undefined.
> >>
> >> Any pointers would be greatly appreciated.
> >
> > Not particularly elegant but it works . . .
> > @echo off
> > set x=0000012345
> > :Loop
> > set x= %x: 0=%
> > if [%x:~0,2%]==[ 0] goto Loop
> > set x=%x:~1%
> > echo x=%x%
>
> Here's another inelegant solution:
>
>     @echo off
>
>     set _in=00004006
>
>     set _out=\%_in%
>     set _out=%_out:\0=\%
>     set _out=%_out:\0=\%
>     set _out=%_out:\0=\%
>     set _out=%_out:\0=\%
>     set _out=%_out:\0=\%
>     set _out=%_out:\0=\%
>     set _out=%_out:\0=\%
>     set _out=%_out:\=%
>     echo/"%_in%" - "%_out%"
>
> /Al
>

Or,

  @echo off

  set "in#=00 00 B 1234050"

  Set "out#=%in#%"  you could replace all spaces here (optional).
  :TrimLeadingZeros from out#

  If "%out#:~0,1%"=="0" (
  Set "out#=%out#:~1%"
  Goto:TrimLeadingZeros)

  :: Left Trim
  If "%out#:~0,1%"==" " (
  Set "out#=%out#:~1%"
  Goto:TrimLeadingZeros)

  echo/%in#% =^> %out#%

  pause>nul


\Rems
Author
13 Feb 2009 9:09 PM
Tom Lavedas
On Feb 13, 2:25 pm, \RemS <R***@discussions.microsoft.com> wrote:
Show quoteHide quote
> "Al Dunbar" wrote:
>
> > "Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
> >news:%23oezs7fjJHA.1388@TK2MSFTNGP06.phx.gbl...
>
> > > <jnton***@gmail.com> wrote in message
> > >news:fd5319c6-7c35-4f27-aeac-744700d05c80@t13g2000yqc.googlegroups.com....
> > >> I'm using a variable that is a random hex number in my batch file. I'm
> > >> using this value to search some registry keys, but the registry keys
> > >> don't use the leading zeros. So, I'm looking for a way to drop only
> > >> the leading zeros in my variable.
>
> > >> It's always an 8 character value and could contain up to 7 zeros. I
> > >> can do:
>
> > >> set VAR=%VAR:0=%
>
> > >> ...but this drops all zeros instead of just the leading zeros. So if
> > >> my VAR=00110010, I would just want to drop the leading two zeros so my
> > >> result would be VAR=110010. If my VAR=0000000A, I'd want to drop all 7
> > >> leading zeros with an end result of VAR=A.
>
> > >> It's also valid to have all zeros for this number, so in that case, I
> > >> would want to drop all zeros leaving VAR undefined.
>
> > >> Any pointers would be greatly appreciated.
>
> > > Not particularly elegant but it works . . .
> > > @echo off
> > > set x=0000012345
> > > :Loop
> > > set x= %x: 0=%
> > > if [%x:~0,2%]==[ 0] goto Loop
> > > set x=%x:~1%
> > > echo x=%x%
>
> > Here's another inelegant solution:
>
> >     @echo off
>
> >     set _in=00004006
>
> >     set _out=\%_in%
> >     set _out=%_out:\0=\%
> >     set _out=%_out:\0=\%
> >     set _out=%_out:\0=\%
> >     set _out=%_out:\0=\%
> >     set _out=%_out:\0=\%
> >     set _out=%_out:\0=\%
> >     set _out=%_out:\0=\%
> >     set _out=%_out:\=%
> >     echo/"%_in%" - "%_out%"
>
> > /Al
>
> Or,
>
>   @echo off
>
>   set "in#=00 00 B 1234050"
>
>   Set "out#=%in#%"  you could replace all spaces here (optional).
>   :TrimLeadingZeros from out#
>
>   If "%out#:~0,1%"=="0" (
>   Set "out#=%out#:~1%"
>   Goto:TrimLeadingZeros)
>
>   :: Left Trim
>   If "%out#:~0,1%"==" " (
>   Set "out#=%out#:~1%"
>   Goto:TrimLeadingZeros)
>
>   echo/%in#% =^> %out#%
>
>   pause>nul
>
> \Rems

Here's my offering ...

@echo off
  set "x=%~1"
:loop
  if [%x:~0,1%]==[0] (set x=%x:~1% & goto :loop)
  echo %x%

Tom Lavedas
==========
Author
14 Feb 2009 4:07 AM
Al Dunbar
"Tom Lavedas" <tglba***@cox.net> wrote in message
news:cd9b0c9b-da4f-4bd6-9c0d-8dd63087ed94@i38g2000yqd.googlegroups.com...
On Feb 13, 2:25 pm, \RemS <R***@discussions.microsoft.com> wrote:
Show quoteHide quote
> "Al Dunbar" wrote:
>
> > "Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
> >news:%23oezs7fjJHA.1388@TK2MSFTNGP06.phx.gbl...
>
> > > <jnton***@gmail.com> wrote in message
> > >news:fd5319c6-7c35-4f27-aeac-744700d05c80@t13g2000yqc.googlegroups.com...
> > >> I'm using a variable that is a random hex number in my batch file.
> > >> I'm
> > >> using this value to search some registry keys, but the registry keys
> > >> don't use the leading zeros. So, I'm looking for a way to drop only
> > >> the leading zeros in my variable.
>
> > >> It's always an 8 character value and could contain up to 7 zeros. I
> > >> can do:
>
> > >> set VAR=%VAR:0=%
>
> > >> ...but this drops all zeros instead of just the leading zeros. So if
> > >> my VAR=00110010, I would just want to drop the leading two zeros so
> > >> my
> > >> result would be VAR=110010. If my VAR=0000000A, I'd want to drop all
> > >> 7
> > >> leading zeros with an end result of VAR=A.
>
> > >> It's also valid to have all zeros for this number, so in that case, I
> > >> would want to drop all zeros leaving VAR undefined.
>
> > >> Any pointers would be greatly appreciated.
>
> > > Not particularly elegant but it works . . .
> > > @echo off
> > > set x=0000012345
> > > :Loop
> > > set x= %x: 0=%
> > > if [%x:~0,2%]==[ 0] goto Loop
> > > set x=%x:~1%
> > > echo x=%x%
>
> > Here's another inelegant solution:
>
> > @echo off
>
> > set _in=00004006
>
> > set _out=\%_in%
> > set _out=%_out:\0=\%
> > set _out=%_out:\0=\%
> > set _out=%_out:\0=\%
> > set _out=%_out:\0=\%
> > set _out=%_out:\0=\%
> > set _out=%_out:\0=\%
> > set _out=%_out:\0=\%
> > set _out=%_out:\=%
> > echo/"%_in%" - "%_out%"
>
> > /Al
>
> Or,
>
> @echo off
>
> set "in#=00 00 B 1234050"
>
> Set "out#=%in#%" you could replace all spaces here (optional).
> :TrimLeadingZeros from out#
>
> If "%out#:~0,1%"=="0" (
> Set "out#=%out#:~1%"
> Goto:TrimLeadingZeros)
>
> :: Left Trim
> If "%out#:~0,1%"==" " (
> Set "out#=%out#:~1%"
> Goto:TrimLeadingZeros)
>
> echo/%in#% =^> %out#%
>
> pause>nul
>
> \Rems

Here's my offering ...

@echo off
  set "x=%~1"
:loop
  if [%x:~0,1%]==[0] (set x=%x:~1% & goto :loop)
  echo %x%

Tom Lavedas
==========

==> and the award for the simplest and most straightforward solution goes
to...

/Al

Bookmark and Share

Post Thread options