赢批-如果文件名搜索中存在%date%-不起作用 [英] Win Batch - IF EXIST %date% within filename search - not working

查看:84
本文介绍了赢批-如果文件名搜索中存在%date%-不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无法运行的小脚本:

I've this little script that is not working:

set actdate=%date:~6,4%%date:~3,2%%date:~0,2%
set source="\\somewhere\APPL\Logs"
set target="C:\TEMP\nebu_%actdate%.log"
echo %actdate%
if exist "%source%\*%actdate%*.txt" (
    echo vorhanden
    echo %source%\*%actdate%*.txt
) else (
    echo Nicht vorhanden
)

运行脚本时,我得到以下输出:

When I ran the script I get following output:

20171018
vorhanden
"\\somewhere\APPL\Logs"\*20171018*.txt
Nicht vorhanden

现在的问题是,源中实际上没有包含*20171018*.txt的文件:

Now the thing is that there is actually no file with *20171018*.txt in source:

NeBuExport.20171017.102600.txt
NeBuExport.20171016.080552.txt
NeBuExport.20171013.093638.txt

这是什么问题?

推荐答案

Squashman用评论打在了头上-您的意外行为是作业中引号的结果,以及源路径中的空格.实际上,如果您的路径包含任何令牌定界符(例如=,;<space>

Squashman hit the nail on the head with his comment - Your unexpected behavior is the result of quotes in your assignment, coupled with space(s) within your source path. Actually, the problem could arise if your path contains any token delimiter like =, ,, ;, or <space>

假设您的源分配是set source="\\some path\APPL\Logs". source的值是"\\some path\APPL\Logs",包括引号.

Suppose your source assignment is set source="\\some path\APPL\Logs". The value of source is "\\some path\APPL\Logs", including the quotes.

然后,您的IF语句扩展为:

Then your IF statement expands to:

if exist ""\\some path\APPL\Logs"\*20171018*.txt" (

由于双引号引起来,所以不加空格,因此原本打算作为单个令牌的将被视为两个令牌.

Because of the doubled quotes, the space is not quoted, so what is intended to be a single token is treated as two tokens.

IF测试""\\some是否存在,如果存在,则尝试执行以下伪指令:

IF tests if ""\\some exists, and if it does, then it tries to execute the following bogus command:

path\APPL\Logs"\*20171018*.txt"

(被视为伪指令的参数,而不是代码块的开头.

The ( is treated as an argument to the bogus command, not as the beginning of a code block.

当然""\\some不存在,因此不会执行伪指令,并且不会收到任何错误消息.

Of course ""\\some does not exist, so the bogus command is not executed and you don't receive any error message.

现在应该很清楚为什么所有三个ECHO语句都执行.

It should now be obvious why all three ECHO statements execute.

最后一个谜"是为什么) else ()语句不产生任何错误的原因.这是由于以下事实的结果:如果没有任何活动的括号代码块要关闭,则)有效地充当REM.就像备注一样,)之后的所有文本都将被忽略.

The last "mystery" is why the ) else ( and ) statements do not produce any error. This is a result of the fact that ) effectively functions as a REM if there is not any active parenthetical code block to close. All text after the ) is ignored, just like a remark.

如果您按照Squashman的建议巧妙地更改源的定义,您的代码应按预期运行:

Your code should operate as intended if you subtly change the definition of your source as Squashman suggested:

set "source=\\some path\APPL\Logs"

现在,您的值变为\\some path\APPL\Logs,不带引号,并且一切正常:-)

Now your value becomes \\some path\APPL\Logs, without quotes, and everything works :-)

这篇关于赢批-如果文件名搜索中存在%date%-不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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