I'm trying to put the result of:
"WMIC /Node:localhost /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct get /value | find "displayName=Symantec""
into a variable so that I can then ECHO the result.
Could you please advise?
After I spent some more time on this I found the answer. Only a "^" was needed. So command looks like:
FOR /F "tokens=*" %A IN ('WMIC /Node:localhost /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct get /value^| find "displayName=Symantec"') DO (SET HOST=%A)
Then if you echo HOST -> you got it (or I got it).
Thanks anyway :D
Related
I know similar questions have already been asked, but somehow I am unable to figure out the mistake in my code.
I'm making a .bat file with the following code
echo off
echo %cd%
set curr_directory = "%cd%"
echo $curr_directory
pause
OUTPUT is :
C:\Users\MyDesktop>echo off
C:\Users\MyDesktop>
$curr_directory
Press any key to continue . . .
So what I dont get is why the value of variable curr_directory is not being printed.
What i eventually want to do is use the variable to change the directory something like this: cd $curr_directory
Thanks
I don't know where to start. EVERYTHING about your code is wrong. This should work:
#echo off
echo %cd%
set curr_directory=%cd%
echo %curr_directory%
pause
In batch you access variables via %var% and not $var. Further, DO NOT PUT SPACES behind =. SET x=123 will store 123 in x but SET x= 123 will store _123 (_ means space) in x.
EDIT: As SomethingDark stated, the first line should be #echo off except you actually want the message echo off to be printed. And yes, SET x = 123 means %x % is 123
use %curr_directory% instead of $curr_directory
Avoid spaces inbetween like the below one
"set curr_directory = %cd%"
below should work
echo off
echo %cd%
set curr_directory=%cd%
echo curr_directory is %curr_directory%
pause
I need a batch command to return everything until after a certain substring.
What I mean, is when I have a string like this: "Hi! How are you doing? I don't care!!!!!" I can execute a command that gives me everything until after "?".
I looked around the web and didn't find anything that I wanted. I found one method that took everything until after a substring and changed it:
set name=123456789
set blablabla=%name:*5=5%
This returns "56789" to the variable blablabla. The strings in my program are not going to be specific, so this won't work.
Thank you for any help!
Use sed and regular expressions:
$ string1='Hello my name is Foobar? What do I care!'
$ string2=$(echo $string1 | sed 's/^.*\? //')
$ echo $string2
What do I care!
I have not understood your requirements fully. But I think PowerShell is a better option for this. However, you can use following script to get all characters upto given delimiter.
#echo off
set delim=%1
set input=%2
for /f "delims=%upto%" %%i in ("%input%") do (
echo %i
goto :eof
)
Sample run command:
stringupto.bat 5 123456789
Output:
1234
I have a log file, that prefixes all lines in it with the date and time. For instance:
2015-02-04 16:11 Error Message: Important Bit Useless Information.
I wish to write a batchfile that when run picks out all the lines from the last hour and then counts how many of these lines contain "Important Bit" and then output this count if it's greater than a given value.
The part I am having difficulty with is basically the main part. I have set up the batchfile, it finds the date, finds the last ten minute period. I cannot then get the file to do the above.
My current command for this is as below, where date and now are assigned to the current date and time:
set count="findstr /G:“%date% %now%” “C:\Documents\File.txt” | find /C "Important Bit""
If %count% geq 5 echo Found %count% lines >> C:\Documents\Output.txt
When I run this, as far as I can tell, nothing happens. I've tried using a FOR command as well, but this still wouldn't work.
Any suggestions are greatly appreciated!
Thanks
Use something like the following:
set /a COUNT=0
for /f %%i in ('findstr /i /c:"Important Bit" test.txt') do (
set /a COUNT=COUNT + 1
)
echo %COUNT%
I try to set a batch variable to an output of another command. In Linux/Unix you can simply use backticks, e.g. (in csh)
set MY_VAR = `tail /etc/passwd`
Is there something similar available in windows batch?
Actually I found already something but it is not fully working:
d:\>for /F "skip=1" %n in ('wmic OS Get CurrentTimeZone') do set TimeZone=%n
d:\>set TimeZone=120
:\>set TimeZone=
d:\>
The problem is the wmic commands returns several lines, otherwise it would work fine. The first I know to skip, however I did not manage to skip the second empty line. I tried with IF but no success.
yes - the output of wmic is a bit ugly to handle.
Use a trick: search for a number in the ouput (findstr "[0-9] will only return lines, that contain a number):
for /F %n in ('wmic OS Get CurrentTimeZone ^|findstr "[0-9]"') do set TimeZone=%n
echo Timezone is %TimeZone%.
(for use in a batchfile use %%n instead of %n)
Another way is:
for /F %n in ('wmic OS Get CurrentTimeZone') do if not defined TimeZone set TimeZone=%n
EDIT:
I prefer the first version, as findstr (or find) converts the wmic-line-endings, so the second for mentioned by MC ND is not neccessary.
I suggest following batch code:
#echo off
for /F "skip=1" %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS Get CurrentTimeZone') do (
set "TimeZone=%%I"
goto BelowLoop
)
:BelowLoop
echo Time zone difference is: %TimeZone%
The FOR loop is exited with command GOTO after the value of interest is assigned to environment variable TimeZone.
The entire FOR loop can be optimized to a single command line:
#echo off
for /F "skip=1" %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS Get CurrentTimeZone') do set "TimeZone=%%I" & goto BelowLoop
:BelowLoop
echo Time zone difference is: %TimeZone%
Exiting the FOR loop after having the value of interest avoids the problem with wrong parsing of Unicode (UTF-16 Little Endian) encoded output of WMIC by FOR which otherwise would result in deleting the environment variable TimeZone. For details on wrong parsing of Unicode output by FOR see answer on How to correct variable overwriting misbehavior when parsing output?
for /f "tokens=2 delims==" %a in ('wmic OS get CurrentTimeZone /value') do set "timeZone=%a"
(to use in a batch file, remember to double the percent signs)
The added /value in wmic changes its output to key=value format. The delims clause in for command indicates a = as a separator. The tokens clause ask to retrieve only the second token/field in the line. As the only line with two tokens is the line with the required data, only this line is processed.
BUT, wmic output includes an aditional carriage return at the end of its output, that needs to be removed from the variable. An aditional for command can be used. The resulting command will be
for /f "tokens=2 delims==" %a in ('wmic OS get CurrentTimeZone /value') do for /f %b in ("%a") do set "timeZone=%b"
Or, for a batch file
for /f "tokens=2 delims==" %%a in (
'wmic OS get CurrentTimeZone /value'
) do for /f %%b in ("%%a") do set "timeZone=%%b"
echo %timeZone%
I tried to use the tasklist command in cmd but it was not listed in there.
I also notice that the process is a little indented in task manager together with another process called wowexec.exe.
Any way to get the PID of the process? For reasons of hex editing.
wmic is nice for running sql-like queries to get the information you need. Replace wowexec in the following example with something resembling the task name of your VB3 process.
for /f "tokens=2 delims==" %%I in ('wmic process where "name like '%%wowexec%%'" get processid /format:list') do set "PID=%%I"
Something like that is what you would put in a batch script.
If you're just running this from a cmd console, use %I instead of %%I, and do #echo %I instead of do set etc.
for /f "tokens=2 delims==" %I in ('wmic process where "name like '%%wowexec%%'" get processid /format:list') do #echo %I
Note: The double percents around wowexec signify literal percent symbols, a SQL syntax wildcard character. wowexec is not a variable, but a literal string.