CMD - How do I run a command twice in one line? [duplicate] - batch-file

This question already has answers here:
Can I search for multiple strings in one "find" command in batch script?
(3 answers)
Closed 4 days ago.
How do I display both the host name and boot time?
The code I'm trying:
systeminfo | find /i "host name" && "boot time"
I know I could use
systeminfo | find /i "host name"
systeminfo | find /i "boot time"
but I really don't want to.
Basically I want the command to display the host name and boot time and ignore all else. How do I do that on one line? I know I'm missing something super simple here.

I have faced a similar problem before, and I may be able to help you out. If I understand your question correctly, you're looking for a way to display both the host name and boot time on one line, using a single prompt. In that case, the following command should do the trick, ignore the curly brackets of course:
{systeminfo | find /i "host name" & systeminfo | find /i "boot time"}
Using the & character will execute the two systeminfo commands sequentially and output the results on the same line.
I wish you the best of luck with your project. Let me know if I can help with anything else.

I would strongly advise against running the slowest utility on Windows, systeminfo.exe, just to get this type of information.
You could, if you wish try to use a less intensive, and quicker utility, net.exe.
You should also try to not to expect utilities to output strings in a specific language.
Example:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "CN=" & For /F "EOL= Delims=" %%G In ('(%SystemRoot%\System32\net.exe
Stats Srv 2^>NUL ^|^| %SystemRoot%\System32\net.exe Stats Work^) ^|
%SystemRoot%\System32\findstr.exe /ELV "."') Do If Not Defined CN (
Set "CN=%%~nxG") Else For %%H In (%%G) Do (SetLocal EnabledelayedExpansion
For %%I In ("!BT!") Do EndLocal & Set "BD=%%~I" & Set "BT=%%H")
If Defined CN Echo %CN% was booted on %BD% at %BT%& Pause

Related

Utilize a command in batch to change the Output language of the CMD

I wrote a batch script and everything ran fine, however I encountered problems when I transfered the script to another computer which uses the cmd in english and I no longer could utilize the script as it looked for specific words in the german cmd output.
I currently have it set to english, and I was trying to figure out if there was a way for me to use a command which can be added at the beginning of a batch script to either check the language and change it or just ultimately change it.
On google I found something about "chcp" but that didnt seem to help either. If anyone knows of anyway to solve my problem I deeply thank you.
P.S. I am trying to change it to german if that helps.
https://github.com/Pedrov01/PingsCommandOrganiser
Feel free to ask any questions if needed!
-Kind regards
It's not possible to change the language if the other language is not installed. So you have to write your code language-independent. The following takes advantage that the hostname is immediately followed by the IPaddress (not sure if it's the same in all languages (although I guess it is) - tested with German and English):
language dependent text HOSTNAME [IPADDRESS] language dependent text
Code:
#ECHO OFF
setlocal
set /p "destination=Enter destination: "
for /f "delims=" %%i in ('ping -4 -n 1 %destination% ^|find "["') do (
for %%j in (%%i) do (
echo %%j|find "[" >nul && (set "_ip=%%j" & goto :continue )
set "_name=%%j"
)
)
:continue
set _ip=%_ip:~1,-1%
set _

Make batch omit lines from a file

I'm creating a batch fingerprinter, and I'm using a mix of SystemInfo and IpConfig to get a unique sha256 hash for a computer, the problem is i the systeminfo output there are things like boot time and others that change every time, and i need a way of omitting them before i generate the hash.
my current whole code is:
#echo off
systeminfo >>fingerprint.txt
echo - - - - - - - - - - - - >> fingerprint.txt
ipconfig >>fingerprint.txt
:: Here i want to have the omitter
powershell Get-FileHash fingerprint.txt -Algorithm SHA256 > comphash.txt
pause
Like pretty much all information about your computer, the computer's unique identifier can be found in wmic. In this case, it's the UUID property of the csproduct class, which handles computer system product information from SMBIOS.
wmic has some extra carriage returns at the end of the data so there's a bit of tweaking that needs to be done before you can just run it through a for /f loop like a normal command, but it will look like this:
for /f "delims=" %%A in ('wmic csproduct get UUID /value ^| find "="') do set %%A
This will have the wmic command output its data in the format UUID=00000000-0000-0000-0000-000000000000 and then set the UUID to a variable called %UUID%, using find to look for the = and removing the excess carriage returns.
Found an answer thanks to Squashman, final code in case someone wants to use it is:
#echo off
color 33
systeminfo | find "Domain:" /v | find "System" /v | find "Virtual" /v | find "Available" /v >> fingerprint.txt
echo Your computers unique fingerprint: >> finalhash.txt
powershell Get-FileHash fingerprint.txt -Algorithm SHA256 >> finalhash.txt
del fingerprint.txt
start finalhash.txt
pause

Having trouble storing output of "wmic csproduct get vendor /format:list | findstr/c=" as a variable [duplicate]

This question already has answers here:
Batch WMIC redirecting output and wrapping into variable
(5 answers)
Closed 2 years ago.
Thank you for checking out my question.
I am trying to:
get the output of a wmic query into a variable
Here is some background information for why I need to get this accomplished.
The policy at my job requires that laptops and tablets equipped with webcams have them disabled in the BIOS.
Now that we are teleworking we have a need to enable the webcams in the BIOS.
We have tools that can be used to enable BIOS features while the system is running.
I want to write a script that will psexec into a machine, fetch the vendor information and store it in a variable that can be accessed later in the script once psexec exits.
I can write the rest of my script without issue I think, however I'm having trouble figuring out how to store the results of the following command in a variable:
wmic csproduct get vendor /format:list | findstr/c=
For example, the output of that command on my machine is:
Vendor=Dell Inc.
I want to capture the output and store it in a variable for later use in the script I'll be writing so that I can process vendor specific BIOS manipulation.
I have tried
set sysvend=wmic csproduct get vendor /format:list | findstr/c=
But when I echo %sysvend% to see if it worked I get the following instead of what I wanted:
wmic csproduct get vendor /format:list | findstr/c=
If anyone knows how to do this I would greatly appreciate the help. I am open to other alternatives for determining the vendor.
Even easier than using find or findstr and without the problematic 0x0D0D0A line endings:
#Echo Off & SetLocal EnableExtensions
For /F Tokens^=6Delims^=^" %%G In (
'%__APPDIR__%wbem\WMIC.exe CSProduct Get Vendor /Format:MOF 2^>NUL'
) Do Set "vendor=%%G"
This solution also prevents unwanted trailing characters with the vendor name string, which can be a common occurrence.
There is a known wmic bug, so you need to parse wmic output using two nested loops, e.g. as follows:
FOR /F "tokens=1* delims==" %%s IN ('
wmic csproduct get vendor /value ^| find "="
') DO for /F "tokens=*" %%i in ("%%t") do SET "vendor=%%i"
Here the for loops are
%%s to retrieve the vendor value;
%%i to remove the ending carriage return in the value returned (wmic behaviour): each output line ends with 0x0D0D0A (CR+CR+LF) instead of common 0x0D0A (CR+LF).
See Dave Benham's WMIC and FOR /F: A fix for the trailing <CR> problem
Answer posted to demonstrate the command can be assigned and executed as a variable, Contrary to Neko's answer, as per the OP's question.
#Echo Off
Set Get.Vendor=^>"%TEMP%\Vendor.log" (wmic csproduct get vendor /value ^|
findstr /c:=) ^&^& (For /F "UsebackQ Tokens=2* Delims==^EOL" %%A In ("%TEMP%\Vendor.log") Do For /F "Delims=" %%T In ("%%A") Do Set "Vendor=%%~T") ^&^& Echo.!Vendor!
Setlocal EnableDelayedExpansion
%Get.Vendor%
pause
Use for %%i in (wmic csproduct get vendor /format:list ^| findstr/c=) do (set var=%%~I)
What this does is that the for loop variable %%i becomes the output of the command in the for loop which allows you to set another variable the for loop variable. You need the caret (^) before the | in the for loop since if you didn't, the command would pipe out of the for loop and not execute in the for loop. You could also just surround the command with double quotes (") and you wouldn't need to escape the pipe. As a result of the command, %var% would become equal to the output of your wmic command.
The final command will be:
#echo off
for /f "tokens=* delims==" %%i in (' wmic csproduct get vendor /format:list ^| findstr /c:"=" ') do (set var=%%~i)
echo %var%
rem echoing var to make sure the variable is what you wanted
Output for me:
Vendor=Microsoft Corporation
If you want the output to be Microsoft Corporation specifically, you can add:
set var=%var:~7%
to remove the first 7 characters. You can also change tokens to 2* instead of *. :
for /f "tokens=2* delims==" %%j in (' wmic csproduct get vendor /format:list ^| findstr /c:"=" ') do (set var=%%~j)
Outputs:
Microsoft Corporation
Notes:
In batch-files, unlike powershell, you cannot set a variable as a command and have it become the output which is why the conventional way to set output of a command to a variable is using the for loop. Also, just preference, I would suggest using the /value switch rather than /format:list.
Tested and works for me

Batch commands for rds

I have a project for my Radio Station where I copy the text within a .wsx file of what is on air and parse it to a Audio Processor in my private network for RDS Display using a wget command like
set /p TEXTO= 0<R:\40.wsx
wget -q "http://x.x.x.x:7380/parameter/fm/rds/rds_rt=%TEXTO%" -O NUL
It works great but it won't filter if it's music or promotions.
My challenge is to be able to filter and only parse music names.
For the process I marked the Files that i don't want to show up like commercial, or promotions to start with a "#-" without the quotes.
So the text will show like #-Promo1
My Code:
for /F "delims=" %%a in ('FINDSTR "\<#-.*" C:\RDS\PRUEBAS\txt1.wsx') do set
"VAR=%%a"
echo %VAR%
if "%VAR%" == "true" (
set /p VAR=0<C:\FILEPATH\LOS40.wsx & wget -q
"http://x.x.x.x:7380/parameter/fm/rds/rds_rt=%VAR%" -O NUL
) else (
set /p TEXTO=0<C:\FILEPATH\ENVIVO.wsx & wget -q
"http://x.x.x.x:7380/parameter/fm/rds/rds_rt=%TEXTO%" -O NUL
)
I can't seem to find a correct way to filter it.
pls Heelpp..
for /F "delims=" %%a in ('FINDSTR "\<#-.*" C:\RDS\PRUEBAS\txt1.wsx') do set
should be
for /F "delims=" %%a in ('FINDSTR /b "#-" C:\RDS\PRUEBAS\txt1.wsx') do set
to find those lines in the .wsx file that do /b begin with "#-"
If you want to find those lines that do not begin with "#-" then add /v to the /b.
The result will be a line from the file which does [not] begin with "#-" which will be placed in %%a.
If you simply assign %%a to a variable as you are doing, that variable will contain after the for the last value that was assigned to it.
If you want to execute your wget on each name, then use
for /F "delims=" %%a in ('FINDSTR /B "#-" C:\RDS\PRUEBAS\txt1.wsx') do (
echo %%a
)
and between the parentheses you can execute commands using %%a as a filename.
Quite what you propose to do is obscure. I've no idea what the set/p from an unexplained file is meant to do, but be aware that any code between parentheses is subject to the delayedexpansion trap - please explain what processing you intend to apply to the filenames that do[not] match a leading #-.
You should read SO items on delayed expansion (it's documented with and without the space) to understand the problems with and solutions to processing values that are altered within a loop.
First of all thanks for your help.. I don't have experience in coding.
Let me explain a little bit more..
As I said before its a radio station which will provide text to the Car o home stereos using the RDS Protocol which allow me to send text like song name, title, etc.
Im My case the Audio Processor that let me send the text will receive the info as a URL where I add at the end the text I want to send.
For Example:
http://x.x.x.x:7380/parameter/fm/rds/rds_rt=%TEXTO%
%TEXTO% will be the text Im sending.
C:\RDS\PRUEBAS\txt1.wsx Contains the text of what it being played at the moment and which is being read to see if the #- for the script to avoid sending those titles.
I have a Pc running a Directory Monitor Program that will monitor events on file C:\RDS\PRUEBAS\txt1.wsx and that as soon as being modified, it will execute the batch file CHECKRDS.cmd.
After testing I decided to run a code in CHECKRDS.cmd like:
for /F "delims=" %%a in ('FINDSTR /v "#-" R:\40PPALES.wsx') do (
START "" RDS.bat
)
EXIT
RDS.bat contains code and it works fine:
set /p TEXTO= 0<C:\RDS\PRUEBAS\txt1.wsx
wget -q "http://x.x.x.x:7380/parameter/fm/rds/rds_rt=%TEXTO%" -O NUL
EXIT
As far as the set /p I test it from What does /p mean in set /p?
AS I said before Im new at coding and I just googled everything and started to assable the pieces of the puzzle.
Pls, If you think I should be doing these process diferently, pls let me know..
And sorry for my bad english..
regards

batch that return drive letter of USB with specific volume name

I do not have much experience with batch an d need a help with batch script.
Task is, return drive letter as parameter to %disk_letter%
Idea is use this for search:
WMIC LogicalDisk Where VolumeName='MY_USB' Get /Format:list | FIND "Caption="
I have "Caption=G:" as the result. I need that %disk_leter% parameter was equal just "G:"
Need help to finish this script.
Thank you!
On Linux right now but here's what I think you'll need to do. Part 1: save the result of your FIND command to a variable, and 2: take a substring of the variable. The second part is simple, so I'll start with that (assuming that in the first step you named your variable var
#echo %var:~-2%
That's about as far as I'm comfortable in batch, so this next bit is cobbled together:
To store the result of your find as a variable, try amending your code to:
set cmd="WMIC LogicalDisk Where VolumeName='MY_USB' Get /Format:list | FIND "Caption=" "
FOR /F %%i IN (' %cmd% ') DO SET var=%%i
and then (remember above) output it with:
#echo %var:~-2%
The related question from which I am cobbling together the second part is this question so if this doesn't work as expected I would jump over to that one first.
Here goes...
#echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype
2^>NUL`) do (
if %%l equ 2 (
echo %%i is a USB drive.
)
)

Resources