|
server
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Drop leading zeros in a variableusing 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. <jnton***@gmail.com> wrote in message
Show quoteHide quote news:fd5319c6-7c35-4f27-aeac-744700d05c80@t13g2000yqc.googlegroups.com... Not particularly elegant but it works . . .> 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. @echo off set x=0000012345 :Loop set x= %x: 0=%if [%x:~0,2%]==[ 0] goto Loop set x=%x:~1% echo x=%x%
Show quote
Hide quote
"Pegasus (MVP)" <I.***@fly.com.oz> wrote in message Here's another inelegant solution: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% @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
Show quote
Hide quote
"Al Dunbar" wrote: Or,> > "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 > @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 On Feb 13, 2:25 pm, \RemS <R***@discussions.microsoft.com> wrote:
Show quoteHide quote > "Al Dunbar" wrote: Here's my offering ...> > > "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 @echo off set "x=%~1" :loop if [%x:~0,1%]==[0] (set x=%x:~1% & goto :loop)echo %x% Tom Lavedas ========== "Tom Lavedas" <tglba***@cox.net> wrote in message On Feb 13, 2:25 pm, \RemS <R***@discussions.microsoft.com> wrote:news:cd9b0c9b-da4f-4bd6-9c0d-8dd63087ed94@i38g2000yqd.googlegroups.com... Show quoteHide quote > "Al Dunbar" wrote: Here's my offering ...> > > "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 @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
Batch Script to parse lines in text file
Command line parameters? Get attributes of user list from AD send mail if ping faile run once script Monitor redundent processes running on the Windows Server WMI Script, access denied? Deleting and re-installing printer with new name using login scrip Echo to column position Script for deleting folders + content in root of D:\ after # days |
|||||||||||||||||||||||