Looking for a function or macro that can safely write all strings to a text file - without having to manually escape characters. For example:
Code: Select all
set "x=1% 2% 3% 4^% 5% ) == & =. a! b! < c! d^! >>e! ^ ^ <<'n1 = n +2 -( )))>>"
ECHO %x%>file.txt
I'm just wondering if this is possible. Currently, if I have a string that may cause problems I use vbscript (script below) and call it like this (but this can be slow to invoke the vbscript engine for a large number of lines):
Code: Select all
CScript //nologo "vbsWriteLine" x
Code: Select all
qt=chr(34):pct=chr(37)
Set wshShell = WScript.CreateObject("WScript.Shell")
'WScript.Echo WScript.Arguments(0)
'WScript.Echo WScript.Arguments(1)
ArgCntOrig=WScript.Arguments.Count
IF ArgCntOrig=0 THEN CALL EndScript
str=""
'DIM ArgsInUse
'REDIM ArgsInUse(ArgCntOrig-1)
FullCmdLine=WScript.ScriptFullName
FOR x=1 to ArgCntOrig -1
'ArgsInUse(x)=WScript.Arguments(x)
'FullCmdLine=FullCmdLine & " " & WScript.Arguments(x)
str=str & wshShell.ExpandEnvironmentStrings(pct & WScript.Arguments(x) & pct)
NEXT
'str=wshShell.ExpandEnvironmentStrings(pct & WScript.Arguments(0) & pct)
myTxtFile=WScript.Arguments(0)
x=0
' TRIM TABS - SPACES and QUOTES from File Path-Name
DO WHILE x=0
x=1
myTxtFile=TRIM(myTxtFile)
IF LEFT(myTxtFile,1)=vbTAB THEN
myTxtFile=RIGHT(myTxtFile, LEN(myTxtFile)-1)
x=0
END IF
IF RIGHT(myTxtFile,1)=vbTAB THEN
myTxtFile=LEFT(myTxtFile, LEN(myTxtFile)-1)
x=0
END IF
IF LEFT(myTxtFile,1)=qt THEN
myTxtFile=LEFT(myTxtFile, LEN(myTxtFile)-1)
x=0
END IF
IF RIGHT(myTxtFile,1)=qt THEN
myTxtFile=LEFT(myTxtFile, LEN(myTxtFile)-1)
x=0
END IF
LOOP
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile(myTxtFile, 8, True)
oFile.WriteLine(str)
SUB EndScript
On Error Resume Next
oFile.Close
On Error Goto 0
Set FSO= Nothing
Set wshShell = Nothing
END Sub