Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid "&" when creating file through batch file

I have to redirect a vbs file through batch-file.

My code is like:

@echo off
echo Option Explicit>> weather.vbs
echo Dim LogFile,Ws,Tag,Content>> weather.vbs
echo LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, .)) & txt >> weather.vbs
echo Set Ws = CreateObject(wscript.Shell) >> weather.vbs
echo With CreateObject(InternetExplorer.Application) >> weather.vbs
echo     .Visible = False>> weather.vbs
echo    .Navigate http://aldweathersh2.blogspot.in/ >> weather.vbs
echo    Do Until .ReadyState = 4 >> weather.vbs
echo         Wscript.Sleep 6000>> weather.vbs
echo     Loop>> weather.vbs
echo     For Each Tag In .Document.GetElementsByTagName(script) >> weather.vbs
echo         Tag.OuterHtml =  >> weather.vbs
echo     Next>> weather.vbs
echo     For Each Tag In .Document.GetElementsByTagName(noscript) >> weather.vbs
echo         Tag.OuterHtml =  >> weather.vbs
echo     Next>> weather.vbs
echo     Content = .Document.GetElementsByTagName(body)(0).InnerText >> weather.vbs
echo     Do While InStr(Content, vbCrLf & vbCrLf)>>weather.vbs
echo         Content = Replace(Content, vbCrLf & vbCrLf, vbCrLf) >> weather.vbs
echo     Loop >> weather.vbs
echo     WriteLog Content,LogFile >> weather.vbs
echo    .Quit>> weather.vbs
echo End With>> weather.vbs
echo '******************************************************************* >> weather.vbs
echo Sub WriteLog(strText,LogFile) >> weather.vbs
echo    Dim fso,ts>> weather.vbs
echo     Const ForWriting = 2 >> weather.vbs
echo    Set fso = CreateObject(Scripting.FileSystemObject) >> weather.vbs
echo     Set ts = fso.OpenTextFile (LogFile,ForWriting,True,-1) >> weather.vbs
echo     ts.WriteLine strText>> weather.vbs
echo     ts.Close>> weather.vbs
echo End Sub>> weather.vbs
echo '******************************************************************>> weather.vbs
pause

Problem occurs with the batch-file while executing strings containing "&" in it. Every time it breaks the redirection with "&".

Error is:

LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, .))
'txt' is not recognized as an internal or external command,
operable program or batch file.
    Do While InStr(Content, vbCrLf
'vbCrLf)' is not recognized as an internal or external command,
operable program or batch file.
        Content = Replace(Content, vbCrLf
'vbCrLf' is not recognized as an internal or external command,
operable program or batch file.

Is there any way to also redirect strings with "&" in batch-file ?

like image 703
Ekagra Sinha Avatar asked Feb 09 '23 05:02

Ekagra Sinha


1 Answers

Escape the & (and other problematic characters) with ^, so

echo     Do While InStr(Content, vbCrLf ^& vbCrLf)>>weather.vbs
like image 140
GolezTrol Avatar answered Apr 25 '23 03:04

GolezTrol