I'm using the following code to check whether there is a java process with the specific name.
for /f "tokens=1" %i in ('jps -m ^| find "TouchPosApplication"') do ( EXIT /B )
how can I use this as an if condition rather than iterating. and this command exits the batch code even if there is no process named such. is there any thing wrong with this code.
This is my entire batch script
start start-chrome.bat
for /f "tokens=1" %i in ('jps -m ^| find "TouchPosApplication"') do ( EXIT /B )
ant main
set var=False
start start-chrome.bat
for /f "tokens=1" %%i in ('jps -m ^| find "TouchPosApplication"') do set var=True
if "%var%"=="True" (
Echo Found
) else (
ant main
)
I'm on phone so can't test it but
goto label
will jump to and execute ant main whereas goto finish would end program.
label:
ant main
finish:
PS. I don't recall batch accepting single quotes, which are apostrophes really.
Related
I am trying to retrieve the ProcessID of a Java app launched through a batch file. The problem is that the application does not start.
My code:
set cmd=java -jar XXXXXXX.jar XXXXXX.yml
for /f "tokens=2 delims==; " %%a in (' wmic process call create "%cmd%" ^| find "ProcessId" ') do set PID=%%a
start cmd /k echo %pid%
PAUSE
Batch files are pretty picky with whitespace.
Change this:
set cmd = java -jar XXXXXXX.jar XXXXXX.yml
To this:
set cmd=java -jar XXXXXXX.jar XXXXXX.yml
This works:
set cmd=notepad
for /f "tokens=2 delims==; " %%a in (' wmic process call create "%cmd%" ^| find "ProcessId" ') do set PID=%%a
start cmd /k echo %pid%
PAUSE
There may be something wrong with your jar file or perhaps java is not on your PATH. Try running your command directly at a command prompt to see if it works:
c:\>java -jar XXXXXXX.jar XXXXXX.yml
Also make sure that you use the full path to your jar file in your cmd otherwise the path will be relative to your system folder:
set cmd=java -jar c:\yourpath\XXXXXXX.jar XXXXXX.yml
This is irritating. I am trying to incorporate a trigger in a script to run if the major version of Java is 1.6,1.7, etc.
In the line I am trying to set the variable in, I pull the Java version and pipe it to for with tokens and delims identified, resulting in the "do" as a set command for variable %jver%. However, echoing %jver% results in "echo is on". Why wont it set the variable? Everything looks legit until %jver% is used.
Yes, I double the percentages for the script. The code here is for use at the command prompt.
Here is the line:
%systemroot%\system32\java.exe -version 2>&1 | for /f "tokens=1-4 delims=. " %a in ('findstr /i "version"') do (set jver=%~c.%d)
#rao thank you for helping me discover an alternate solution to meet my intent.
My workaround solution to this is the following line:
for /f "tokens=1-4 delims=. " %a in ('java -version 2^>^&1 ^| findstr /i "version"') do (SET JVER=%~c.%d)
Looks this is answered in this post by Patrick Cuff
Here is the solution adding from mentioned original post
#echo off
setlocal
set VERSION6="1.6.0_21"
for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (
#echo Output: %%g
set JAVAVER=%%g
)
set JAVAVER=%JAVAVER:"=%
#echo Output: %JAVAVER%
I am trying to iterate all the registry key in order to find (contains) and delete jre1.5.0_14 values. Is there a way to do it?
The code below just finds jre1.5.0_14 under a specific key! I do want to iterate all of the keys. By the way if clause gets if it is equal to jre1.5.0_14, but it should be if it contains jre1.5.0_14.
Thanks in advance.
Best Regards.
#echo off
setlocal
set KEY_NAME="HKEY_CURRENT_USER\Software\Microsoft\Notepad"
set VALUE_NAME=jre1.5.0_14
FOR /F "skip=2 tokens=3" %%A IN ('REG QUERY %KEY_NAME%') DO if "%%A"=="%VALUE_NAME%" (
#echo %%A
)
I am posting this per your last comment. You (probably) need to go through all the keys, and you don't want /d at the end.
If you know the string is in a specific key, use this (which I think is what you were originally after):
For /f %%a in ('reg query HKCR /f jre1.5.0_14') do (
rem do deletion here
echo %%a &&pause
)
Use the following to look through all the keys. It runs the query on each key and if "hkey" is found in the output (indicating the query turned up something) it writes the line--the location--to regout.txt. The second for loop goes through each line of the file, where you can do a reg delete command.
#echo off
SETLOCAL EnableDelayedExpansion
del regout.txt >NUL 2>&1
for %%a in (HKCU, HKLM, HKU, HKCR) do REG QUERY %%a /f jre1.5.0_14 | FIND /i "hkey" >>regout.txt
for /f %%a in (regout.txt) do (
rem do deletion here
echo %%a &&pause
)
exit
If the file is empty the string wasn't found and you should run regedit in Windows again to confirm it's there.
Edit: Attempt to resolve "The process can not access the file because the file is being used by another process . . .
Try this code: It uses different file names and creates a tmp file with query output, then copies it to txt. The new txt file should definitely be unlocked.
#echo off
SETLOCAL EnableDelayedExpansion
del regout.txt >Nul 2>&1
del regout.tmp >Nul 2>&1
del output.txt >Nul 2>&1
for %%a in (HKCU, HKLM, HKU, HKCR) do REG QUERY %%a /f jre1.5.0_14 | FIND /i "hkey" >>output.tmp
copy output.tmp output.txt /y
for /f %%a in (output.txt) do (
rem do deletion here
echo %%a &&pause
del output.tmp
)
exit
I have a set of files, labeled file1.java, file2.java, ...
I want to pipe the output of the files to .txt files, such as output1.txt, output2.txt, etc. corresponding to each java file.
I know that I can do this by doing:
javac file1.java
java file1 > output1.txt
However, is there a way to do this of all files in a given directory? (instead of doing it manually)
This is a good for loop example:
Command line usage:
for /f %f in ('dir /b c:\') do echo %f
Batch file usage:
for /f %%f in ('dir /b c:\') do echo %%f
if the directory contains files with space in the names, you need to change the delimiter the for /f command is using. for example, you can use the pipe char.
for /f "delims=|" %%f in ('dir /b c:\') do echo %%f
If the directory name itself has a space in the name, you can use the usebackq option on the for:
for /f "usebackq delims=|" %%f in (`dir /b "c:\program files"`) do echo %%f
And if you need to use output redirection or command piping, use the escape char (^):
for /f "usebackq delims=|" %%f in (dir /b "c:\program files" ^|
findstr /i microsoft) do echo %%f
It shows both command line + batch for loop examples. Then you will have the filenames and can just run some commands on those filenames like javac and java.
Just try to break it down to small tasks:
Get list of files (all or just .class)
Loop through each file and call javac and java on them
Google is your best friend - search for "DOS batch" + what you need
Your job is much easier if you restructure your output file names a bit: file1.java --> file1_output.txt
From the command line:
for %F in (*.java) do #javac "%F" >"%~nF_output.txt"
If in a batch file:
#echo off
for %%F in (*.java) do javac "%%F" >"%%~nF_output.txt"
If you keep your original naming convention, then you must parse out the number form each file name. Very doable, but it doesn't seem worthwhile in your case.
I need to know java process PID from Windows batch console.
#echo off
set p=%CD%
FOR /F "tokens=1" %%A IN ('"%JAVA_HOME%/bin/jps.exe -v"\|find "%p%"') DO SET str=%%A
echo str = "%str%"
Java process unique identifier is path from what it was executed.
Script executes jps, that returns all java process information, for example
9376 Jps -Denv.class.path=D:\tools\timesten\lib\ttjdbc6.jar; -Dapplication.home=C:\Program Files\Java\jdk1.6.0_24 -Xms8m
3856 -Dexe4j.semaphoreName=c:_program files (x86)_jetbrains_intellij idea community edition 12.0.1_bin_idea.exe -Dexe4j.moduleName=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 12....etc
Batch says that:
| was unexpected at this time.
Could you please said , how to correctly extract PID.
You have to escape the pipe like this ^| within a FOR statement, otherwise it tries to pipe the first half of the FOR statement into the second.
Also this is how I would find a PID.
for /f "tokens=2" %%a in ('tasklist ^| find "jps.exe") do set javapid=%%a
You can user command "tasklist" for show PID。
Correct script
"%JAVA_HOME%/bin/jps.exe" -v>temp.txt
for /f "tokens=1" %%f in ('find "%CD%" "temp.txt"') do set str=%%f
echo str=%str%