CMD command not running in batch file - windows

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

Related

Re-opening files in Batch

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.

Wait for a .bat file to close within a windows batch file

I need to create a windows batch file (*.bat) file that only runs its commands if certain processes (and batch files) are NOT running.
I have looked at a solution that works for processes (*.exe) here:
How to wait for a process to terminate to execute another process in batch file
I want to do something very similar, however, there is one difficulty: Batch files show up as "cmd.exe" in the "TASKLIST" command.
I want to check if a specific bat file is running, for example: "C:\mybatch.bat", and if it is, wait until it is closed.
Checking if a specific bat file mybatch.bat is running could be a tougher task than it could look at first sight.
Looking for a particular window title in tasklist /V as well as testing CommandLine property in wmic process where "name='cmd.exe'" get CommandLine might fail under some imaginable circumstance.
1st. Can you
add title ThisIsDistinguishingString command at beginning of the mybatch.bat and
remove all other title commands from mybatch.bat and
ensure that mybatch.bat does not call another batch script(s) containing a title command?
Then check errorlevel returned from find command as follows:
:testMybatch
tasklist /V /FI "imagename eq cmd.exe" | find "ThisIsDistinguishingString" > nul
if errorlevel 1 (
rem echo mybatch.bat batch not found
) else (
echo mybatch.bat is running %date% %time%
timeout /T 10 /NOBREAK >NUL 2>&1
goto :testMybatch
)
2nd. Otherwise, check if wmic Windows Management Instrumentation command output could help
wmic process where "name='cmd.exe'" get /value
Then you could detect mybatch.bat in its output narrowed to
wmic process where "name='cmd.exe'" get CommandLine, ProcessID
Note that wmic could return some Win32_Process class properties, particularly CommandLine, empty if a particular process was launched under another user account or elevated (run as administrator).
Elevated wmic returns all properties in full.
What you say happens by default.
To test, crate a new .bat file (let's say 1.bat) and put in it
calc
mspaint
Save and run it.
Calculator will start. You will notice that Paitbrush will launch only when you have closed calculator.

Run as clicked vbs from Scheduled Tasks - Windows 7

I have a .vbs file called test.vbs. And this is running test.bat file silently.
In test.bat file,
CD.>"C:\folder\empty.srt"
dir /b /s "C:\folder" | findstr /m /i "\.srt$" > C:\old.txt
CD.>C:\new.txt
.
.
echo for /f "delims=" %%a in ('findstr /G:%new% /I /L /B /V %old%') do (#echo %%~nxa >> C:\added.txt)
.
.
CD.>C:\added.txt
del "C:\folder\empty.srt"
When I run test.vbs manually .bat file works fine.
But when I run test.vbs from Windows' Scheduled Tasks (command is: C:\test.vbs (tried wscript test.vbs) )
Only creates empty.srt and removes empty.srt. The other commands are not working.
I don't understand why (maybe administrator priviliges (Account is administrator too).
I thought running as administrator would solve the problem. Or is there another way to do this? How can I do that?
edit: Also working when I run this command from CMD --> wscript C:\test.vbs
Also its working fine on Windows 8
I also ran into this issue a while back, as well as have answered a variation of this issue before.
If it's a 64 bit operating system, then you need to open the script via "C:\windows\syswow64\cscript.exe your_vbs_code.vbs" if not, just use "C:\windows\system32\cscript.exe".

how to kill all batch files except the one currently running

How can you run a batch file which taskkills all other cmd.exes which are currently running, except for the one that is doing the task kill command?
Combining these 2 threads:
how to get PID from command line filtered by username and imagename
how to get own process pid from the command prompt in windows
you can write down something like this in a simple cmd file (akillfile.cmd)
title=dontkillme
FOR /F "tokens=2 delims= " %%A IN ('TASKLIST /FI ^"WINDOWTITLE eq dontkillme^" /NH') DO SET tid=%%A
echo %tid%
taskkill /F /IM cmd.exe /FI ^"PID ne %tid%^"
copy cmd.exe, rename it to a.exe, then use this command in a batch file: start a.exe /k taskkill /f /im cmd.exe
This worked for me, added to the very beginning of the .bat which is to kill all previously launched instances of itself, then immediately make itself susceptible to be killed by subsequent calls of the same .bat file:
title NewlyLaunchedThing
taskkill /F /IM cmd.exe /FI "WINDOWTITLE ne NewlyLaunchedThing"
title Thing
...do everything else
Note that, apparently, the "title" is a keyword, which sets a variable named "WINDOWTITLE".
Also, the "=" (equals sign) is apparently optional for assigning title/WINDOWTITLE. Meaning, this works as well, and may be preferred for clarity:
title = newTitle

Batch list open windows

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!
)

Resources