简体   繁体   English

批处理脚本 - FOR-Loop 后不会继续

[英]Batch-Script - Won't proceed after FOR-Loop

I got a Startup-Script which does close and open some services and in the end open a process on the desktop.我得到了一个启动脚本,它确实关闭并打开了一些服务,最后在桌面上打开了一个进程。 The name of the desktop icon could be variable, so we used a FOR-Loop there.桌面图标的名称可以是可变的,因此我们在那里使用了 FOR-Loop。 Anyhow since we added the FOR-Loop it wont close the script.无论如何,自从我们添加了 FOR-Loop 它不会关闭脚本。 It looks like its hanging in the for Loop, but when i try the for loop in a testcript alone it will close the script after but not in my original script..它看起来像是挂在 for 循环中,但是当我单独在测试脚本中尝试 for 循环时,它会在之后关闭脚本,但不会在我的原始脚本中关闭。

tasklist |find /i "pxTel.exe" >nul
if %errorlevel% == 1 (
echo [32mStarte Programm Telefon Integration... Alles in Ordnung![0m
FOR /f "tokens=*" %%G IN ('dir /b %HOMEPATH%\Desktop\pxTel*') DO %HOMEPATH%\Desktop\%%G
timeout 3
) else (
echo [32mProgramm Telefon-Integration laeuft bereits... ueberspringe..[0m
)

This can be happening due to a large number of reasons.由于多种原因,可能会发生这种情况。

It is actually happening to me even: the command dir /b %HOMEPATH%\Desktop\* does not seem to return on my machine right now.它实际上甚至发生在我身上:命令dir /b %HOMEPATH%\Desktop\*现在似乎没有在我的机器上返回。 That's because my %HOMEPATH% variable is set to \ , so the command expands to dir /b \\Desktop\* , and there is no machine called Desktop , so it appears to hang while timing out.那是因为我的%HOMEPATH%变量设置为\ ,所以命令扩展为dir /b \\Desktop\* ,并且没有名为Desktop的机器,所以它似乎在超时时挂起。

You might ask, how come my %HOMEPATH% is set to \ ?您可能会问,我的%HOMEPATH%怎么设置为\ Well, that's because my %HOMEDRIVE% is set to Y: , a network share, so %HOMEDRIVE%%HOMEPATH% expands to Y:\ which is the intention.好吧,那是因为我的%HOMEDRIVE%设置为Y: ,一个网络共享,所以%HOMEDRIVE%%HOMEPATH%扩展为Y:\这就是意图。

So, what do we learn from this?那么,我们从中学到什么?

Either always use %HOMEPATH% together with %HOMEDRIVE% , or just use %USERPROFILE% instead.要么始终将%HOMEPATH%%HOMEDRIVE%一起使用,要么只使用%USERPROFILE%

Also, always enclose paths in double quotes, otherwise the spaces will wreak havoc.此外,始终用双引号将路径括起来,否则空格会造成严重破坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM