简体   繁体   中英

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. Anyhow since we added the FOR-Loop it wont close the script. 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..

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. 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.

You might ask, how come my %HOMEPATH% is set to \ ? Well, that's because my %HOMEDRIVE% is set to Y: , a network share, so %HOMEDRIVE%%HOMEPATH% expands to Y:\ which is the intention.

So, what do we learn from this?

Either always use %HOMEPATH% together with %HOMEDRIVE% , or just use %USERPROFILE% instead.

Also, always enclose paths in double quotes, otherwise the spaces will wreak havoc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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