如何避免"&"通过批处理文件

时间:2015-12-27 10:56:26

标签: batch-file syntax-error

我必须通过批处理文件重定向vbs文件。

我的代码就像:

@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

执行包含"&"的字符串时批处理文件出现问题。在里面。 每次用"&"。

打破重定向

错误是:

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.

是否有任何方法可以使用"&"重定向字符串?在批处理文件中?

2 个答案:

答案 0 :(得分:3)

使用&转义^(以及其他有问题的字符),所以

echo     Do While InStr(Content, vbCrLf ^& vbCrLf)>>weather.vbs

答案 1 :(得分:3)

哎哟。

GolezTrol直接回答了您的问题 - 将&转义为^&

但这是一个有用的提示,虽然它实际上没有回答你的问题 - 当将一组命令重定向到同一个文件时,重定向一次会更有效(也更容易阅读)。

(
  echo line 1
  echo line 2
  echo line 3
)>output.txt

但是,上述内容会强制您在文本中将)转换为^)

或者,您可以重定向CALL,而不必担心转义)

call :write >output.txt
exit /b
:write
echo line 1
echo line 2
echo line 3
exit /b

但实际上有一种更好的方法可以消除担心转义的麻烦。使用:::为每行添加前缀,并使用FINDSTR查找行,使用FOR / F删除前缀。重要的是,所有文本行都不以:开头。

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

echo(是一种模糊的语法,能够打印空的行或只包含空格的行。它比echo.更可靠看起来它会弄乱括号块,但事实并非如此。你的文字中没有空行,所以这里不是问题,但这是一个很好的诀窍。

::充当评论,因此不会执行这些行。

有两种情况,上述技术可能会因致命的语法错误而失败:

  • 您的文字包含%~,其被解析为无效的参数扩展。
  • 您的文本包含%var:=,其中var是环境变量的名称。该表达式被解析为无效的变量扩展查找/替换操作。

如果您遇到上述任何一个问题,那么您只需将文本放在脚本的底部,然后使用EXIT / B。

如果您的某些行以:开头,那么您只需将前缀修改为以从不开始一行的字符结束,然后稍微更改FOR / F.例如,假设没有行以}

开头
::}Line 1
::}Line 2
::}Line 3
>output.txt (for /f "delims=} tokens=1*" %%A in ('findstr "^::}" "%~f0"') do echo(%%B)

另一个小修改允许嵌入多个独立的文本块

::A}Line 1
::A}Line 2
::A}Line 3

::B}Line A
::B}Line B
::B}Line C

>a.txt (for /f "delims=} tokens=1*" %%A in ('findstr "^::A}" "%~f0"') do echo(%%B)
>b.txt (for /f "delims=} tokens=1*" %%A in ('findstr "^::B}" "%~f0"') do echo(%%B)