C:\_G\WWW\~ELISANET\INFO\tscmd152.html
<http://www.elisanet.fi/tsalmi/info/tscmd152.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Sun 25-Nov-2018 14:56:53

 
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.



152} How can I remove all !:s and &:s from a text file with a script?

Special characters among &()[]{}^=;!'+,`~ in a text file may cause problems when the text file is processed with a CLI script. A SED solution can be used:
  sed -e "s/[\x21\x26]//g" C:\_M\MyText.txt
The character code values for ! and & respectively are in hexadecimal (21, 26). Given a text file like
  Hello World & others! Scripting is fun!
the output would be
  Hello World   others Scripting is fun

Another situation applying SED. Say that you wished to substitute all the @ characters with %40 in a file. Then
  @echo off
  sed -e "s/@/%%40/g" C:\_M\MyText.txt
Note the doubles in %%40 when within a script.

A Visual Basic Script (VBScript) aided solution can be used so that third party utilities are not needed
  @echo off & setlocal enableextensions
  rem C:\_M\TEST\CMDTEST.CMD
  ::

  set myfile_=C:\_M\TEST\My Test File.txt
  ::
  :: Build a Visual Basic Script

  set skip=
  set vbs_=%temp%\tmp$$$.vbs
  >"%vbs_%" findstr "'%skip%VBS" "%~f0"
  ::
  :: Run it with Microsoft Windows Script Host Version 5.6

  <"%myfile_%" cscript //nologo "%vbs_%"
  ::
  :: Clean up

  for %%f in ("%vbs_%") do if exist %%f del %%f
  endlocal & goto :EOF
  '
  '............................................
  'The Visual Basic Script
  '

  Do While Not WScript.StdIn.AtEndOfStream 'VBS
    str = WScript.StdIn.ReadLine 'VBS
    str = Replace (str, "&","") 'VBS
    str = Replace (str, "!","") 'VBS
    WScript.StdOut.WriteLine str 'VBS
  Loop 'VBS

C:\_M\TEST>type "My Test File.txt"
This & is ! line & 1
This & is ! line & 2
"& and ! this & is ! line 3"

C:\_M\TEST>cmdtest
This  is  line  1
This  is  line  2
" and  this  is  line 3"

One further question (also see item #34). How does one include removing quotes (") in the above VBS solution? One has to use the (decimal) Chr function:
  @echo off & setlocal enableextensions
  rem C:\_M\TEST\CMDTEST.CMD
  ::

  set myfile_=C:\_M\TEST\My Test File.txt
  ::
  :: Build a Visual Basic Script

  set skip=
  set vbs_=%temp%\tmp$$$.vbs
  >"%vbs_%" findstr "'%skip%VBS" "%~f0"
  ::
  :: Run it with Microsoft Windows Script Host Version 5.6

  <"%myfile_%" cscript //nologo "%vbs_%"
  ::
  :: Clean up

  for %%f in ("%vbs_%") do if exist %%f del %%f
  endlocal & goto :EOF
  '
  '............................................
  'The Visual Basic Script
  '

  Do While Not WScript.StdIn.AtEndOfStream 'VBS
    str = WScript.StdIn.ReadLine 'VBS
    str = Replace (str, "&","") 'VBS
    str = Replace (str, "!","") 'VBS
    str = Replace (str, Chr(34),"") 'VBS
    WScript.StdOut.WriteLine str 'VBS
  Loop 'VBS

C:\_M\TEST>cmdtest
This  is  line  1
This  is  line  2
and  this  is  line 3

[Previous] [Next]

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