首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在通过批处理文件创建文件时如何避免"&“

问在通过批处理文件创建文件时如何避免"&“
EN

Stack Overflow用户
提问于 2015-12-27 10:56:26
回答 2查看 117关注 0票数 4

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

我的代码是:

代码语言:javascript
复制
@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

在执行包含"&“的字符串时,批处理文件会出现问题。每次它用"&“来打破重定向。

错误是:

代码语言:javascript
复制
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.

有没有方法也可以重定向批文件中的"&“字符串?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-27 11:00:58

使用& (和其他有问题的字符)转义^,因此

代码语言:javascript
复制
echo     Do While InStr(Content, vbCrLf ^& vbCrLf)>>weather.vbs
票数 4
EN

Stack Overflow用户

发布于 2015-12-27 16:22:26

唉哟。

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

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

代码语言:javascript
复制
(
  echo line 1
  echo line 2
  echo line 3
)>output.txt

但是,以上所述将迫使您将文本中的任何)作为^)转义。

或者,您可以重定向一个电话,而不必担心转义)。

代码语言:javascript
复制
call :write >output.txt
exit /b
:write
echo line 1
echo line 2
echo line 3
exit /b

但实际上,有一种更好的方法可以消除对逃跑的担忧。用:::前缀每一行,并使用FINDSTR查找行,/F删除前缀。重要的是,您想要的文本行都不要以:开头。

代码语言:javascript
复制
@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是环境变量的名称。表达式被解析为无效的变量展开查找/替换操作。

如果您遇到上述任何一个问题,那么您可以简单地将文本放在脚本的底部,并在其前面加上一个退出/B。

如果您的一些行以:开头,那么您只需修改前缀,使其以一个从不以行开头的字符结尾,并稍微更改一下FOR /F。例如,假设没有一行以}开头

代码语言:javascript
复制
::}Line 1
::}Line 2
::}Line 3
>output.txt (for /f "delims=} tokens=1*" %%A in ('findstr "^::}" "%~f0"') do echo(%%B)

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

代码语言:javascript
复制
::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)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34479719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档