<http://www.netikka.net/tsneti/info/tscmd047.htm>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Mon 23-Feb-2015 13:33:30

 
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.



47} Is it possible to echo the redirection symbol in a script?

There are several examples to that effect throughout the FAQ at hand. You may wish to search through the FAQ for instances of the caret ^ escape character.*)   Note that on occasion the caret can also be used to continue a command line.

*) The caret is the regular escape character in cmd.exe command line programming. However, depending on the circumstances the escape character can (or must) be %, "" or \ as will be seen in the some of the examples below.

An example of echoing the pipe symbol:
  @echo off & setlocal enableextensions disabledelayedexpansion
  echo |Hello World!|
  endlocal & goto :EOF
will produce
  C:\_D\TEST>cmdfaq
  The syntax of the command is incorrect.
while
  @echo off & setlocal enableextensions disabledelayedexpansion
  echo ^|Hello World!^|
  endlocal & goto :EOF
will produce
  C:\_D\TEST>cmdfaq
  |Hello World!|
Also note the option of using the following to produce the same result
  @echo off & setlocal enableextensions enabledelayedexpansion
  echo ^|Hello World^^!^|
  endlocal & goto :EOF

It is not the only method, though, as reminded in Google Groups Dec 29 2003, 2:39 am [M]
The line below is an exact extract from that message:
  <nul (set /p z="<this>|<is>|<only>|<a>|<TEST>")>out.txt

A second option to produce the said text is
  for %%a in ("<this>|<is>|<only>|<a>|<TEST>") do echo %%~a
which also will give you
  <this>|<is>|<only>|<a>|<TEST>
The difference is that set /p method will not produce a new line while the second one will.

You could also write
  for /f "delims=" %%a in ("<this>|<is>|<only>|<a>|<TEST>") do echo %%~a

One way of producing any character is using G(nu)AWK. For example, to produce the redirection symbol (>) Hex 3E apply
  gawk 'BEGIN{print "\x3E\n"}'
or with GnuWin32 gawk (let's call it unxgawk)
  unxgawk "BEGIN{print \"\x3E\n\"}"
or, in octal
  unxgawk "BEGIN{print \"\076\n\"}"
Note that in the UNIX-originating gawk the escape character is \ not ^ hence the \"

Likewise, VBScript could be used
  @echo off & setlocal enableextensions
  >"%temp%\tmp$$$.vbs" echo WScript.Echo "Redirection symbol >"
  cscript //nologo "%temp%\tmp$$$.vbs"
  for %%f in ("%temp%\tmp$$$.vbs") do if exist %%f del %%f
  endlocal & goto :EOF

The output would be
  C:\_D\TEST>cmdfaq
  Redirection symbol >

Or, more generally
  @echo off & setlocal enableextensions
  >"%temp%\tmp$$$.vbs" echo WScript.Echo "Any valid symbol " + Chr(62)
  cscript //nologo "%temp%\tmp$$$.vbs"
  for %%f in ("%temp%\tmp$$$.vbs") do if exist %%f del %%f
  endlocal & goto :EOF

The output would be
  C:\_D\TEST>cmdfaq
  Any valid symbol >

Also the methods presented in the MS-DOS+Win..95/98/ME FAQ item
  "38) Is it possible to echo the redirection symbol in a batch?"
  http://www.netikka.net/tsneti/pc/link/tsbat.zip
should work. But these are just additional, complicated ways of doing what is already covered by the ^ escape character.

Furthermore, consider the following example demonstrating a difference between MS-DOS+Win../95/98/Me COMMAND.COM batches and NT/2000/XP CMD.EXE scripts. The user wishes to echo the ampersand (&) character. For the former neither the caret ^ nor the ampersand & has a special meaning while for the latter they do. For CMD.EXE the caret in the escape character and the ampersand & the multiple commands on one command line character. Thus given
  @echo off
  echo &
  echo ^&
MS-DOS+Win../95/98/Me will produce
  &
  ^&
while NT/2000/XP will produce
  ECHO is off.
  &
Hence for NT/2000/XP the solution is
  @echo off
  echo Echoing the ^& ampersand

The NT/2000/XP special characters which require escaping are space&()[]{}^=;!'+,`~

Let's take an example. How does one echo the following from within a command script?
  ^null@uwasa\.fi$
The answer
  @echo off & setlocal enableextensions
  rem Alternative 1
  echo ^^null@uwasa\.fi$
  rem Alternative 2
  for %%i in ("^null@uwasa\.fi$") do echo %%~i
  endlocal & goto :EOF

One alternative, just for demonstration, is
  @echo off & setlocal enableextensions %void%
  goto MainScript %void%
  ^null@uwasa\.fi$
  :MainScript %void%
  findstr /v /c:"void" "%~0" %void%
  endlocal & goto :EOF %void%

The output
  C:\_D\TEST>cmdfaq.cmd
  ^null@uwasa\.fi$

Escaping must be done the right number of times. For example
  @echo off & setlocal enableextensions
  for %%i in ("yes ^& no") do echo %%~i
  endlocal & goto :EOF
will produce
  yes ^& no
because the quotes already effect the escaping once. That same output would, of course, also be produced by
  @echo off & setlocal enableextensions
  echo yes ^^^& no
  endlocal & goto :EOF
demonstrating the need to escape exactly the right number of times to get the results one may be striving at.

A question from microsoft.public.win2000.cmdprompt.admin:
I (Tom) am trying to have a BAT file generate another BAT file. In that target BAT file, I would like to write values like:

CALL MyBatFile.bat %1 %2 > MyTargetOutputFile

I tried executing the following command...

echo "CALL MyBatFile.bat %1 %2 > MyTargetOutputFile" >> NewBatFile.bat

Of course the %1 and %2 are getting substituted (I just want the literal values %1 and %2 written). Furthermore the first > tries to pipe the output to MyTargetOutputFile (I also wanted "> MyTargetOutputFile" to be written to the NewBatFile.bat. Is there any way to do what I want?

The answer:
  echo CALL MyBatFile.bat %%1 %%2 ^> MyTargetOutputFile >> NewBatFile.bat

A somewhat similar question to echoing a redirection character is the question how to echo a % from within a script.
  @echo off
  echo Give it a 100%%

  C:\_D\TEST>cmdfaq
  Give it a 100%

Consider another special demonstration
  @echo off
  echo "&"
  set /a decand_=121 "&" 145
  echo %decand_%

The output will be
  C:\_D\TEST>cmdfaq
  "&"
  17
Why the 17? Because "&" is an escaped bitwise AND as per e.g. item #102.

Still a further example of an "irregular" escape character in cmd.exe command line programming:
  @echo off & setlocal enableextensions
  findstr /a:0E /s /p /i "\-x.*\"\*\.\*\"" C:\_G\*.HTM
  endlocal & goto :EOF

The ouput could be e.g.
  C:\_D\TEST>cmdfaq
  C:\_G\WWW\INFO\tscmd047.htm
  C:\_G\WWW\INFO\tscmd098.htm
The cases matched in the above example were -x"*.*" and -x "*.*"

Question:
> I have a file (saytest.txt) which includes the following line
>  3EPlus v4.0 (C:\Program Files\3EPlus4\)
>How do I match it with FINDSTR? It does not seem to work.

This is exactly what has been covered in this item. It is about the escape characters. The relevant search is
  findstr /c:"3EPlus v4.0 (C:\\Program Files\\3EPlus4\\)" test.txt



Consider a real-life example involving among other things echoing < and >. The task is about converting between the email address book formats of different programs.

In the address book of the UNIX elm the format of an invidiual adress is
tsuvasa     = Timo Salmi = ts@uvasa.fi
In Thunderbird the format is the very common
Timo Salmi <ts@uvasa.fi>
and in the CSV (Microsoft Excel Comma Separated Values File) format
,,Timo Salmi,,ts@uvasa.fi,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Given an elm address book, the task is 1) to pick an address based on the first field (called the alias) and to convert the found address into 2) the Thunderbird compliant format, and 3) the CSV format.

  @echo off & setlocal enableextensions
  echo +----------------------------------------------------+
  echo ELMC.CMD Expand a UNIX elm email alias             
  echo By prof. Timo Salmi, Last modified Sat 22-Nov-2008
  echo +----------------------------------------------------+
  echo.

  :: Set the relevant environment variables
  set vbs_=%temp%\tmp$$$.vbs
  set source_=C:\ELM\ALIASES.TEXT
  set alias_=%~1

  :: Is help asked for
  if "%~1"=="" goto _help
  if "%~1"=="?" goto _help
  if "%~1"=="/?" goto _help

  :: Is the alias present in the aliases file
  findstr /b /i /c:"%alias_% " "%source_%">nul
  if %errorlevel% GTR 0 (
    echo Alias %alias_% not found in "%source_%"
    goto _out)

  :: Echo the relevant line from the elm aliases file
  findstr /b /i /c:"%alias_% " "%source_%"

  :: Establish the name and the address, untrimmed, allow apostrophes
  for /f "tokens=2-3 usebackq delims==" %%a in (`
    findstr /b /i /c:"%alias_% " "%source_%"`) do (
      set name_=%%a
      set addr_=%%b)

  :: Remove any quote (") characters
  set name_=%name_:"=%
  set addr_=%addr_:"=%

  :: Trim the name and the address, i.e. remove leading and trailing blanks
  >"%vbs_%" echo WScript.Echo Trim("%name_%")
  for /f "tokens=* delims=" %%a in (
    'cscript //nologo "%vbs_%"') do set name_=%%a
  >"%vbs_%" echo WScript.Echo Trim("%addr_%")
  for /f "tokens=* delims=" %%a in (
    'cscript //nologo "%vbs_%"') do set addr_=%%a

  :: Display the results
  for %%a in ("<%addr_%>") do echo %name_% %%~a
  echo ,,%name_%,,%addr_%,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
  goto _out

  :_help
  echo Usage: ELMC alias
  goto _out

  :_out
  for %%f in ("%vbs_%") do if exist %%f del %%f
  endlocal & goto :EOF

The output will be
  C:\_D\TEST>ELMC tsuvasa
  +----------------------------------------------------+
  ELMC.CMD Expand a UNIX elm email alias             
  By prof. Timo Salmi, Last modified Sat 22-Nov-2008
  +----------------------------------------------------+

  tsuvasa     = Timo Salmi = ts@uvasa.fi
  Timo Salmi <ts@uvasa.fi>
  ,,Timo Salmi,,ts@uvasa.fi,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

References/Comments: (If a Google message link fails try the links within the brackets.)
  hh ntcmds.chm::/echo.htm
  Google Groups 4 Jul 2008 20:16:14 +0300 [M]