I have made a file named CHECKPRODUCT.CMD. but I have another file that runs at startup and it is named ACTIVATED.cmd. but I don't understand how to exists task ACTIVATED.cmd in CHECKPRODUCT.cmd. like I tried this examples :
if tasklist == "ACTIVATED.cmd" (
goto :1 ) else (
goto :2 )
but, it doesn't work!
can someone help me with that ?? I NEED help. really!
thank you so much!
You can use tasklist to check many facets of tasks running and the OLD school training was give your bat files a title for exactly this reason.
First always ensure (and I often forget) use Title in a bat or cmd that is likely to not exit quickly its useful for tasklist as here and on occasion taskkill /t or considered bad if you use /F. Try to use a unique name otherwise editing activate.cmd can show up in notepad and cmd
#echo off & title ACTIVATED
echo Running Activated
pause
You can use other status tests than "running" see tasklist /?
tasklist /FI "WindowTitle eq ACTIVATED" /FI "STATUS eq running" | Find /i "cmd"
REM bad find=2 not found=1 found at least once=0
if %errorlevel% == 0 (
echo true) else (
echo false)
pause
You may need to use ....ACTIVATED*" if you want to test for multiple entries etc.
If I understand your question correctly, then you're trying to determine if another batch file is still running from another.
You can do that by checking the command line strings of running cmd.exe processes, using the WMI command line utility.
#Echo Off
SetLocal EnableExtensions
Set "ScriptName=ACTIVATED.cmd"
%SystemRoot%\System32\wbem\WMIC.exe Process Where^
"Name='cmd.exe' And CommandLine Like '%%%ScriptName:_=[_]%%%'"^
Get ProcessId /Value 2>NUL | %SystemRoot%\System32\find.exe "=" 1>NUL^
|| GoTo :2
:1
Echo %ScriptName% is running.
Pause
GoTo :EOF
:2
Echo %ScriptName% is not running.
Pause
Note: Whilst you're free to modify the script name on line 4, please be aware that this would need additional work, should you decide to use file names containing [, ], ^, or % characters.
For some reason it says awesome even when the program is not open, and even if I put in a window name like "asdfsd" or something random. Can anyone help?
#echo off
:start
tasklist | find /I "WINDOWNAME"
if errorlevel 1 (
echo awesome
)
goto :start
At first, let me recommend not to use find just to find a certain window title in the whole output of tasklist, because the search string might occur somewhere else, like the image name, for example, which could lead to false matches.
Anyway, the tasklist command does not set the exit code (ErrorLevel) when the filter /FI does not find a match, but you could check whether the output begins with INFO:, which is the case when no match was encountered:
:start
timeout /T 1
tasklist /FI "WindowTitle eq WindowName" | findstr /B "INFO:" > nul && (echo goto :start) || (echo awesome)`.
This depends on the returned single line in case of no matches:
INFO: No tasks are running which match the specified criteria.
This text depends on the locale/region/language settings of the system. To make this even locale-independent, you could use the a trick: tasklist, with default output format (/FO TABLE), returns more than a single line when at least a match is encountered, because there is a two-line header followed by the actual matching items; if there is no match, the aforementioned line is the only one returned. So capture the output of tasklist by a for /F loop, using the option skip=1. The for /F loop will then set the exit code to 1 (not the ErrorLevel though) when it does not iterate, and to 0 when it iterates at least once. This exit code can be checked using the conditional execution operators && and ||:
:start
timeout /T 1
(for /F "skip=1" %%I in ('tasklist /FI "WindowTitle eq WindowName"') do rem/) && (echo awesome) || (goto :start)
I inserted the timeout command in order to avoid heavy CPU loads by the goto :start loop.
Is it possible to make an if statement for batch based off a user opening a program?
I tried this:
color c
echo off
cls
:props
if open WINWORD.exe (echo a & pause 10 & exit)
goto props
Doing so will simply post the error 'WINWORD was unexpected at this time.' and kill the command prompt.
What I am trying to achieve is a batch file that will:
Look if anyone is opening WINWORD.exe
Once somebody has opened the file the command prompt will display 'a'
Exit the command prompt after 10 seconds once 'a' was displayed.
You can search for the process using tasklist.exe, together with find to count the instances:
set PROC=winword.exe
tasklist /FI "IMAGENAME eq %PROC%" | find /C /I "%PROC%"
I used a variable %PROC% to specify the process (*.exe) name just for the sake of convenience.
find will also exit with the return code (ErrorLevel) of 1 if no instances have been found. With a simple if statement you can do what you requested:
if not ErrorLevel 1 ((echo a) & timeout /T 10 /NOBREAK & exit)
I replaced the pause 10 by the timeout command because pause waits for the user to press any key (any arguments are ignored). The switch /NOBREAK means to ignore any key presses.
The parenthesis around echo a avoids a trailing space to be echoed as well.
So all together, the following should do what you asked for:
color c
echo off
set PROC=winword.exe
cls
:props
tasklist /FI "IMAGENAME eq %PROC%" | find /C /I "%PROC%" > nul
if not ErrorLevel 1 ((echo a) & timeout /T 10 /NOBREAK & exit)
timeout /T 1 /NOBREAK > nul
goto props
The (optional) > nul portion after find prevents it from displaying the found number of instances.
The second delay time timeout /T 1 has been inserted to avoid massive CPU load within this loop structure.
What is the Windows batch equivalent of the Linux shell command echo -n which suppresses the newline at the end of the output?
The idea is to write on the same line inside a loop.
Using set and the /p parameter you can echo without newline:
C:\> echo Hello World
Hello World
C:\> echo|set /p="Hello World"
Hello World
C:\>
Source
Using: echo | set /p= or <NUL set /p= will both work to suppress the newline.
However, this can be very dangerous when writing more advanced scripts when checking the ERRORLEVEL becomes important as setting set /p= without specifying a variable name will set the ERRORLEVEL to 1.
A better approach would be to just use a dummy variable name like so:
echo | set /p dummyName=Hello World
This will produce exactly what you want without any sneaky stuff going on in the background as I had to find out the hard way, but this only works with the piped version; <NUL set /p dummyName=Hello will still raise the ERRORLEVEL to 1.
The simple SET /P method has limitations that vary slightly between Windows versions.
Leading quotes may be stripped
Leading white space may be stripped
Leading = causes a syntax error.
See http://www.dostips.com/forum/viewtopic.php?f=3&t=4209 for more information.
jeb posted a clever solution that solves most of the problems at Output text without linefeed, even with leading space or = I've refined the method so that it can safely print absolutely any valid batch string without the new line, on any version of Windows from XP onward. Note that the :writeInitialize method contains a string literal that may not post well to the site. A remark is included that describes what the character sequence should be.
The :write and :writeVar methods are optimized such that only strings containing troublesome leading characters are written using my modified version of jeb's COPY method. Non-troublesome strings are written using the simpler and faster SET /P method.
#echo off
setlocal disableDelayedExpansion
call :writeInitialize
call :write "=hello"
call :write " world!%$write.sub%OK!"
echo(
setlocal enableDelayedExpansion
set lf=^
set "str= hello!lf!world^!!!$write.sub!hello!lf!world"
echo(
echo str=!str!
echo(
call :write "str="
call :writeVar str
echo(
exit /b
:write Str
::
:: Write the literal string Str to stdout without a terminating
:: carriage return or line feed. Enclosing quotes are stripped.
::
:: This routine works by calling :writeVar
::
setlocal disableDelayedExpansion
set "str=%~1"
call :writeVar str
exit /b
:writeVar StrVar
::
:: Writes the value of variable StrVar to stdout without a terminating
:: carriage return or line feed.
::
:: The routine relies on variables defined by :writeInitialize. If the
:: variables are not yet defined, then it calls :writeInitialize to
:: temporarily define them. Performance can be improved by explicitly
:: calling :writeInitialize once before the first call to :writeVar
::
if not defined %~1 exit /b
setlocal enableDelayedExpansion
if not defined $write.sub call :writeInitialize
set $write.special=1
if "!%~1:~0,1!" equ "^!" set "$write.special="
for /f delims^=^ eol^= %%A in ("!%~1:~0,1!") do (
if "%%A" neq "=" if "!$write.problemChars:%%A=!" equ "!$write.problemChars!" set "$write.special="
)
if not defined $write.special (
<nul set /p "=!%~1!"
exit /b
)
>"%$write.temp%_1.txt" (echo !str!!$write.sub!)
copy "%$write.temp%_1.txt" /a "%$write.temp%_2.txt" /b >nul
type "%$write.temp%_2.txt"
del "%$write.temp%_1.txt" "%$write.temp%_2.txt"
set "str2=!str:*%$write.sub%=%$write.sub%!"
if "!str2!" neq "!str!" <nul set /p "=!str2!"
exit /b
:writeInitialize
::
:: Defines 3 variables needed by the :write and :writeVar routines
::
:: $write.temp - specifies a base path for temporary files
::
:: $write.sub - contains the SUB character, also known as <CTRL-Z> or 0x1A
::
:: $write.problemChars - list of characters that cause problems for SET /P
:: <carriageReturn> <formFeed> <space> <tab> <0xFF> <equal> <quote>
:: Note that <lineFeed> and <equal> also causes problems, but are handled elsewhere
::
set "$write.temp=%temp%\writeTemp%random%"
copy nul "%$write.temp%.txt" /a >nul
for /f "usebackq" %%A in ("%$write.temp%.txt") do set "$write.sub=%%A"
del "%$write.temp%.txt"
for /f %%A in ('copy /z "%~f0" nul') do for /f %%B in ('cls') do (
set "$write.problemChars=%%A%%B ""
REM the characters after %%B above should be <space> <tab> <0xFF>
)
exit /b
As an addendum to #xmechanix's answer, I noticed through writing the contents to a file:
echo | set /p dummyName=Hello World > somefile.txt
That this will add an extra space at the end of the printed string, which can be inconvenient, specially since we're trying to avoid adding a new line (another whitespace character) to the end of the string.
Fortunately, quoting the string to be printed, i.e. using:
echo | set /p dummyName="Hello World" > somefile.txt
Will print the string without any newline or space character at the end.
A solution for the stripped white space in SET /P:
the trick is that backspace char which you can summon in the text editor EDIT for DOS. To create it in EDIT press ctrlP+ctrlH.
I would paste it here but this webpage can't display it. It's visible on Notepad though (it's werid, like a small black rectangle with a white circle in the center)
So you write this:
<nul set /p=.9 Hello everyone
The dot can be any char, it's only there to tell SET /P that the text starts there, before the spaces, and not at the "Hello".
The "9" is a representation of the backspace char that I can't display here. You have to put it instead of the 9, and it will delete the "." , after which you'll get this:
Hello Everyone
instead of:
Hello Everyone
I hope it helps
Here is another method, it uses Powershell Write-Host which has a -NoNewLine parameter, combine that with start /b and it offers the same functionality from batch.
NoNewLines.cmd
#ECHO OFF
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 1 - ';Write-Host -NoNewLine 'Result 2 - ';Write-Host -NoNewLine 'Result 3 - '"
PAUSE
Output
Result 1 - Result 2 - Result 3 - Press any key to continue . . .
This one below is slightly different, doesn't work exactly like the OP wants, but is interesting because each result overwrites the previous result emulating a counter.
#ECHO OFF
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 1 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 2 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 3 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 4 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 5 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 6 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 7 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 8 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 9 - '"
PAUSE
You can remove the newline using "tr" from gnuwin32 (coreutils package)
#echo off
set L=First line
echo %L% | tr -d "\r\n"
echo Second line
pause
By the way, if you are doing lots of scripting, gnuwin32 is a goldmine.
I made a function out of #arnep 's idea:
echo|set /p="Hello World"
here it is:
:SL (sameline)
echo|set /p=%1
exit /b
Use it with call :SL "Hello There"
I know this is nothing special but it took me so long to think of it I figured I'd post it here.
DIY cw.exe (console write) utility
If you don't find it out-of-the-box, off-the-shelf, you can DIY. With this cw utility you can use every kind of characters. At least, I'd like to think so. Please stress-test it and let me know.
Tools
All you need is .NET installed, which is very common nowadays.
Materials
Some characters typed/copy-pasted.
Steps
Create .bat file with the following content.
/* >nul 2>&1
#echo off
setlocal
set exe=cw
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*csc.exe"') do set "csc=%%v"
"%csc%" -nologo -out:"%exe%.exe" "%~f0"
endlocal
exit /b %errorlevel%
*/
using System;
namespace cw {
class Program {
static void Main() {
var exe = Environment.GetCommandLineArgs()[0];
var rawCmd = Environment.CommandLine;
var line = rawCmd.Remove(rawCmd.IndexOf(exe),exe.Length).TrimStart('"');
line = line.Length < 2 ? "\r" : line.Substring(2) ;
Console.Write(line);
}
}
}
Run it.
Now you have a nice 4KB utility so you can delete the .bat.
Alternatively, you can insert this code as a subroutine in any batch, send the resulting .exe to %temp%, use it in your batch and delete it when you're done.
How to use
If you want write something without new line:
cw Whatever you want, even with "", but remember to escape ^|, ^^, ^&, etc. unless double-quoted, like in "| ^ &".
If you want a carriage return (going to the beginning of the line), run just
cw
So try this from command line:
for /l %a in (1,1,1000) do #(cw ^|&cw&cw /&cw&cw -&cw&cw \&cw)
From here
<nul set /p =Testing testing
and also to echo beginning with spaces use
echo.Message goes here
Maybe this is what your looking for, it's a old school script... :P
set nl=^& echo.
echo %nl%The%nl%new%nl%line%nl%is%nl%not%nl%apparent%nl%throughout%nl%text%nl%
echo only in prompt.
pause
or maybe your trying to replace a current line instead of writing to a new line?
you can experiment with this by removing the "%bs%" after the "." sign and also by spacing out the other "%bs%" after the "Example message".
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "bs=%%a"
<nul set /p=.%bs% Example message %bs%
pause
I find this really interesting because it uses a variable for a purpose other than what it is intended to do. as you can see the "%bs%" represents a backspace. The second "%bs%" uses the backspace to add spaces after the "Example message" to separate the "Pause command's output" without actually adding a visible character after the "Example message". However, this is also possible with a regular percentage sign.
Sample 1: This works and produces Exit code = 0. That is Good.
Note the "." , directly after echo.
C:\Users\phife.dog\gitrepos\1\repo_abc\scripts #
#echo.| set /p JUNK_VAR=This is a message displayed like Linux echo -n would display it ... & echo %ERRORLEVEL%
This is a message displayed like Linux echo -n would display it ... 0
Sample 2: This works but produces Exit code = 1. That is Bad.
Please note the lack of ".", after echo. That appears to be the difference.
C:\Users\phife.dog\gitrepos\1\repo_abc\scripts #
#echo | set /p JUNK_VAR=This is a message displayed like Linux echo -n would display it ... & echo %ERRORLEVEL%
This is a message displayed like Linux echo -n would display it ... 1
Inspired by the answers to this question, I made a simple counter batch script that keeps printing the progress value (0-100%) on the same line (overwritting the previous one). Maybe this will also be valuable to others looking for a similar solution.
Remark: The * are non-printable characters, these should be entered using [Alt + Numpad 0 + Numpad 8] key combination, which is the backspace character.
#ECHO OFF
FOR /L %%A in (0, 10, 100) DO (
ECHO|SET /P="****%%A%%"
CALL:Wait 1
)
GOTO:EOF
:Wait
SET /A "delay=%~1+1"
CALL PING 127.0.0.1 -n %delay% > NUL
GOTO:EOF
You can suppress the new line by using the set /p command. The set /p command does not recognize a space, for that you can use a dot and a backspace character to make it recognize it. You can also use a variable as a memory and store what you want to print in it, so that you can print the variable instead of the sentence. For example:
#echo off
setlocal enabledelayedexpansion
for /f %%a in ('"prompt $H & for %%b in (1) do rem"') do (set "bs=%%a")
cls
set "var=Hello World! :)"
set "x=0"
:loop
set "display=!var:~%x%,1!"
<nul set /p "print=.%bs%%display%"
ping -n 1 localhost >nul
set /a "x=%x% + 1"
if "!var:~%x%,1!" == "" goto end
goto loop
:end
echo.
pause
exit
In this way you can print anything without a new line. I have made the program to print the characters one by one, but you can use words too instead of characters by changing the loop.
In the above example I used "enabledelayedexpansion" so the set /p command does not recognize "!" character and prints a dot instead of that. I hope that you don't have the use of the exclamation mark "!" ;)
Use EchoX.EXE from the terrific "Shell Scripting Toolkit" by Bill Stewart
How to suppress the linefeed in a Windows Cmd script:
#Echo Off
Rem Print three Echos in one line of output
EchoX -n "Part 1 - "
EchoX -n "Part 2 - "
EchoX "Part 3"
Rem
gives:
Part 1 - Part 2 - Part 3
{empty line}
d:\Prompt>
The help for this usage is:
Usage: echox [-n] message
-n Do not skip to the next line.
message The text to be displayed.
The utility is smaller than 48K, and should live in your Path. More things it can do:- print text without moving to the next line- print text justified to the left, center, or right, within a certain width- print text with Tabs, Linefeeds, and Returns- print text in foreground and background colors
The Toolkit includes twelve more great scripting tricks.
The download page also hosts three other useful tool packages.
I found this simple one-line batch file called "EchoPart.bat" to be quite useful.
#echo | set /p=%*
I could then write something like the line below even on an interactive CMD line, or as part of a shortcut. It opens up a few new possibilities.
echopart "Hello, " & echopart "and then " & echo Goodbye
And if you're using it in batch files, the texts can be got from parameter variables instead of immutable strings. For instance:
#echopart Hello %* & #echo , how are you?
So that executing this line in "SayHello.bat" allows:
or even...
Have a play, and have fun!
I believe there's no such option. Alternatively you can try this
set text=Hello
set text=%text% world
echo %text%
Echo with preceding space and without newline
As stated by Pedro earlier, echo without new line and with preceding space works (provided "9" is a true [BackSpace]).
<nul set /p=.9 Hello everyone
I had some issues getting it to work in Windows 10 with the new console but managed the following way.
In CMD type:
echo .◘>bs.txt
I got "◘" by pressing [Alt] + [8]
(the actual symbol may vary depending upon codepage).
Then it's easy to copy the result from "bs.txt" using Notepad.exe to where it's needed.
#echo off
<nul set /p "_s=.◘ Hello everyone"
echo: here
With jscript:
#if (#X)==(#Y) #end /*
#cscript //E:JScript //nologo "%~nx0" %*
#exit /b %errorlevel%
*/if(WScript.Arguments.Count()>0) WScript.StdOut.Write(WScript.Arguments.Item(0));
if it is called write.bat you can test it like:
call write.bat string & echo _Another_String_
If you want to use powershell but with cmd defined variables you can use:
set str=_My_StrinG_
powershell "Write-Host -NoNewline ""%str%"""" & echo #Another#STRING#
Late answer here, but for anyone who needs to write special characters to a single line who find dbenham's answer to be about 80 lines too long and whose scripts may break (perhaps due to user-input) under the limitations of simply using set /p, it's probably easiest to just to pair your .bat or .cmd with a compiled C++ or C-language executable and then just cout or printf the characters. This will also allow you to easily write multiple times to one line if you're showing a sort of progress bar or something using characters, as OP apparently was.
I am launching a browser from batch file.
START "www.google.com"
I would like to know the PID of this browser window launched.
There can be many browser windows launched on a single machine. I need to find the PID of the process which was launched by my batch file only. I tried with WINDOWTITLE filter. But its not a good idea as titles may change in future. I am using Windows XP/7
Any help would be appreciated.
Thanks.
For what it worth (question is more than 2 years old) this code do the trick, just change variable according to default browser exe
set "browser=palemoon.exe"
tasklist /FI "imagename eq %browser%" /NH /FO csv > task-before.txt
start www.google.com
tasklist /FI "imagename eq %browser%" /NH /FO csv > task-after.txt
:: fc /L /LB1 test4-Before.txt test4-After.txt | find /I "%browser%"
for /f "delims=, tokens=2,*" %%A in ('"fc /L /LB1 task-before.txt task-after.txt | find /I "%browser%""') do set pid=%%A
SETLOCAL enabledelayedexpansion
pid=!pid:"=!
ENDLOCAL
echo pid is %pid%
This is just an idea, to get you maybe on the way
there is a command called Tasklist
there is a switch called filter /FI with lets you decide what filter parameters you want to output, f.e PID. Output this to a > 1.TXT
start your proces
recheck the watchlist and output to 2.TXT
Then you would have to get creative. COmpare 1 to 2,
maybe remove the processes in 1 from the 2.TXT
The remainig PID is what you wanted?
If you have some programming experience, you could create your own console application that accepts command-line parameters and passes them to the Win32 API CreateProcess() function. One of its output values is the spawned process ID, which your app could then return. Then just update your batch file to call your app instead of using START directly.
I'm trying to do the same thing. Though there must be some way of doing it, but all my Googling suggests not.
Check out robvanderwoude.com to see a list of 3rd party tools and examples. Also check out the full list of Sysinternal's process utilities here.
I've been looking at this for about 2 hours now and I think that there is a way to do this, but it requires some more insight on how windows handles iexplore.exe for PID...
I have a working version of a batch file I wrote that will get you what you want, BUT only if its the FIRST AND ONLY Internet Explorer Window open.
For some reason I can't get the PID to change when I open new browsers, but I can get results if there is no window open (obviously because there is no PID)
Anyhow, this is what I have... you should be able to run this on your system and it will tell you that there are no differences and it might actually produce results if your default browser is Firefox or Chrome or something... just need to make the changes to what I'm providing.
#echo off
IF EXIST c:\temp\pshell.txt del c:\temp\pshell.txt
IF EXIST C:\temp\PID1.txt del C:\temp\PID1.txt
IF EXIST C:\temp\PID2.txt del C:\temp\PID2.txt
IF EXIST C:\temp\PowerFormat.txt del C:\temp\PowerFormat.txt
powershell.exe Get-Process iexplore>C:\temp\pshell.txt
FOR /F "skip=3 tokens=7 delims= " %%1 IN ( c:\temp\pshell.txt ) DO #echo %%1>> C:\temp\PID1.txt
start "title" "www.google.com"
powershell.exe Get-Process iexplore>C:\temp\pshell.txt
FOR /F "skip=3 tokens=7 delims= " %%2 IN ( c:\temp\pshell.txt ) DO #echo %%2>> C:\temp\PID2.txt
FC /L c:\temp\pid1.txt c:\temp\pid2.txt> C:\temp\FileComparison.txt
FOR /F "tokens=7 delims=" %%3 IN (c:\temp\FileComparison.txt) DO #echo %%3>C:\temp\DiffPID.txt
FINDSTR "FC: no differences encountered" c:\temp\FileComparison.txt
IF '%ERRORLEVEL%'=='0' del C:\temp\FileComparison.txt & echo.No PID Found
IF NOT '%ERRORLEVEL%'=='0' type c:\temp\FileComparison.txt
pause
exit
Let me know if this helps...