I am trying to write a windows batch file that will print and execute a command if a window is open on the system. I can do this with a process but this is not the effect i want. I want to know if a window is open if so then print open if not do nothing, If there is a way that i can do this please let me know. Better description is when you open taskmanager and the first tab shows open windows. I just want a list of these windows and then from there just search for a program such as googlechrome.exe
Thank you all in advanced!
To see which executables are running from the command line, use: tasklist.exe. This enables you to filter on a WINDOWTITLE attribute as well. Beyond that, you might be out of luck.
Example to see if an untitled notepad instance is running:
tasklist /FI "WINDOWTITLE eq Untitled - Notepad"
You might use this inside a batch file as follows:
FOR /F "tokens=* USEBACKQ" %%F IN (`tasklist /FI "WINDOWTITLE eq Untitled - Notepad" ^| find/c "exe"`) DO (
SET windowcount=%%F
)
echo %windowcount%
if "%windowcount%"=="0" (
echo no windows!
) ELSE (
echo %windowcount% windows found!
)
Related
Running on Windows 10 Pro 64-bit [Version 10.0.17763.1637];
I'm writing a simple batch file to show active processes:
cls
echo.
echo Showing all processes similar to %1*
tasklist /FO table /FI "IMAGENAME eq %1*"|more
However, I see that the screen is not being cleared as the script is launched (CLS works fine if I invoke it manually from my Command Line).
I know it's actually a quite silly issue, I'm just curious to know why that hppens and if there's any workaround.
Thanks in advance!
Massimo
i checked your script and cls works perfect(except for it isn't showing active processes) you just need to add #echo offand also validate encoding as ANSI if it isn't,if you want command line without prompt,here it is:
#echo off
cls
echo.
echo Showing all processes similar to %1*
tasklist
rem tasklist /FO table /FI "IMAGENAME eq %1*"|more
I am trying to create a batch file that runs a command.
The first part of the command checks to see if a particularly named window is open on my desktop. If it is it then closes that window (by sending a command to the program with the window open - Virtual Audio Cable).
#For /f "Delims=:" %A in ('tasklist /nh /v /fi "WINDOWTITLE eq Headphones"') do #if %A==INFO (echo Prog not running) else (start /min "audiorepeater_ks" "c:program files\Virtual Audio Cable\audiorepeater_ks.exe" /CloseInstance: Headphones)
Now this command seems to work fine when I execute it via the command line.
But it does nothing when I try to put it in a batch file
I have tried a .bat file and a .cmd file and also created a shortcut to the .cmd file where i have prepended the target field in properties with "C:\Windows\System32\cmd.exe /c"
Any ideas on how I can get this to run via batch?
You will have to escape "%A" with "%%A" as CristiFati pointed out. For more escape characters, please visit https://www.robvanderwoude.com/escapechars.php
For definitely not malicious reasons, I need to have a batch file always open.
I have some base code:
:b
echo off
tasklist /fi "imagename eq cmd.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "game.bat"&start game.bat
goto b
And it works fine if want notepad.exe or blah.txt and etc.
Except for batch files, as the program itself is a batch file,
the system sees cmd.exe is already open.
It works except for batch files, as the system sees cmd.exe is already open.
Give your batch file a Title by adding the following command to game.bat:
title %~nx0
Check if game.bat is running by using tasklist with /v option:
:b
#echo off
tasklist /v | find "game.bat" > nul
rem errorlevel 1 means game.bat is not running, so start it
if errorlevel 1 start game.bat
rem timeout to avoid excessive processor load
timeout 60
goto b
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
tasklist - TaskList displays all running applications and services with their Process ID (PID) This can be run on either a local or a remote computer.
timeout - Delay execution for a few seconds or minutes, for use within a batch file.
title - Change the title displayed above the CMD window.
I'm trying to uninstall a program EXE via batch file and am not having any success.
The uninstall string found in the registry is as follows:
C:\PROGRA~1\Kofax\Capture\ACUnInst.exe /Workstation
C:\PROGRA~1\Kofax\Capture\UNWISE.EXE /U
C:\PROGRA~1\Kofax\Capture\INSTALL.LOG
If I run that from CMD or batch it does nothing.
If I run C:\PROGRA~1\Kofax\Capture\UNWISE.EXE /U from CMD it will open up a dialog box to point to the INSTALL.LOG file and then proceed to uninstall.
At the end, it will ask me to click finish.
I need this to be silent, can you point me in the right direction? This is on XP and 7.
Every program that properly installs itself according to Microsoft's guidelines makes a registry entry in either HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall (for machine installs) or HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall (for user profile installs). Usually, the key for the program will be its GUID, or else the name of the program. Within that key will be an entry called UninstallString. This contains the command to execute to uninstall the program.
If you already know ahead of time what you will be uninstalling, it should be easy enough to just put that in your batch file. It gets tricky when you try to automate that process though. You can use the reg command to get data from the registry, but it returns a lot of text around the actual value of a given key, making it hard to use. You may want to experiment with using VBscript or PowerShell, as they have better options for getting data from the registry into a variable.
This might help you further.....
How to Create a script via batch file that will uninstall a program if it was installed on windows 7 64-bit or 32-bit
I've had the same problem and this is what I came up with.
Before you start using this method though, you might wanna look up the name of the application on WMIC using CMD so..
First you wanna do: WMIC product > C:\Users\"currentuser"\Desktop\allapps.txt
I'd recommend to output the command to an TXT file because it's really confusing to read it in the Cmd prompt, plus is easier to find the data you are looking for.
Now what you wanna do is find the actual name of the app... If you look at the code I put in, the app name says SkypeT because skype has "™" in the end of it and the command prompt can't interpretate that as it is.
After you got the app name, just put in the find in the 4th line and substitute, a few lines which contain my examples with skype...
Also you can probably creat a variable called %APP% and not worry as much, but at it's current it works just fine...
One thing to note! with me the msi /quiet command did not work, the program would not install or uninstall so I used /passive, which lets the users see what's going on.
#Echo off
CD %cd%
:VerInstall
for /f "tokens=12,*" %%a in ('wmic product list system ^| Find /I "SkypeT"') do (
if Errorlevel = 0 (
Echo Skype is installed! )
if Errorlevel = 1 ( Echo Skype is not installed, proceding to the installation!
Ping localhost -n 7 >nul
goto :Reinstall )
)
:Status
tasklist /nh /fi "IMAGENAME eq "APP.exe" | find ":"> nul
if errorlevel = 1 goto :force
goto :Uninstall
:Force
echo We are killing the proccess... Please do not use the application during this process!
Ping localhost -n 7 > nul
taskkill /F /FI "STATUS eq RUNNING" /IM APP* /T
echo The task was killed with success! Uninstalling...
Ping localhost -n 7 > nul
:Uninstall
cls
for /f "tokens=12,*" %%a in ('wmic product list system ^| Find /I "SkypeT"') do (
set %%a=%%a: =%
msiexec.exe /x %%a /passive /norestart
)
:DoWhile
cls
Tasklist /fi "IMAGENAME eq msi*" /fi "STATUS eq RUNNING" | Find ":" >nul
if errorlevel = 1 (
echo Installation in progress
Goto :DoWhile
)
echo Skype is Uninstalled
:Reinstall
msiexec.exe /i SkypeSetup.msi /passive /norestart
:reinstallLoop
Tasklist /fi "IMAGENAME eq msi*" /fi "STATUS eq RUNNING" | Find ":" >nul
if errorlevel = 1 (
echo Installation in progress
goto :reinstallLoop
)
echo Skype is installed
:end
cls
color 0A
Echo Done!
exit
One last thing. I used this as an Invisible EXE task, so the user couldn't interact with the command prompt and eventually close the window (I know, I know, it makes the whole echoes stupid, but it was for testing purposes).for that I used BAT to EXE converter 2.3.1, you can put everything to work on the background and it will work very nicelly. if you want to show progress to users just write START Echo "info" and replace the info with whatever you want, it will open another prompt and show the info you need.
Remember, Wmic commands sometimes take up to 20 seconds to execute since it's querying the conputer's system, so it might look like it's doing nothing at first but it will run! ;)
Good luck :)
We needed a batch file to remove a program and we couldn't use programmatic access to the registry.
For us, we needed to remove a custom MSI with a unique name. This only works for installers that use msi or integrate such that their cached installer is placed in the Package_Cache folder. It also requires a unique, known name for the msi or exe. That said, it is useful for those cases.
dir/s/b/x "c:\programdata\packag~1\your-installer.msi" > removeIt.bat
set /p RemoveIt=< removeIt.bat
echo ^"%RemoveIt%^" /quiet /uninstall > removeIt.bat
removeIt.bat
This works by writing all paths for 'your-installer.msi' to the new file 'removeIt.bat'
It then assigns the first line of that bat file to the variable 'RemoveIt'
Next, it creates a new 'removeIt.bat' that contains the path/name of the .msi to remove along with the needed switches to do so.
Finally, it runs the batch file which executes the command to uninstall the msi. This could be done with an .exe as well.
You will probably want to place the 'removeIt.bat' file into a known writable location, for us that was the temp folder.
Is it possible to attach the windows debugger in VS2010 to a process from a batch file?
preferably by giving it a process name
Since you presumably already have the process running, you would use vsjitdebugger.exe /p 1234 where 1234 is the PID of the process you want to debug. If you don't know it, you would have to use some other method to figure it out.
If you have the debugging tools for windows available, the tlist.exe utility will yield the process ID for a process name. If that is available, then the following will attach to a given process:
rem Get the process ID
for /f %%f in ('tlist -p %1') do set mypid=%%f
rem attach to it with selected debugger
vsjitDebugger -p %mypid%
Edit If tlist is not available, I think tasklist will work. It's a bit uglier, but the following worked for me (you know ... it works my on my system :) Note too that I edited the command previous example to work in a cmd.exe prompt (I use tcc, which does require as many % signs).
rem Get the process ID
for /f "tokens=2 delims= " %%f in ('tasklist /nh /fi "imagename eq %1"' ) do set mypid=%%f
rem attach to it with selected debugger
vsjitDebugger -p %mypid%
Specifying a /Command switch on devenv.exe 's command-line will make it run a specified command on open. You could specify the Debug.AttachToProcess command. Don't know if you can specify a pid, though, when you execute that command.