通过批处理文件创建文件时如何避免“&”

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

我必须通过批处理文件重定向 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.

有没有办法在批处理文件中也使用“&”重定向字符串?

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

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

哎呀

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 删除前缀。重要的是 none 您想要的文本行以 :.

开头
@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 是环境变量的名称。表达式被解析为无效的变量扩展 find/replace 操作。

如果您 运行 遇到上述任一问题,那么您只需将文本放在脚本的底部,并在其前面加上 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)