C:\_G\WWW\~ELISANET\INFO\tscmd029.html
<http://www.elisanet.fi/tsalmi/info/tscmd029.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Fri 12-Oct-2018 02:41:19

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



29} How do I drop the first two characters of a string, and so on?

In CMD.EXE scripts dropping the first two characters is very simple:
  @echo off & setlocal enableextensions
  setlocal enableextensions
  set test_=This is a test
  echo %test_:~2%
  endlocal & goto :EOF

The output is
is is a test

As we have seen, the base syntax for extracting a substring is %var_:Start,Count% where the first character is designated 0 (zero) not one. Thus
  @echo off & setlocal enableextensions
  setlocal enableextensions
  set test_=This_is_a_test
  echo %test_:~2,5%
  endlocal & goto :EOF

gives
is_is

Now, how about dropping the LAST two characters?
  @echo off & setlocal enableextensions
  setlocal enableextensions
  set test_=This_is_a_test
  echo %test_:~0,-2%
  endlocal & goto :EOF

gives
This_is_a_te

The above drops the last two characters, but reversing how does one get them?
  @echo off & setlocal enableextensions
  setlocal enableextensions
  set test_=This is a test
  echo %test_:~-2%
  endlocal & goto :EOF

gives
st

Obviously there are more potential combinations, but this is how the basics go. For more see CMD1DEMO.CMD " A CMD syntax selection demo". Also see Item #44.

For the sake of comparison let's demonstrate solving dropping the first two characters with a Visual Basic aided script (VBScript):
  @echo off & setlocal enableextensions
  setlocal enableextensions
  set test_=This is a test
  echo %test_%
  >"%temp%\tmp$$$.vbs" echo WScript.Echo Mid("%test_%",3)
  for /f "tokens=* delims=" %%a in (
    'cscript //nologo "%temp%\tmp$$$.vbs"') do set test_=%%a
  for %%f in ("%temp%\tmp$$$.vbs") do if exist %%f del %%f
  echo %test_%
  endlocal & goto :EOF

The output is
C:\_D\TEST>cmdfaq
This is a test
is is a test

With G(nu)AWK:
  @echo off & setlocal enableextensions
  setlocal enableextensions
  set test_=This is a test
  echo %test_%
  gawk 'BEGIN{printf "set test_=%%s\n",substr("%test_%",3)}'>"%temp%\tmp$$$.cmd"
  for %%c in (call del) do %%c "%temp%\tmp$$$.cmd"
  echo %test_%
  endlocal & goto :EOF

  @echo off & setlocal enableextensions
  setlocal enableextensions
  rem The same with GnuWin32 gawk (let's call it unxgawk)
  set test_=This is a test
  echo %test_%
  unxgawk "BEGIN{printf \"set test_=%%s\n\",substr(\"%test_%\",3)}"^
    >"%temp%\tmp$$$.cmd"
  for %%c in (call del) do %%c "%temp%\tmp$$$.cmd"
  echo %test_%
  endlocal & goto :EOF

References/Comments: (If a Google message link fails try the links within the brackets.)
  SET /?
  Google Groups Jan 11 2003, 7:40 pm [M]
  VBScript Microsoft

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd029.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd029
http://www.elisanet.fi/tsalmi/info/tscmd029.html
file:///c:/_g/www/~elisanet/info/tscmd029.html