Alias
Alias

Reputation: 391

Batch file fails on startup, but works when I click it

I have a small problem with my batch script. The idea is simple: I have an app that checks for updates periodically (checks for one exe file). If it finds one it then downloads it, renames it and puts it in the same directory as the old file. Then the app creates a .bat file and also puts it in the same directory. The contents of .bat file are :

@ECHO OFF
ECHO Waiting while old application closes...
ping 127.0.0.1 -n 5
taskkill /IM myApp.exe /f
ECHO Updating application
move /y myApp_TEMP.exe myApp.exe
START myApp.exe
pause 

Before I close my application (myApp.exe) I instruct it to execute the .bat file, ant wait 5 sec. so it will properly close, that’s why the ping 127.0.0.1 -n 5 is here for. So ideally this script should:

Close myApp.exe
Rename myApp_TEMP.exe to myApp.exe
Overwrite old myApp.exe with new myApp.exe
Start myApp.exe

And it works when I double click on the .bat file, the problem occurs when I put myApp.exe in Windows startup list.
So the app start's, downloads update, generates the .bat file (keep in mind that everything is happening in the same directory) and then runs it. After some digging i found out that the line
move /y myApp_TEMP.exe myApp.exe is not executing. But when I run this script manually everything works. Maybe someone has already experienced similar issues?

Upvotes: 0

Views: 527

Answers (3)

Jake Jensen
Jake Jensen

Reputation: 61

Might need to add some redundancy to the script. Such as:

@ECHO OFF
ECHO Waiting while old application closes...
taskkill /IM myApp.exe /f
timeout 5 /nobreak >nul
ECHO Updating application
IF EXIST "%cd%\MyApp_TEMP.exe" move /y myApp_TEMP.exe myApp.exe
IF EXIST "%cd%\MyApp.exe" GOTO STARTAPP
ECHO File not found. Unable to update.
pause
exit
:STARTAPP
START myApp.exe
pause 

The problem seems to stem from a problem in directory searching. While this could be solved with a rather long search time, it'd be easier to make sure that the batch is in the same directory as "MyApp_TEMP.exe" or using

cd <path to MyApp_TEMP.exe>
ECHO Updating application
move /y myApp_TEMP.exe myApp.exe
START myApp.exe
pause

You could also use an ELSE if the file is not found, I just Personally prefer to use label jumping with GOTO.

Upvotes: 0

Dennis van Gils
Dennis van Gils

Reputation: 3452

I would suggest using a CD /D <directory of the .exe file> before the move, that way you always know for sure you're in the right directory. Also, you can use timeout 5 /NOBREAK to wait for 5 seconds in regular batch-files

Upvotes: 1

Tim B
Tim B

Reputation: 41188

You are using relative paths in your filenames. Check that it's being run with the correct current directory or switch to absolute paths.

Upvotes: 1

Related Questions