Command to fetch column information - batch-file

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%

Related

Batch File: Setting Variable from second output line error with space on output "Was Unexpected at this time"

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.

.NET Framework installation only in case it is not installed

I downloaded the .NET offline installer from: http://go.microsoft.com/fwlink/?LinkId=852107
How can I make it install the .NET 4.7.1 only in case it wasn't previously installed?
Cause when I run:
NDP471-KB4033342-x86-x64-AllOS-ENU.exe /passive
It tries to install it although I already have the .NET 4.7.1 installed on my computer.
There's a help switch for the tool, but none of the commands seem to fit my needs:
---------------------------
Microsoft .NET Framework
---------------------------
Usage: Setup [switches]
All switches are optional.
/CEIPconsent - Optionally send anonymous feedback to improve the customer experience.
/chainingpackage <name> - Optionally record the name of a package chaining this one.
/createlayout <full path> - Download all files and associated resources to the specified location. Perform no other action. * Disabled *
/lcid - Set the display language to be used by this program, if possible. Example: /lcid 1031
/log <file | folder> - Location of the log file. Default is the process temporary folder with a name based on the package.
/msioptions - Specify options to be passed for .msi and .msp items. Example: /msioptions "PROPERTY1='Value'"
/norestart - If the operation requires a reboot to complete, Setup should neither prompt nor cause a reboot.
/passive - Shows progress bar advancing but requires no user interaction.
/showfinalerror - Passive mode only: shows final page if the install is not successful.
/showrmui - Passive mode only: shows restart manager dialog if files are in use.
/pipe <name> - Optionally create a communication channel to allow a chaining package to get progress.
/promptrestart - If the operation requires a reboot to complete, Setup should prompt, and trigger it if the user agrees.
/q - Quiet mode, no user input required or output shown.
/repair - Repair the payloads.
/serialdownload - Force install operation to happen only after all the payload is downloaded.
/uninstall - Uninstall the payloads.
/parameterfolder <full path> - Specifies the path to the Setup’s configuration and data files.
/NoSetupVersionCheck - Do not check ParameterInfo.xml for setup version conflicts.
/uninstallpatch {patch code} - Removes update for all products the patch has been applied to.
/? - Display this help.
Examples:
Silently install the package and create log file SP123.htm in the temp folder: Setup /q /log %temp%\SP123.htm
Install with no user interaction unless reboot is needed to complete the operation: Setup /passive /promptrestart
Some command line switches are disabled for this package: createlayout
(c) Microsoft Corporation. All Rights Reserved.
---------------------------
OK
---------------------------
Whilst this doesn't answer your question it may be of use to you or future readers.
It is an untested batch file intended to determine the installed versions of .Net Framework from reading the registry:
#Echo Off
SetLocal EnableDelayedExpansion
Set "rk=HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP"
Set/A "vi=ri=0"
For /F "Tokens=3*" %%A In ('Reg Query "%rk%" /F v /K') Do If "%%B"=="" (
If Not "%%~xA"=="" (Set/A "vi+=1"
For /F "Tokens=2*" %%C In (
'Reg Query "%rk%\%%~nxA" /V Version 2^>Nul^|Find /V "\"'
) Do Set "_v!vi!=%%D"
) Else (Set/A "ri+=1"
For /F "Tokens=2*" %%E In (
'Reg Query "%rk%\%%~nxA\Full" /V Release 2^>Nul^|Find /V "\"'
) Do Set/A "_r!ri!=%%F"))
If %ri% Gtr 0 (Set/A "vi+=1"
For /F "Tokens=2" %%A In ('FindStr/B "!_r%ri%!" "%~f0"'
) Do Set "_v%vi%=%%A")
If %vi% Gtr 0 For /F "Tokens=1* Delims==" %%A In ('Set _v') Do Echo([%%B]
Timeout -1
GoTo :EOF
Rem Later Version Table - do not remove
378389 4.5
378675 4.5.1
378758 4.5.1
379893 4.5.2
393295 4.6
393297 4.6
394254 4.6.1
394271 4.6.1
394802 4.6.2
394806 4.6.2
460798 4.7
460805 4.7
461308 4.7.1
461310 4.7.1
461808 4.7.2
461814 4.7.2
528040 4.8
528049 4.8
528372 4.8
Future Release dWord values can be added to the bottom of the table following the same format: 'dWordValueSPACE.NETVersion'

How to find If Java is installed or not using batch file?

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.

Batch for loop, Appending to text file, error, Reg Query, Win XP vs Win 7

Problem and Current Output: %file3% (%~dp0%computername%-programs_unsorted.txt) is not created on a computer with Win XP x86. However %file3% is created on one with Win 7 x86.
Need %file3% created with the below output (for example):
Adobe AIR
Adobe AIR
Adobe Flash Player 13 ActiveX
Adobe Reader XI (11.0.07)
Adobe Shockwave Player 12.1
Cisco AnyConnect Network Access Manager
Cisco AnyConnect Secure Mobility Client
Cisco AnyConnect Secure Mobility Client
Cisco Jabber Video for TelePresence
Citrix Authentication Manager
Batch code:
#echo OFF
setlocal enableextensions enabledelayedexpansion
set "Version_tool=v39"
SET "file3=%~dp0%computername%-programs_unsorted.txt"
If Exist %file3% Del %file3%
Rem -------- Win XP x86 commands ---------
:WinXPx86
echo Win XP x86...
for /f "tokens=* delims=" %%A in (
'REG Query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /S ^| find "DisplayName"'
) do echo %%A %%B %%C %%D %%E>> %file3%
pause
for /f "tokens=* delims=" %%A in (
'REG Query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /S ^| find "QuietDisplayName"'
) do echo %%A %%B %%C %%D %%E>> %file3%
echo %computername%-programs_unsorted.txt created ...
pause
goto Continue
This batch snippet doesn't work as described on either XP or Win7. I set a value for %file3% so I could run this on my systems.
%file3% never gets written for me on either version of Windows as long as delims is blank ("delims="). If I stick a space after the equals sign ("delims= "), I start getting output as follows.
On both Windows versions, I get a file that looks like this:
DisplayName REG_SZ Microsoft Help Viewer 1.1 %B %C %D %E
DisplayName REG_SZ Microsoft Team Foundation Server 2010 Object Model - ENU %B %C %D %E
DisplayName REG_SZ Microsoft Visual Studio 2010 Tools for Office Runtime (x64) %B %C %D %E
DisplayName REG_SZ Intel(R) Network Connections Drivers %B %C %D %E
DisplayName REG_SZ Microsoft Sync Framework Services v1.0 SP1 (x64) %B %C %D %E
...
%%A is holding the value of the complete line because "tokens=*" means that the whole line is a token. From the FOR command line help:
If the last character in the tokens= string is an asterisk, then an
additional variable is allocated and receives the remaining text on
the line after the last token parsed.
So each whole line is stored in %%A and the other variables just get spit out as their own names.
To achieve the output you described, change your FOR commands. Add space as a delimiter and change the tokens you want. In XP, use "tokens=2,*" and in Win7 use "tokens=3,*". Here is the XP example that works:
for /f "tokens=2,* delims= " %%A in (
'REG Query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /S ^| find "DisplayName"'
) do echo %%A %%B>> %file3%
When I make these changes, the program output looks like this in both XP and Win7:
Microsoft Help Viewer 1.1
Microsoft Team Foundation Server 2010 Object Model - ENU
Microsoft Visual Studio 2010 Tools for Office Runtime (x64)
Intel(R) Network Connections Drivers
Microsoft Sync Framework Services v1.0 SP1 (x64)
On XP, tons of Security Updates are listed. And "ParentDisplayName" is also being matched by your find filter on XP. Don't know if you want to amend that situation. It lists a lot of duplicates.

How to get the current version of jre installed using batch file

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

Resources