Advertisement
maphew

DosTips :Format function truncating results

Feb 9th, 2012
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.63 KB | None | 0 0
  1. :: cont'd from http://www.dostips.com/forum/viewtopic.php?f=3&t=2891&p=13349#p13349
  2. :: Here is the full script and output. It quits after processing the first loop:
  3. @echo off
  4. :showWinExe
  5.   setlocal enabledelayedexpansion
  6.   set string=arbitrary list of words that are at least this long in order to show truncation
  7.   call :Format [-15][-15][-15] %string%
  8.   goto :eof
  9.  
  10. :Format fmt str1 str2 ... -- outputs columns of strings right or left aligned
  11. ::                        -- fmt [in] - format string specifying column width and alignment, i.e. "[-10][10][10]"
  12. :$created 20060101 :$changed 20091130 :$categories Echo
  13. :$source http://www.dostips.com
  14. SETLOCAL
  15. set "fmt=%~1"
  16. set "line="
  17. set "spac=                                                     "
  18. set "i=1"
  19. echo. && echo. Recieved from caller: "%*" && echo.
  20. for /f "tokens=1,2 delims=[" %%a in ('"echo..%fmt:]=&echo..%"') do (
  21.     ECHO a = "[%%a]" : b = "[%%b]"
  22.     set /a i+=1
  23.     call call set "subst=%%%%~%%i%%%spac%%%%%~%%i%%"
  24.     if %%b0 GEQ 0 (call set "subst=%%subst:~0,%%b%%"
  25.     ) ELSE        (call set "subst=%%subst:~%%b%%")
  26.     call set "const=%%a"
  27.     call set "line=%%line%%%%const:~1%%%%subst%%"
  28.     ECHO Loop number: %i%
  29. )
  30. echo.%line%
  31. EXIT /b
  32. :: ------------ end of file ------------------
  33.  
  34. :: output:
  35. B:\code>format-test.bat
  36.  
  37.  Recieved from caller: "[-15][-15][-15] arbitrary list of words that are at least this long in order to show truncation"
  38.  
  39. a = "[.]" : b = "[-15]"
  40. Loop number: 1
  41. a = "[.]" : b = "[-15]"
  42. Loop number: 1
  43. a = "[.]" : b = "[-15]"
  44. Loop number: 1
  45. a = "[.]" : b = "[]"
  46. Loop number: 1
  47.       arbitrary           list             of
  48. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement