Well I am writing a batch file where i need to get the current version of the installed jre in my system and then make a decision based on that- I am aware that i can get the current version by querying the registry. The following command gives the below output-
reg query "HKLM\Software\JavaSoft\Java Runtime Environment"
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment
Java7FamilyVersion REG_SZ 1.7.0
CurrentVersion REG_SZ 1.7
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\1.7
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\1.7.0
But i am unable to get the value 1.7 parsing this output using the
for /f "tokens=3" %%i in ('Command') do echo %%i
I want only the "1.7" specific to the current Version and then compare it and make a decision. I am not able to understand what option will get that.
Anyone good in batch commands can please help me out on this ? Any help will be appreciated. I also tried to parse the output of java -version and get the value.
You can use the "java -version" command and filter/parse the output.
rem Taking "java -version" output and filter the line containing "Runtime Environme
nt". Taking the the 6th word and echo the part before "_"
for /f "tokens=1-10" %%a in ('java -version 2^>^&1 ^| find "Runtime Environme
nt"') do for /f "delims=_" %%x in ('echo %%f') do echo %%x
Related
I have been fighting with this for 2 hours now. I need to set a variable from a second line output of a command. I found this code here at stackoverfglow:
for /f "skip=1delims=" %%a in (
'%SystemRoot%\System32\wbem\wmic.exe CSPRODUCT GET NAME'
) do set sid=%%a&goto next
The output for wmic.exe CSPRODUCT GET NAME is:
Name
PowerEdge T610
but when I run the batch file I get:
>set sid=PowerEdge T610
T610 was unexpected at this time.
I tried putting quotes everywhere.
I'm doing this script to check if the computer is a VirtualBox o VMware machine, the idea is to set a variable with the output of that command then compare it to "VirtualBox". But physical computers have product name with spaces.
THANKS!
I would offer the following method, as it will capture the full string without any potentially unwanted trailing space characters:
#For /F Tokens^=6Delims^=^" %%G In ('""%__AppDir__%wbem\WMIC.exe" CSProduct Get Name /Format:"MOF" 2>Nul"')Do #Set "SID=%%G"
Note: If you are using Windows 7, the use of some output formats may be broken, as the relevant .xsl stylesheets are located inside a language specific subdirectory which is not read by default. This issue can be worked around, by copying/moving or creating links to, all of the .xsl files one level higher, i.e. inside the wbem directory. (see potential alternative fix at the foot of this answer)
Alternatively, you could omit the for-loop completely, and instead of defining a variable, just use conditional statements:
#("%__AppDir__%wbem\WMIC.exe" CSProduct Where "Name Like '%%VirtualBox%%' Or Name Like '%%VMWare%%'" List Instance /Format:"List" 2>NUL|"%__AppDir__%find.exe" "__">NUL&&(Echo Virtual)||Echo Physical)&Pause
Note: In this example please ensure that you replace VirtualBox and/or VMWare as/if necessary. You can obviously replace the respective Echo commands with your chosen commands after testing.
Alternatively, for the actual process you need it for, based upon your comments, the following should also suffice:
#("%__AppDir__%wbem\WMIC.exe" ComputerSystem Where "Model Like '%%Virtual%%'" Get /Value 2>NUL|"%__AppDir__%find.exe" "=">NUL&&(Echo Virtual)||Echo Physical)&Pause
This should pick up a Hyper-V machine, (Model:Virtual Machine), a VMware machine, (Model:VMware Virtual Platform), and an Oracle VM, (Model:VirtualBox).
Here is a modified batch-file, which is intended to fix the potential Format issue in windows-7, as mentioned in my first example above:
#Echo Off
Set "XSL=MOF"
For /F "Delims==" %%G In ('"Set SID 2>NUL"')Do Set "%%G="
For /F "EOL=MDelims=" %%G In (
'""%__AppDir__%wbem\WMIC.exe" OS Get MUILanguages,Version 2>NUL"'
)Do For /F Tokens^=2^,4-5Delims^=.^"^ %%H In ("%%G"
)Do If %%I Equ 6 If %%J Equ 1 (Call :Target "%__APPDIR__%wbem\%%H\%XSL%"
)Else Call :Target "%XSL%"
Set SID 2>NUL&&Pause
GoTo :EOF
:Target
For /F Tokens^=6Delims^=^" %%G In (
'""%__AppDir__%wbem\WMIC.exe" CSProduct Get Name /Format:"%~1" 2>Nul"'
)Do Set "SID=%%G"
Line 9 is where you'd place your code, replacing mine which was added just for demonstration purposes.
I am trying to fetch if java is installed or not in a system using batch file.
The below code gives me the version if Java is already present. If Java is not present the code fails to execute
for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (
Set VERSION=%%g
)
How can I detect that Java is not installed?
Assuming that any version output you require is on the first line:
#Echo Off
Set "JV="
For /F "Tokens=3" %%A In ('java -version 2^>^&1') Do If Not Defined JV Set "JV=%%~A"
If /I "%JV%"=="not" (Echo Java is not installed) Else Echo Java Version "%JV%"
Pause
As a side note, this does not tell you if java is not installed, it tells you whether the java executable is available in the current directory or %PATH%. Also note that this uses your stated phrase 'java' is not recognized as an internal or external command, operable program or batch file meaning that this script is probably language dependent.
I'm having difficulty getting a command to run in a For Loop. From a cmd prompt this works fine:
"C:\Program Files (x86)\MakeMKV\makemkvcon64.exe" -r info | find "E:"
But, the following seems to have a syntax problem
for /f "tokens=1-7 delims=," %%A in ('"C:\Program Files (x86)\MakeMKV\makemkvcon64.exe" ^-r info ^| find "E:"') do (set _DRVID=%%A)
Yielding an error - C:\Program' is not recognized as an internal or external command, operable program or batch file.
Just for reference, I found a way to get my desired result with the following:
I can get the following code to work in Batch as well generating the desired output:
for /f "tokens=1-7 delims=," %%A in ('"C:\Program Files (x86)\MakeMKV\makemkvcon64.exe" ^-r info') do (IF %%G=="E:" set _DRVID=%%A)
What am I doing wrong?
when piping, both sides of a pipe are executed in an own instance of cmd. This makes quoting and escaping look non-intuitive. Try:
for /f "tokens=1-7 delims=," %%A in ('""C:\Program Files (x86^)\MakeMKV\makemkvcon64.exe" -r info | find "E:""') do (set _DRVID=%%A)
The best work-around in my opinion is to place a pair of quoted quotation marks around the expression:
for /f "tokens=1-7 delims=," %%A in ('^""C:\Program Files (x86)\MakeMKV\makemkvcon64.exe" ^-r info ^| find "E:"^"') do (set "_DRVID=%%~A")
After for /F consumes the enclosing '', the cmd instance executing the expression removes the outer-most "", so the remaining command line appears odd and therefore fails to be executed:
C:\Program Files (x86)\MakeMKV\makemkvcon64.exe" ^-r info ^| find "E:
An extra pair of "" fixed that, but then you need to change escaping. Adding a pair of escaped quotes ^"^" lets the code work without having to change escaping. This is therefore the most general solution.
Hmm - after a little experimentation, I got this (or my version) to work:
for /f "delims=" %%X in ('"C:\Program Files (x86)\MakeMKV\makemkvcon64.exe" ^-r') do (
for /f "tokens=1-7 delims=," %%A in ('ECHO(%%X^| find "E:"') do (set _DRVID=%%A)
)
But I've not been able to get the output of a quoted-executablename to be filtered as expected by the find.
I tried to make the title as descriptive as possible, but im basically trying to locate javaw.exe because when I launch my JAR cmd only finds java.exe but the prompt window is ugly and I don't want it there.
What works:
java -jar myJar.jar
What I'm trying to do
javaw -jar myJar.jar
What also works:
JAVAWPATH\javaw.exe -jar myJar.jar
I'm trying to make the program adapt to any computer automatically, since I don't know if javaw will always be in the same location.
So I have managed to locate a bunch of "javaw.exe" files using
WHERE /R c:\ *javaw.exe
I want to select one of the returned paths and set some kind of string variable with which I can then: elevate MYstringPATHtoJAVAW -jar myJar.jar.
I've written a batch script which locates the java instalation folder through the registry:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#ECHO OFF
:: Export java settings from registry to a temporary file
START /W REGEDIT /E %Temp%.\java.reg "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft"
:: Find java location
FOR /F "tokens=1* delims==" %%A IN ('TYPE %Temp%.\java.reg ^| FIND "INSTALLDIR"') DO SET JAVA_HOME=%%B
SET JAVA_HOME=%JAVA_HOME:"=%
SET JAVA_HOME=%JAVA_HOME:\\=\%
SET JAVA_HOME
:: Get java version
FOR /F "tokens=1* delims==" %%A IN ('TYPE %Temp%.\java.reg ^| FIND "CurrentVersion"') DO SET JAVA_VERSION=%%B
SET JAVA_VERSION=%JAVA_VERSION:"=%
SET JAVA_VERSION
SET JAVA_VERSION=%JAVA_VERSION:.=%
SET JAVA_VERSION=%JAVA_VERSION:_=%
SET /A JAVA_VERSION=%JAVA_VERSION%
:: Delete temp file
#DEL %Temp%.\java.reg /S /Q > NUL 2>&1
:: Check java version compatibility
IF %JAVA_VERSION% LSS 16020 (
ECHO.
ECHO YOU NEED AT LEAST JAVA WITH VERSION 1.6.0_20 -- this is just an example echo.
GOTO :EOF
)
PAUSE
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I've used this as a base.But it might not work properly if the installed java is a 64bit (yes, I must update this script...) , but it can be updated easy.Hope this will help you.
I have a command incmd version which produces below output
All Rights Reserved.
This Software is protected by U.S. Patent Numbers 5,794,246; 6,014,670; 6,016,50
1; 6,029,178; 6,032,158; 6,035,307; 6,044,374; 6,092,086; 6,208,990; 6,339,775;
6,640,226; 6,789,096; 6,820,077; 6,823,373; 6,850,947; 6,895,471; 7,117,215; 7,1
62,643; 7,254,590; 7,281,001; 7,421,458; 7,496,588; 7,523,121; 7,584,422; 7,720,
842; 7,721,270; and 7,774,791, international Patents and other Patents Pending.
Version: 9.1.0 HotFix2
Build: 1668 - 2011-Sep-02
Command ran successfully.
I want to fetch 9.1.0 out of it. so far using findstr "Version" i only get Version: 9.1.0 HotFix2 Is there a way I can extract column information?
Note: Without using any third party tools.. (anytool that is available by default in windows is fine)
You can use for to tokenize that output:
for /f "tokens=2, delims= " %v in ('incmd version ^| findstr Version') do #set Version=%v
echo %Version%
See help for for a detailed explanation of the command and its options.
Note that if you are using this in a batch file (which I assumed at first, but apparently wasn't the case) you have to double the percent signs in the for variable:
for /f "tokens=2, delims= " %%v in ('incmd version ^| findstr Version') do #set Version=%%v
echo %Version%