Running a program after copy file, in one Windows CMD line? - windows

I try to create visual studio post-build event command line and i want:
to kill process if exist;
copy file;
launch a program;
start taskkill /f /im app.exe /t & xcopy /y
"$(TargetPath)" "C:\Program Files (x86)\path\$(TargetFilename)" & start "" "C:\Program Files (x86)\program\app.exe"
I use start taskkill /f /im app.exe /tinstead of taskkill /f /im app.exe /t because if the process not exists i get not found error
Now all tree command runs OK with no error but the last command not launch the program but if i execute this command separately the program is executed.
How can i fix this?

You have a Timing Problem.
Try:
start /wait taskkill /f /im app.exe /t & xcopy /y "D:\Workspaces\Dev\path\bin\Debug\my.dll" "C:\Program Files (x86)\path\my.dll" & start "" "C:\Program Files (x86)\program\app.exe"

Related

Trying to kill tasks that have been started in a file

So I am trying to start a butch of programs at once but then I want to to be able to kill all those programs at a later time. So I have created two bat files one to start the programs in the file and one to stop them.
This is the bat file that should stop them.
for /r "." %%a in (*.exe) do taskkill /IM %%~nxI
pause
It gives me an error that file was not found here is the error that it gave me. > ERROR: Invalid argument/option - 'D:\FileName'
Here is the code that starts them:
for /r "." %%a in (*.exe) do start "" "%%~fa"
pause
To kill all the tasks in the file:
for /r "." %%a in (*.exe) do taskkill /IM "%%~nxa"
pause

How to keep batch file running whilst using TASKKILL /f /t /im explorer.exe?

In short, I need to start 1.bat, that can CALL or START 2.bat to execute TASKKILL /im explorer.exe /f /t but keeps "1.bat" running (or reopens it) once "2.bat" is finished. The difficulty is, I need to keep the /t switch in TASKKILL to make it transferable.
Full Explanation:
I have several older laptops running Windows 7 x64. They will run games, but only if I use Task Manager to end Explorer, it's associated processes and stop several unneeded services. This frees up RAM and CPU to start games via Task Manager..
To avoid ending each process/service individually every time, I wrote 2 cmd batch files:
1) Options.bat -- (SHORTENED)
#echo off
:begin
echo (0) Kill Processes
echo (1) Run [game]
SET /p op=Select Task:
if "%op%"=="0" goto killall
if "%op%"=="1" goto op1
killall:
CALL "C:\TK.bat"
cls
goto begin
op1:
start " " /realtime "C:\[exe path]"
goto exit
:exit
exit
2) TK.bat -- (SHORTENED)
net stop [service]
taskkill /im explorer.exe /f /t
taskkill /im [specific process].exe /f /t
Both of which work as intended - provided I initially start Options.bat via Task Manager.
Problem: when I run Options.bat from Windows Explorer (even "Run As Administrator") and call the TK.bat script to run TASKKILL /im explorer.exe /f /t it does work, but also closes the CMD window, when i want it to return to the options selection. This does make sense when i'm using the "tree" attribute for TASKKILL.
However, if I run Options.bat via Task Manager, and CALL TK.bat, it will execute the commands (without closing itself) then return to the task selection - which is exactly what i want to happen!
I assume this is because it is running as the Local System account via Task Manager and not mine or the built in Administrator account?
I have tried the RUNAS command within Options.bat -
RUNAS /user:Adminstrator "C:\TK.bat"
which runs TK.bat, executes the TASKKILL command and then RUNAS again to return to the first batch file in my username. This does work, but I still have to press enter at each password prompt.
I have also tried numerous variations of the TASKKILL /FI switch:
TASKKILL /fi IMAGENAME ne cmd.exe /im explorer.exe /f /t`
TASKKILL /fi USERNAME eq [name] /fi WINDOWTITLE ne Options.bat /im explorer.exe /f /t
Which, I thought, would end all processes "not equal" to cmd.exe/Options.bat but I cannot get it to work.
Question: Is there a way of executing the TASKKILL /im explorer.exe /f /t within TK.bat, that will not close the currently running batch file and without having to run it from Task Manager? Perhaps a different command or giving the Options.bat some sort of elevated authority to stop it from being closed when Explorer.exe /t is ended. Ideally, without installing separate 3rd party tools?
I know I could edit the batch file to end explorer.exe, end each associated .exe and then stop the services I do not need, individually, but this is time consuming and defeats the point of writing the file to make it automated and transferable (if needed).
Any help would be greatly appreciated and I can upload the full code of both batch files, if that would help.
Strangely enough, the simple taskkill /F /IM explorer.exe & start explorer command does this task. I don't know why, but it works.
To prevent taskkill /im explorer.exe /f /t from killing the cmd instance which is executing your batch file, You need to break the parent-child relationship between that particular instance of cmd.exe and explorer.exe.
The trick is to use two extra instances of nested cmds. The first one launches the second one and terminates immediately so the second instance becomes an orphaned process which can not be determined as descendant of exeplorer.exe. At this point the second instance can safely execute taskkill /im explorer.exe /f /t
start "Chain breaker" /min cmd /d /c start "Orphaned Process" cmd /d /c Options.bat
The above can be used from Command Prompt or from another batch file.
You can also incorporate this technique directly in to the Options.bat file, But extra logic is needed for Options.bat to determine when to launch itself in nested cmd and when to execute the actual code.
A sample script demonstrating the concept would be:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=3 delims=:" %%L in ("%~0") do goto %%L
REM Reinvoke the batch file forcing it to jump to the :main label
start "Chain breaker" /min cmd /d /c start "Orphaned Process" cmd /d /c #"%~d0\:main:\..%~pnx0"
exit /b
:main
taskkill /im explorer.exe /f /t
echo I'm alive
pause
exit /b

I want assistance on Taskkill command

I have below command and saved it as a .bat file.
taskkill /F /IM explorer.exe
start /realtime chrome.exe
now I want to add some further command so that upon closing google chrome Windows Explorer process gets started automatically.
use start /wait to wait until the started process is finished/exited. Then just execute explorer.exe
taskkill /F /IM explorer.exe
start /realtime /wait chrome.exe
explorer.exe
Note: I don't think, its a good idea to kill explorer or to start a browser as "realtime", but I see your point: being on a meager system you want to assign every available resource to the task to be done.
(But Chrome might not be the best choice in this case)
Try the following with administrator privileges
start /min taskkill.exe /f /im explorer.exe 2>nul >nul && start "" /wait "%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe" && start "" /wait /realtime "%windir%\explorer.exe" && start "" /realtime "%windir%\system32\taskkill.exe" /f /im cmd.exe <nul
This final part: start "" /realtime "%windir%\system32\taskkill.exe" /f /im cmd.exe <nul, can be replace by call some_bat.bat <nul with all commands that you need to run.

CMD prompt - bat file - Taskkill command ignores start /wait

Here's my script in my bat file:
cd "C:\Program Files (x86)\Steam\Steamapps\common\F13game\"
Start F13theGameRemap.exe
cd "C:\Program Files (x86)\Steam\steamapps\common\F13Game\SummerCamp\Binaries\Win64\"
Start /WAIT SummerCamp.exe
Start /WAIT Taskkill /f /im F13theGameRemap.exe
exit
All I want is to run the first program, then the 2nd. When the 2nd program "summercamp.exe" exits, i want the first program "F13thegameremap.exe" to exit.
The way this script works now is that Taskkill immediately ignores the start /wait command of summercamp.exe and closes F13theGameRemap.exe immediately.
How do I fix this?
Something like this:
#Echo off
Cd /D "C:\Program Files (x86)\Steam\Steamapps\common\F13game\"
Start F13theGameRemap.exe
Cd /D "C:\Program Files (x86)\Steam\steamapps\common\F13Game\SummerCamp\Binaries\Win64\"
Set "App2=SummerCamp.exe"
Start /WAIT %App2%
:loop
Timeout /t 1 >NUL
tasklist.exe /FI "ImageName eq %App2%" /NH |find /i "%App2%" >NUL && Goto :loop
Start /WAIT Taskkill /f /im F13theGameRemap.exe
The :loop will check with 1 second delay if %App2% aka SummerCamp.exe is still running.

I have a Batch file that runs perfectly fine on a local machine, but doesn't run through SCCM

#echo off
taskkill /IM Notebook.exe /F
taskkill /IM ResponseSoftwareService.exe /F
taskkill /IM DesktopMenu.exe /F
del "C:\ProgramData\FLEXNet\*" /A /Q /F
"C:\Program Files (x86)\Common Files\SMART Technologies\SMART Product Update\activationwizard.exe" --puid education_bundle --m=4 --v=3 --a --pk="Key input here"
This is the code I placed the actual key where it says Key input here....But like I said when I run this batch file locally everything runs correctly. but when I put it in the Task sequence of SCCM it doesn't run correctly and I have to run this batch file any way. Any ideas or tips?
What return code do you get?
It might also be worth redirecting the output of each command and its exit code to get more information, so for example:
taskkill /IM Notebook.exe /F >> C:\Windows\Temp\mylogfile.log
echo %ERRORLEVEL% >> C:\Windows\Temp\mylogfile.log
taskkill /IM ResponseSoftwareService.exe /F >> C:\Windows\Temp\mylogfile.log
echo %ERRORLEVEL% >> C:\Windows\Temp\mylogfile.log
And so on.
That should then tell you if it's permissions related or any other errors that arise.
If you're stilling having problems afterwards then post your results and I'll be happy to advise further.

Resources