Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Re: Drop leading zeros in a variable

Reply
Thread Tools Display Modes

Re: Drop leading zeros in a variable

 
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      02-13-2009

<> 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%


 
Reply With Quote
 
 
 
 
Al Dunbar
Guest
Posts: n/a

 
      02-13-2009

"Pegasus (MVP)" <> wrote in message
news:%...
>
> <> 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


 
Reply With Quote
 
\RemS
Guest
Posts: n/a

 
      02-13-2009
"Al Dunbar" wrote:

>
> "Pegasus (MVP)" <> wrote in message
> news:%...
> >
> > <> 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
 
Reply With Quote
 
Al Dunbar
Guest
Posts: n/a

 
      02-14-2009

"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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
write zeros Bob Hansen Windows Vista General Discussion 14 01-23-2008 03:49 PM
Re: write zeros stuartbe Windows Vista Talk 0 01-22-2008 04:21 PM
Square zeros in IE7 tabs in task bar by start button Regi Internet Explorer 0 02-01-2007 04:25 AM
All Zeros Paul Richards Windows MSN Messenger 1 01-04-2005 09:36 PM
Leading Zero in channel changing. Randy Windows Media Center 4 10-28-2004 01:43 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59