udfStrDosEscape
str udfStrDosEscape (str, int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrDosEscape (strText, intFlag)
If strText == "" Then Return ""
blnQL = '"' == StrSub (strText, 1, 1)
blnQR = '"' == StrSub (strText, StrLen (strText), 1)
blnSpace = !!StrIndex (strText, " ", 1, @FWDSCAN)
Switch @TRUE
Case 0 == intFlag
   intFlag = 2
   If !blnSpace Then intFlag = 1
   Continue
Case 4 == (intFlag & 4)
   strSpecialChars = StrClean ('^&|<>()"+\', strText, '', @TRUE, 2)
   intCount = StrByteCount (strSpecialChars, -1)
   For intI = 1 To intCount
      strChar = StrSub (strSpecialChars, intI, 1)
      strText = StrReplace (strText, strChar, "^" : strChar)
   Next
   Break
   ;..................
   ; Alternative code.
   ; Case 4 == (intFlag & 4)
   ;    arrCharsToEscape = ObjectType ('ARRAY', ArrayFromStr (StrClean ('^&|<>()"+\', strText, '', @TRUE, 2)))
   ;    If !!ArrInfo (arrCharsToEscape, 1)
   ;       ForEach strChar In arrCharsToEscape
   ;          strText = StrReplace (strText, strChar, "^" : strChar)
   ;       Next
   ;       strText = ChrUnicodeToString (strText) ; For sure, cast data type from unicode string back to normal string.
   ;    EndIf
   ;    Break
   ;..................
Case 2 == (intFlag & 2)
   Switch @TRUE
   Case !blnQL
      strText = '"' : strText
      Continue
   Case !blnQR
      strText = strText : '"'
   EndSwitch
   Break
Case 1 == (intFlag & 1)
   Switch @TRUE
   Case blnQL
      strText = StrSub (strText, 2, -1)
      Continue
   Case blnQR
      strText = StrSub (strText, 1, StrLen (strText) - 1)
   EndSwitch
   Break
EndSwitch
Return strText
;..........................................................................................................................................
; This UDF "udfStrDosEscape" takes a text string and prepares it for usage on the DOS commandline.
;
; intFlag = 4 ... Escape ... Certain characters will be escaped by a prepended caret sign to change their commandline meaning to their literal value.
; intFlag = 2 ... Quote .... String will be enclosed in quote characters (double apostrophe), if there are no quote characters already exist.
; intFlag = 1 ... UnQuote .. Enclosing quote characters (double apostrophe) will be removed from string.
; intFlag = 0 ... AutoQuote  For string with embedded space character, same as intFlag = 2.
;                            For string without space character, same as intFlag = 1.
;
; (c)Detlev Dalitz.20110401.20111222.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strText11 = '11 abc.txt'
strText12 = '"12 abc.txt'
strText13 = '13 abc.txt"'
strText14 = '"14 abc.txt"'

strText21 = '21abc.txt'
strText22 = '"22abc.txt'
strText23 = '23abc.txt"'
strText24 = '"24abc.txt"'

intFlag = 0 ; Auto quote.

strOut11 = udfStrDosEscape (strText11, intFlag)  ;  "11 abc.txt"
strOut12 = udfStrDosEscape (strText12, intFlag)  ;  "12 abc.txt"
strOut13 = udfStrDosEscape (strText13, intFlag)  ;  "13 abc.txt"
strOut14 = udfStrDosEscape (strText14, intFlag)  ;  "14 abc.txt"

strOut21 = udfStrDosEscape (strText21, intFlag)  ;  21abc.txt
strOut22 = udfStrDosEscape (strText22, intFlag)  ;  22abc.txt
strOut23 = udfStrDosEscape (strText23, intFlag)  ;  23abc.txt
strOut24 = udfStrDosEscape (strText24, intFlag)  ;  24abc.txt


intFlag = 1 ; No quote.

strOut11 = udfStrDosEscape (strText11, intFlag)  ;  11 abc.txt
strOut12 = udfStrDosEscape (strText12, intFlag)  ;  12 abc.txt
strOut13 = udfStrDosEscape (strText13, intFlag)  ;  13 abc.txt
strOut14 = udfStrDosEscape (strText14, intFlag)  ;  14 abc.txt

strOut21 = udfStrDosEscape (strText21, intFlag)  ;  21abc.txt
strOut22 = udfStrDosEscape (strText22, intFlag)  ;  22abc.txt
strOut23 = udfStrDosEscape (strText23, intFlag)  ;  23abc.txt
strOut24 = udfStrDosEscape (strText24, intFlag)  ;  24abc.txt


intFlag = 2 ; Always quote.

strOut11 = udfStrDosEscape (strText11, intFlag)  ;  "11 abc.txt"
strOut12 = udfStrDosEscape (strText12, intFlag)  ;  "12 abc.txt"
strOut13 = udfStrDosEscape (strText13, intFlag)  ;  "13 abc.txt"
strOut14 = udfStrDosEscape (strText14, intFlag)  ;  "14 abc.txt"

strOut21 = udfStrDosEscape (strText21, intFlag)  ;  "21abc.txt"
strOut22 = udfStrDosEscape (strText22, intFlag)  ;  "22abc.txt"
strOut23 = udfStrDosEscape (strText23, intFlag)  ;  "23abc.txt"
strOut24 = udfStrDosEscape (strText24, intFlag)  ;  "24abc.txt"

DropWild ("strText*")
DropWild ("strOut*")


strText31 = 'Caret ..............: ^ '
strText32 = 'Ampersand ..........: & '
strText33 = 'Pipe ...............: | '
strText34 = 'Angle Bracket Left  : < '
strText35 = 'Angle Bracket Right : > '
strText36 = 'Round Bracket Left  : ( '
strText37 = 'Round Bracket Right : ) '
strText38 = 'Plus ...............: + '
strText39 = 'Backslash ..........: \ '
strText40 = '....................: (^^&) '

intFlag = 4

strOut31 = udfStrDosEscape (strText31, intFlag)  ; "Caret .............: ^^ "
strOut32 = udfStrDosEscape (strText32, intFlag)  ; "Ampersand .........: ^& "
strOut33 = udfStrDosEscape (strText33, intFlag)  ; "Pipe ..............: ^| "
strOut34 = udfStrDosEscape (strText34, intFlag)  ; "Angle Bracket Left : ^< "
strOut35 = udfStrDosEscape (strText35, intFlag)  ; "Angle Bracket Right: ^> "
strOut36 = udfStrDosEscape (strText36, intFlag)  ; "Round Bracket Left : ^( "
strOut37 = udfStrDosEscape (strText37, intFlag)  ; "Round Bracket Right: ^) "
strOut38 = udfStrDosEscape (strText38, intFlag)  ; "Plus ..............: ^+ "
strOut39 = udfStrDosEscape (strText39, intFlag)  ; "Backslash .........: ^\ "
strOut40 = udfStrDosEscape (strText40, intFlag)  ; "...................: ^(^^^^^&^) "


strCmdExe = Environment ("COMSPEC")
strCmdPar = '/K'
strCmdPar = strCmdPar : ' ECHO.'
strCmdPar = strCmdPar : ' & ECHO.'
strCmdPar = strCmdPar : udfStrDosEscape ('Example string: <angel brackets> & (round brackets) | ^ + "quoted" \', 4)
strCmdPar = strCmdPar : ' & ECHO.'
strCmdPar = strCmdPar : ' & PAUSE'
strCmdPar = strCmdPar : ' & EXIT'
blnresult = RunWait (strCmdExe, strCmdPar)

strCmdExe = Environment ("COMSPEC")
strCmdPar = '/K'
strCmdPar = strCmdPar : ' ECHO.'
strCmdPar = strCmdPar : ' & ECHO.'
strCmdPar = strCmdPar : udfStrDosEscape (udfStrDosEscape ('Example string: <angel brackets> & (round brackets) | ^ + "quoted" \', 2), 4)
strCmdPar = strCmdPar : ' & ECHO.'
strCmdPar = strCmdPar : ' & PAUSE'
strCmdPar = strCmdPar : ' & EXIT'
blnresult = RunWait (strCmdExe, strCmdPar)

:CANCEL
Exit