"Tom Lavedas" <> wrote in message
news:cd9b0c9b-da4f-4bd6-9c0d-...
On Feb 13, 2:25 pm, \RemS <R...@discussions.microsoft.com> wrote:
> "Al Dunbar" wrote:
>
> > "Pegasus (MVP)" <I....@fly.com.oz> wrote in message
> >news:%...
>
> > > <jnton...@gmail.com> wrote in message
> > >news:fd5319c6-7c35-4f27-aeac-...
> > >> 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
|