Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如何避免&&引用;通过批处理文件创建文件时_Batch File_Syntax Error - Fatal编程技术网

Batch file 如何避免&&引用;通过批处理文件创建文件时

Batch file 如何避免&&引用;通过批处理文件创建文件时,batch-file,syntax-error,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

我必须通过批处理文件重定向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
但是,上述情况会迫使您将文本中的任何
转义为
^)

或者,您可以重定向呼叫,而不必担心转义问题。)

但实际上有一种更好的方法可以消除逃跑的担忧。在每行前面加上
,使用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)

你能用插入符号(^)将符号(&)转义吗?你还应该转义
或者
),因为括号可以打开/关闭代码块…而不是在
echo
语句中。只要
echo
不在一个块内,转义是可选的;只要你有
(echo a(b) c)
,解析器怎么知道哪个
是结束的呢?所以你肯定需要
(echo a(b^)c)
;为了可读性,我更喜欢避开开头的一个(虽然不是强制性的),所以我会使用
(echo a^(b^)c)
…伙计,你的回答让我为我的回答感到羞愧。//:d
::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)