NOTE: This tip has been superceeded by How can a batch job process files that contain command control and TAB characters?


If a file that you want to parse has command control characters, like an HTML file, the CMD.EXE processor will treat these characters as controls, causing your script to malfunction.

The ! % ^ & | " characters have special meaning to the CMD.EXE processor, as well as the = ; ( ) , : ' characters, when used in various combinations.

I have scripted CMDCTRL.bat, CMDCTRLdo.vbs, and CMDCTRLundo.vbs to allow a batch job to process a file that contains command control characters, by creating a new file in which the command control characters have been replaced, using the following table.

  Control Character   !     %     ^     &     |          >     "     =     ;     (     )     ,     :     '  
  Resulting Character     ¡     ¥     Þ     ¦     ü     ¼     ¾     ¢     ½     »     ¨     ©     ¬     º     ±  
  Keystrokes
  (Numeric Keypad)
  ALT+
  0161     0165     0222     0166     0252     0188     0190     0162     0189     0187     0168     0169     0172     0186     0177  

The syntax for using CMDCTRL.bat is:

CALL CMDCTRL iFile oFile \[/U\]

Where:

iFile is the fully qualified file name of the input file that contains the command Control Characters.                              oFile is the fully qualified file name of the output file that will Resulting Characters                              /U    is an optional switch that will reverse the operation, converting an iFile that contains                                    Resulting Characters  to an oFile that contains command Control Characters.
NOTE: CMDCTRL.bat, CMDCTRLdo.vbs, and CMDCTRLundo.vbs must all be located in the same folder.

CMDCTRL.bat contains:

                              @echo off                              setlocal                              if \{%2\}

\{\} goto err1 if not exist %1 goto err2 if exist %2 del /q /f %2 set iFile=%1 set oFile=%2 if /i %ifile% EQU %ofile% goto err3 if \{%3\}

\{\} goto doit if /i \{%3\}==\{/U\} goto undoit :err1 @echo Syntax: CMDCTRL iFile oFile /U endlocal goto :EOF :err2 @echo CMDCTRL - %1 NOT found. endlocal goto :EOF :err3 @echo CMDCTRL - %1 is the same file as %2. endlocal goto :EOF :doit set vbs="%~DP0CMDCTRLdo.vbs" cscript //nologo %vbs% %iFile% %oFile% endlocal goto :EOF :undoit set vbs="%~DP0CMDCTRLundo.vbs" cscript //nologo %vbs% %iFile% %oFile% endlocal

CMDCTRLdo.vbs contains:

                              dim fso, iFile, oFile,rFile, wFile                              dim iString, oString                              Dim objArguments                              Set objArguments = Wscript.Arguments                              iFile=objArguments(0)                              oFile=objArguments(1)                              set fso = CreateObject("Scripting.FileSystemObject")                              set rfile = fso.OpenTextFile(iFile, 1, false)                              set wFile = fso.CreateTextFile(oFile, 2)                              Do until rFile.AtEndOfStream = True                               iString = rFile.ReadLine                               oString = Replace(iString, "!", "¡")                               oString = Replace(oString, "%", "¥")                               oString = Replace(oString, "^", "Þ")                               oString = Replace(oString, "&", "¦")                               oString = Replace(oString, "|", "ü")                               oString = Replace(oString, "", "¾")                               oString = Replace(oString, 
", "¢") oString = Replace(oString, "=", "½") oString = Replace(oString, ";", "»") oString = Replace(oString, "(", "¨") oString = Replace(oString, ")", "©") oString = Replace(oString, ",", "¬") oString = Replace(oString, ":", "º") oString = Replace(oString, "'", "±") wFile.writeLine oString loop rFile.close wFile.close

CMDCTRLundo.vbs contains:

                              dim fso, iFile, oFile,rFile, wFile                              dim iString, oString                              Dim objArguments                              Set objArguments = Wscript.Arguments                              iFile=objArguments(0)                              oFile=objArguments(1)                              set fso = CreateObject("Scripting.FileSystemObject")                              set rfile = fso.OpenTextFile(iFile, 1, false)                              set wFile = fso.CreateTextFile(oFile, 2)                              Do until rFile.AtEndOfStream = True                               iString = rFile.ReadLine                               oString = Replace(iString, "¡", "!")                               oString = Replace(oString, "¥", "%")                               oString = Replace(oString, "Þ", "^")                               oString = Replace(oString, "¦", "&")                               oString = Replace(oString, "ü", "|")                               oString = Replace(oString, "¼", "")                               oString = Replace(oString, "¢", 
") oString = Replace(oString, "½", "=") oString = Replace(oString, "»", ";") oString = Replace(oString, "¨", "(") oString = Replace(oString, "©", ")") oString = Replace(oString, "¬", ",") oString = Replace(oString, "º", ":") oString = Replace(oString, "±", "'") wFile.writeLine oString loop rFile.close wFile.close