Batch file extract string part - windows

I have this window's batch command:
wmic process call create "notepad.exe" | find "ProcessId"
It returns this string
(spaces) ProcessId = 13764;
And I need to store in a variable only the pid number (13764), how can I do?

for /f "tokens=2 delims=;= " %%P in ('wmic process call create "notepad.exe" ^| find "ProcessId"') do echo %%P

for /F "delims=" %%a in ('wmic process call create "notepad.exe" ^| find "ProcessId"') do (
for %%b in (%%a) do set value=%%b
)
echo %value%
This method return the last word in the line, so it may be used also in other lines with variable number of words at beginning.

Use this:
echo %strContent:~6, -1%

for /f "tokens=3 delims=;=" %%a in ("(spaces) ProcessId = 13764;") do set value=%%c
echo %value%

Related

How would I isolate or just store the second element in a for loop inside a batch file?

I'm using a loop inside a Windows batch file to return PID numbers and I only want to use the second PID number. How do I set the second element, the second PID number as a variable or just use it in a command in the loop instead of echo?
The code..
for /f "tokens=2 delims=," %%a in ('tasklist /FO CSV ^| findstr /I cmd.exe') do (
echo PID:%%a
)
I tried indexing the element like this PID:%%a[1]. It didn't work.
Perhaps you want deal like this code ?
#echo off
SetLocal EnableDelayedExpansion
#for /f "tokens=2 delims=," %%a in ('tasklist /FO CSV ^| findstr /I cmd.exe') do (
set /A count+=1
Set "PID[!count!]=%%~a"
)
#For /L %%i in (1,1,%count%) do echo PID[%%i] = !PID[%%i]!
echo( I want this PID = !PID[2]!
pause

Start program and get process id

In batch file I can start a program in parallel to current session via
start "" notepad.exe
but I need to get a handle of the process I've started. How can I get it?
#echo off
:: set your own command here
set "command=notepad"
set "workdir=."
set "ReturnValue="
set "ProcessId="
for /f " skip=5 eol=} tokens=* delims=" %%a in ('wmic process call create "%command%"^,"%workdir%"') do (
for /f "tokens=1,3 delims=; " %%c in ("%%a") do (
set "%%c=%%d"
)
)
if not %ReturnValue%==0 (
echo some kind of error - error code %ReturnValue%
) else if defined ProcessId echo PID -^> %ProcessId%
delims in this line for /f "tokens=1,3 delims=; " should be for /f "tokens=1,3 delims=;<tab><space>" and I don't know if the tab will correctly copied.You also need to check if your editor replaces tab with spaces.Check also this.
Based on #npocmaka answer found the other solution:
#echo off
set pid=0
for /f "tokens=2 delims==; " %%a in ('wmic process call create "notepad.exe"^,"%~dp0." ^| find "ProcessId"') do set pid=%%a
echo %pid%
timeout 5
taskkill /pid %pid%

Echo is not working correctly in my Windows Batch script

Main Problem:
The ECHO command ignores all characters before the token argument and inserts all text after the token argument before the token in the output:
Here's the script:
#ECHO OFF
FOR /F "USEBACKQ TOKENS=2 DELIMS==" %%a IN (`
WMIC NIC WHERE
"MACAddress IS NOT NULL"
GET
MACAddress
/FORMAT:LIST ^|
FINDSTR /V /R "^%"
`) DO (
ECHO [%%a]
ECHO 0123456789%%a9876543210
)
PAUSE > NUL
Here's the expected output:
[AA:BB:CC:11:22:33]
0123456789AA:BB:CC:11:22:339876543210
Here's the actual output:
]AA:BB:CC:11:22:33
9876543210AA:BB:CC:11:22:33
Why do I care?
My intention is to replace the DO () block in the example above with this block:
:: ...FOR LOOP STUFF...
`) DO (
WMIC NIC WHERE ^
"MACAddress=%%a" ^
GET ^
NetConnectionID ^
/FORMAT:LIST | FINDSTR /V /R "^$"
WMIC NICCONFIG WHERE ^
"MACAddress=%%a" ^
GET ^
DefaultIPGateway,^
DNSServerSearchOrder,^
IPAddress,^
IPSubnet,^
MacAddress ^
/FORMAT:LIST | FINDSTR /V /R "^$"
)
As described above, all characters before %%a get cut off and all characters after %%a show up before %%a when the script is processed. Somebody please help, I'm figuratively crying here.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=2 DELIMS==" %%a IN ('
WMIC NIC WHERE "MACAddress IS NOT NULL" GET MACAddress
/FORMAT:LIST
') DO (
SET "macaddr=%%a"
SET "macaddr=!macaddr:~0,-1!"
ECHO [!macaddr!]
ECHO 0123456789!macaddr!9876543210
)
GOTO :EOF
I could find no reason for the findstr - as posted, the code gave me no output at all. Removing the findstr provided the reported output.
The wmic quirk can be observed by redirecting wmic output to a file and examining the result with a hex-editor. The cure is to simply mechanically remove the last character of the value assigned to the token.
I simply use Getmac command like in this script :
#echo off
mode 80,5 & Color 9E
Title Get (LAN ,Public) (IP) and MAC Addresses by Hackoo 2017
cls
echo( & echo(
echo Please Wait a While ... Searching for (LAN ,Public)(IP) and MAC addresses ...
#for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do (
set "LAN_IP=%%a"
)
for /f "tokens=2 delims=: " %%A in (
'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
) Do set ExtIP=%%A
#For /f %%a in ('getmac /NH /FO Table') do (
#For /f %%b in ('echo %%a') do (
If /I NOT "%%b"=="N/A" (
Set "MY_MAC=%%b"
)
)
)
Cls
echo(
echo My Private LAN IP : %LAN_IP%
echo My External Public IP : %ExtIP%
echo MAC Address : %MY_MAC%
Timeout /T 5 /NoBreak>nul & Exit

wmic useraccount SID blank spaces/empty lines

I have this two commands to get SID useraccount
wmic useraccount where name='%username%' get sid | findstr /b /C:"S-1" > file.txt
or
for /F "tokens=2 delims=," %f in ('whoami /user /FO CSV /NH') do echo %~f > file.txt
both cases (file.txt) contain blank spaces/empty lines. I try to remove with
findstr /r /v "^$"
But it is impossible. How can remove all blank spaces/empty lines to get only SID number
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "skip=2 tokens=2 delims=," %%a in ('
wmic path win32_useraccount
where name^="%username%"
get sid^,status /format:csv
') do (
>"file.txt" <nul set /p"=%%a"
)
That will skip headers, include an additional field so the sid is the second field in the record (in csv format the node field gets included) so we can avoid an ending carriage return in the output of the wmic command (each line ends in CR+CR+LF) and the output is sent to the file using a set /p command with the input stream reading from nul device. This way the output to the file will not include the ending CR+LF that is included with echo
The same code for in the whoami version
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "tokens=2 delims=," %%a in ('
whoami /user /FO CSV /NH
') do (
>"file.txt" <nul set /p"=%%~a"
)
This batch file can did the trick :
#echo off
for /f "delims= " %%a in ('"wmic path win32_useraccount where name='%UserName%' get sid"') do (
if not "%%a"=="SID" (
set myvar=%%a
goto :loop_end
)
)
:loop_end
echo SID=%myvar%
echo %myvar% > SID_%username%.txt
pause>nul
Start "" SID_%username%.txt
For the second method of whoami /user /FO CSV /NH ,you can take a look at this :
Get SID for current logged in domain user
Here's how I'd do it, WMIC:
for /f "skip=1" %a in ('"wmic useraccount where name='%username%' get sid"') do #for %b in (%a) do #(>file.txt echo(%b)
and with WhoAmI
for /f tokens^=3^ delims^=^" %a in ('whoami /user /FO CSV /NH') do #(>file.txt echo(%a)
both in the command line to match the question (ignoring the batch-file tag).

Using FOR Loop and SC Query in CMD Script

Suppose this instruction:
C:\>SC query state= all | find /I "adobe"
The result is:
SERVICE_NAME: AdobeARMservice
DISPLAY_NAME: Adobe Acrobat Update Service
Now I want to obtain the exact argument in this case is "AdobeARMservice"
Here my Script TestLineArgument.bat:
#echo off
cls
REM
REM for /f "delims=" %%# in ('SC query state= all ^| find /I "adobe"') do ( IS NOT WORKING
REM
set "SCQueryState=SC query state ^= all"
set "QueryAdobe=%SCQueryState%^^^| find /I ^"adobe^""
for /f "delims=" %%# in ("%QueryAdobe%") do (
set "THERSERVICE_NAME=%%#"
echo %%#
goto :END_FORSCQUERY
)
:END_FORSCQUERY
REM show the Line "SERVICE_NAME: AdobeARMservice"
echo THERSERVICE_NAME:%THERSERVICE_NAME%
FOR /f "tokens=2" %%# IN ("%THERSERVICE_NAME%") DO (
set "THESERVICE=%%#"
)
REM show the second part "AdobeARMservice"
echo THESERVICE:%THESERVICE%
My Script is not working, I don't know how use FOR with SC Command
Some clue?
#echo off
setlocal enableextensions disabledelayedexpansion
rem Initialize variables
set "serviceName="
set "displayName="
rem Execute the query and retrieve all the SERVICE_NAME lines and
rem the line with the desired display name
for /f "tokens=1,*" %%a in ('
sc query state^= all ^| findstr /i /l /c:"SERVICE_NAME" /c:"Adobe"
') do if not defined displayName if "%%a"=="SERVICE_NAME:" (
set "serviceName=%%b"
) else set "displayName=%%b"
rem Show retrieved data
if defined displayName (
echo %serviceName%
echo %displayName%
) else echo Not found
This method does not work in the manner you were trying to implement, as #MCND answer does, but if "AdobeARMservice" is only returned by sc query state when it is running, this may be useful.
It simply looks for the string "AdobeARMservice" and sets THESERVICE var to the result: If "AdobeARMservice" not found, THESERVICE remains empty. If found it gets value=AdobeARMservice
#echo off
Set "THESERVICE="
For /f %%A in ('sc query state^= all ^| find /i "AdobeARMservice"') do set THESERVICE=%%A
If "%THESERVICE%"=="" echo AdobeARMservice not found
If NOT "%THESERVICE%"=="" echo THESERVICE: %THESERVICE%
pause
My problem was simply...
#echo off
set "SERVICE=Adobe"
for /f "delims=" %%s in ('sc query state^= all ^| find /I "%SERVICE%"') do (
for /f "tokens=2" %%# in ("%%s") do (
echo THESERVICE:%%#
)
)
I tryng to avoid to change the code according to version something like : "Adobe3.2_1" or "Adobe3.2_5" or "Adobe4.5", this snippet of code only...

Resources