|
Answer» I know little about batch scripting so I am in NEED of some help. This script is a compilation of research and then left to gather dust. I really want it to work but it SEEMS like it skips some of the commands. My goal with the script is to do each section 1 at a time and then wait for an exe to close and then close different things 1 at a time.
I play a game called Minecraft. What I *want* the script to do is 1) close server.exe if it is running, 2) wait for exe to close and download server.exe again to make sure I have the latest greatest build, 3) wait until download is done then start a windows service that opens a VPN program I have, 4) start the server.exe i downloaded, 5) pause script until the server.exe is closed by me, 6) stop the service that was started earlier and THE END!
Code: [Select]echo off
:PREP :: to make sure the server is dead for /f "usebackq" %%A in (`tasklist /fi "windowtitle eq Minecraft server"`) do if %%A==INFO: goto DOWNLOAD taskkill /f /fi "windowtitle eq Minecraft server" & GOTO DOWNLOAD PING -n 1 -w 2000 1.1.1.1 > NUL GOTO PREP
:DOWNLOAD :: to download an updated server EXE URL2File -d https://s3.amazonaws.com/MinecraftDownload/launcher/Minecraft_Server.exe Minecraft_Server.exe
:LOOP1 :: to make sure download is complete for /f "usebackq" %%B in (`tasklist /fi "imagename eq URL2FILE.exe"`) do if %%B==INFO: goto CONTINUE GOTO LOOP1
:CONTINUE :: to start Hamachi if it isn't yet for /F "tokens=3 delims=: " %%C in ('sc query Hamachi2Svc ^| findstr " STATE"') do ( if /I "%%C" NEQ "RUNNING" ( sc start Hamachi2Svc ) ) GOTO STARTSERVER
:STARTSERVER :: start the newly downloaded server EXE start "" "C:\Program FILES (x86)\Minecraft\Minecraft_Server.exe"
:LOOP2 :: wait for server EXE to close before clean-up for /f "usebackq" %%D in (`tasklist /fi "windowtitle eq Minecraft server"`) do if %%D==INFO: goto DONE PING -n 1 -w 6000 1.1.1.1 > NUL GOTO :LOOP2
:DONE :: close out Hamachi sc stop Hamachi2Svc Oh I forgot to mention that I am not entirely sure how to write the FOR commands that are in my script. What I have done is attempt to nest IF commands inside every FOR command. Is this allowed? If so why doesn't my script work completely? If not what alternatives do I have?
My script *seems* to get all the way to the LOOP2 section and then just quits. Also it doesn't seem like it does any waiting.At a first glance, one thing I can see that MIGHT possibly make a script bomb is unquoted strings either side of an IF test. If %%D should ever evaluate to nothing (e.g. because the output of tasklist includes a blank line) then you will get a test like this
if ==INFO:
so try quotes like this (this is a good habit to get into)
if "%%D"=="INFO:" goto DONE
because
if ""=="INFO:"
is a valid test.
the section I am referring to:
:LOOP2 :: wait for server EXE to close before clean-up for /f "usebackq" %%D in (`tasklist /fi "windowtitle eq Minecraft server"`) do if %%D==INFO: goto DONE PING -n 1 -w 6000 1.1.1.1 > NUL GOTO :LOOP2
:DONE
YOu could comment out the echo off line (REM echo off) and then you can see the script's progress. That might give some information.
By the way, I see you use the unofficial, unsupported double colon comment line starter ("::"). Beware because if you use this inside a loop, it will break it.
|