&QUOT ;.在这个时候出乎意料的是“从批处理脚本行生成'如果存在[文件](... [英] ". was unexpected at this time" generated from batch script line 'if exist [file] (...

查看:124
本文介绍了&QUOT ;.在这个时候出乎意料的是“从批处理脚本行生成'如果存在[文件](...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关代码如下所示:

cd /d %~dp0

if exist filename.txt (
    echo %date% %time% *** text... >> filename2.txt
    echo ==============================
    echo ===  text.......           ===
    echo ===  text.......           ===
    echo ===  text....... (text...) ===
    echo ===  text (text... 
    echo ===  text...).
    :loop
        set /p "varx= text: "
    if "%varx%" neq "xxxx" goto loop

 ... more script...

)

已搜索高低解决方案...

Have searched high and low for solutions...

  • was pointed in direction of If statement groupings here: https://www.petri.com/forums/forum/windows-scripting/general-scripting/57625-if-exists-file-was-unexpected-at-this-time - NO GO

指向If块中的循环问题的方向,这里:
(Windows批处理)转到if块表现得非常奇怪 - 没有GO

was pointed in direction of problems with loops in If blocks, here: (Windows batch) Goto within if block behaves very strangely - NO GO

指向使用@setlocal enabledelayedexpansion或@setlocal enableextensions的方向(无法追踪到哪里) - 没有GO

Was pointed in direction of using @setlocal enabledelayedexpansion or @setlocal enableextensions (can't track down where) - NO GO

尝试通过set / p varfile =filename传递文件名,如果存在%varfile% - 没有GO

tried passing filename via set /p varfile="filename" and if exist %varfile% - NO GO

当然认为还有其他部分代码导致错误 - 没有GO

of course thought there were other parts of code causing error -- NO GO

事情是这个WAS工作了很长时间......然后我改变了我认为的一些无害的东西,无法弄清问题在哪里......

The thing is that this WAS working for a long while...then I changed what I thought was some innocuous stuff and cannot figure out where the problem lies...

这样一个难以理解的问题要解决......好的!

such an obscure problem to solve..ugh!

推荐答案

来自整个复合语句,如果e xist ...(到右括号是一个代码块

The entire compound-statement from the if exist ... ( through to the closing parenthesis is a code block.

代码块中,不允许使用标签,未转义的将终止块和任何<$ c当遇到阻止时,$ c>%var%被替换为该变量的值

Within a code block, labels are not allowed, an un-escaped ) will terminate the block and any %var% is replaced by that variable's value when the block is encountered.

您需要致电 :循环例程(最简单的方法)

You need to call the :loop routine (easiest way)

if exist filename.txt (
    echo %date% %time% *** text... >> filename2.txt
    echo ==============================
    echo ===  text.......           ===
    echo ===  text.......           ===
    echo ===  text....... (text...^) ===
    echo ===  text (text... 
    echo ===  text...^).
    call :loop

 ... more script...

)

...等

转到:eof

:循环
set / pvarx = text:
if%varx%neqxxxxgoto loop

:loop set /p "varx= text: " if "%varx%" neq "xxxx" goto loop

goto:eof

请注意 goto:eof 已定义为转到文件结尾 (冒号必需

Note the goto :eof is defined to be "go to end-of-file" (The colon is required)

注意调用在标签前有一个冒号 - 这表明它是一个内部例程 - 它驻留在这个批处理文件中

Note the call has a colon before the label - this indicates it's an internal routine - it resides in this batchfile

注意每个现在通过 ^ 进行转义,这使得普通字符,而不是语句终结符。

Note that each ) is now escaped by a ^ which makes ) an ordinary character, not a statement-terminator.

你可能已经删除了一个重定向器 - 调用中的任何 echo ed程序将被收集到重定向输出中 - 即如果你有

You've probably removed a redirector - any echo within the called procedure will be gathered into the redirected output - that is if you have

(
whatever...
)>filename

然后代码块中的任何数据 echo 将被重定向到文件 - 调用 ed过程中包含 echo es。您可以使用 echo to console> con 在这样的块或过程中显式重定向 echo

then any data echoed within the code block will be redirected to the file - including echoes within a called procedure. You can explicitly redirect an echo within such a block or procedure using echo to console>con for instance.

在括号中包含任何行序列,使其成为代码块允许重定向 echo 来自块的ed输出,所以

Encasing any sequence of lines in parentheses, making it a code block allows redirection of the echoed output from the block, so

(
 echo this
 echo that
)>filename

创建一个新的文件名包含 echo es的总和,而不是必须在每行附加>> filename 。显然,>> 代替> 追加添加到任何现有文件以通常的方式。

creates a new filename containing the sum of the echoes rather than having to append >>filename to each line. Obviously, >> in place of > will append to any existing file in the usual manner.

这篇关于&QUOT ;.在这个时候出乎意料的是“从批处理脚本行生成'如果存在[文件](...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆