home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   alt.msdos.batch.nt      Fun with Windows NT batch files      68,980 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 67,544 of 68,980   
   probyn@berkeley.edu to Tom Del Rosso   
   Re: Remove all but digits   
   22 May 20 12:51:39   
   
   On Friday, May 22, 2020 at 7:31:03 AM UTC-7, Tom Del Rosso wrote:   
   > The objective is to remove everything except digits 0-9 from a string.   
   >    
   > %alpha_substrings% does contain all possible non-numeric substrings    
   > (space separated), so this should remove them and leave only the digits.   
   >    
   > set "only_numbers=string"   
   > for %%t in (%alpha_substrings%) do (   
   >         call set "only_numbers=%%only_numbers:%%t= %%"   
   > )   
   >    
   > But some characters in the non-numeric substrings, like =, don't get    
   > removed because they are never included in %%t.   
   >    
   > If FOR/F is used then it includes those characters because they are not    
   > seen as delimiters, but it doesn't spit them out one at a time in    
   > multiple loops.   
   >    
   > I'm tempted to use GWBASIC (MS just made it open-source!).   
   >    
   >    
   > --   
      
   Herbert has provided an elegant solution, as always. Here's another approach:   
      
   File=OnlyNumbers.cmd   
      
   Line   ····.····1····.····2····.····3·   
   Â·Â·.····4····.   
   00001. @echo off   
   00002.  setlocal   
   00003.  set inpt=%*   
   00004. :char   
   00005.  set char=%inpt:~0,1%   
   00006.  set inpt=%inpt:~1%   
   00007.  echo/%char%|FindStr /R "[0-9]" > nul:   
   00008.  if %errorlevel% equ 0 set out=!out!%char%   
   00009.  if defined inpt goto :char   
   00010.  echo/%*   
   00011.  echo/%out%   
   00012.  endlocal& set %~n0=%out%&goto :EOF   
      
      
   Here's some sample output:   
      
   C:\Users\JoeUser\temp: onlynumbers 3x3= 1 + 8 = 7 + 2   
   3x3= 1 + 8 = 7 + 2   
   331872   
      
   C:\Users\JoeUser\temp: onlynumbers abc 123 a29832hadffgwe3r384rheuw z43828   
   abc 123 a29832hadffgwe3r384rheuw z43828   
   12329832338443828   
      
   (The numbers are returned in variable OnlyNumbers)   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca