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

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?

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

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

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.

Related

Program started from batch file starts in background

I have a batch file that I run from a flash drive. This file attempts to
1. Close 2 programs
2. Update data on the computer based on the data on the flash drive
3. Restart the programs
The code in this batch is:
taskkill /IM "MyProgram".exe
taskkill /f /fi "imagename eq MyProgram.exe"
copy e:\File1.xml C:\Folder\SubFolder1\Themes\Data\File1.xml /Y
copy e:\File2.xml C:\Folder\SubFolder1\Themes\Data\File2.xml /Y
copy e:\File3.xml C:\Folder\SubFolder2\Themes\Data\File3.xml /Y
copy e:\File4.xml C:\Folder\SubFolder2\Themes\Data\File4.xml /Y
start /MAX "" "C:\Folder\SubFolder2\MyProgram.exe"
start /MAX "" "C:\Folder\SubFolder1\MyProgram.exe"
The first two steps work fine, with the commands that are on lines 1-6. My issue starts with step 3 and the commands on lines 7 and 8.
Is there something wrong with the batch commands?
EDIT: After digging a little more, I have found that the programs appear to be starting in the background. When looking at task manager, the programs appear in the "Background Processes" section instead of showing in the foreground as expected.
What I believe is happening is your program does not know where the configuration files are located because the working directory is where the batch file started itself. So your program is looking for its files on your usb drive. By using the /D option with the START command it will switch the working directory to whatever path you set it to.
start "" /MAX /D "C:\Folder\SubFolder2\" MyProgram.exe

CMD doesn't close after I run a batch file and files don't copy after deleting them once?

I made a batch file to copy my Witcher 3 save files and settings to another drive. It runs every time I start Windows and it runs again after 12 hours.
Problems are that it opens CMD and doesn't close it. I wan't it to close automatically, it doesn't matter if it shows deleting and copying the files I would actually prefer it showing those, just close automatically after those and don't show the seconds its counting for 12 hours.
Also after deleting the files from the backup location, it doesn't copy the files there anymore until I restart my PC.
I made the batch file to delete the files because it would just copy the new files and leave the old ones in and it would be a mess, so I'm not sure if I could do this only with the copy command and without the del command?
The batch file looks like this:
:loop
del /s /q "F:\Backups\The Witcher 3 save files"
xcopy /m /e /y "C:\Users\Kristian\Documents\The Witcher 3" "F:\Backups\The Witcher 3 save files"
timeout /t 43200
goto loop
If you want a batch file to run every 12 hours, you should use the Windows Task Scheduler. Then your script doesn't have to run 24/7.

My batch file is closing a program too early

Thank you, for those whom took the time to read my question. I am a gamer and would like to execute a few things. First, I would like to Trigger a batch file when I click a program, how do you do that or is it even possible? Basically, activating a game, triggers the batch file.
NOw for the batch file problem, I want to execute Xpadder when I activate games (this is an mmo) and when I close the game I want Xpadder's process/service to close. Ultimately, it's auto trigger,activate,wait,terminate.
That's kind of the process I want it to go if all can be done.
Batch File:
#echo off
start "Blade" "C:\Users\user\Documents\Blade.xpadderprofile" Blade.xpadderprofile
ECHO Blade.xpadderprofile STARTED
start /w "C:\Program Files (x86)\game\games.exe" games.exe
ECHO games STARTED
taskkill /f /im Xpadder.exe
This actually works but the problem is there are two ".exe" files with mmo's. I'll start the game and it would close Xpadder too early because one launcher starts another launcher/client. Xpadder works for the first launcher but the first launcher closes so the game will start. I hope I am explaining myself clear enough.
Reference link: How to automatically close App_A when I close App_B using batchfile
Essentially, this is the same question I have but it's not very clear. What is the batch code to get Xpadder to stay on until the second launcher/client is closed not the first one?
P.S
The game has to open through the launcher then into the second launcher/client or it will not work.
here is the other clients name and path i think:
C:\Program Files (x86)\game\gamer\bin\gam.exe
How about the use of the PsExec, a small MS ulility? Using it, your batch should work:
cmd /c psexec -d "Blade" "C:\Users\user\Documents\Blade.xpadderprofile" Blade.xpadderprofile
start /w "C:\Program Files (x86)\game\games.exe" games.exe
taskkill /f /im Xpadder.exe
The file psexec.exe must be placed in folder enlisted in the system variable WINDIR or PATH, or, otherwise, you should call it with its full path, eg. *C:\Program Files\Others\pstools.exe".
You can add #echo off, salt, pepper or some green Tabasco if you have mon€y for this :D

Batch: renaming file with the same name as recently deleted file says file already exists

I am trying to use this to kill a running process, then delete the exe which runs that process and after that rename a file (.temp) with the same name as the deleted file and finally run it. However, I am having two problems: first one is that I am required to run this as administrator in order to be able to kill the task (I have solved this already running cmd as admin), and the second problem is that when rename is executed it says that the file already exists (but it should not because there was a delete just before). The code runs fine if you are not running the exe you want to kill, but if it is running then the said error appears.
#echo off
set name=%1
taskkill /F /IM %name%
del %name%
ren %name%.temp %name%
start %name%
exit
Am I missing something in this piece of batch code?
Thanks
It can be that the delete has not entirely resolved yet. You can use MOVE instead of REN, which will overwrite the file.
According to #Mofi, the problem lies in the fact that the deletion is executed too early since the taskkill has not been finished yet. This was fixed by adding "timeout /t 2" between taskkill and del.

How do you have a called bat file not kill it's calling bat file?

I am on a Windows 2003 system and need to script the deletion and creation of a profile in WebSphere Application Server. This requires me to call manageprofiles.bat twice, once to delete the existing profile and once to create a new profile.
In my batch file, I have the following:
cd "C:\Program Files\IBM\WebSphere\AppServer\bin"
manageprofiles.bat -delete -profileName AppSrv01
rmdir /s /q ..\profiles\AppSrv01
manageprofiles.bat -create -templatePath ..\profileTemplates\default -profileName AppSrv01 -profilePath ..\profiles\AppSrv01
The manageprofiles.bat file ends with:
set RC=%ERRORLEVEL%
#endlocal & exit /b %RC%
When there is an error deleting the profile in the second line of my batch file (which happens way too often), manageprofiles.bat spits out an error message and causes my batch file to terminate. I don't want this to happen since I will just delete the remainder of the profile in the next command. Reading the documentation for exit leads me to believe that the /b in the exit command in manageprofiles.bat should cause just manageprofiles.bat to terminate without affecting my bat file.
I don't want to touch the manageprofiles.bat file in any way since my changes could get reverted by an update down the road and break my script again. Is there anything I can do in my batch file to fix this?
Change both occurrences of "manageprofiles.bat" to "call manageprofiles.bat". Without the "call", execution is transferred to the manageprofiles.bat file but doesn't return.
Does using
call manageprofiles.bat
make any difference?

Resources